Skip to content

Commit

Permalink
feat(telemetry): replace unique ids for resources with a slug (#21158)
Browse files Browse the repository at this point in the history
* feat(telemetry): replace unique ids for resources with a slug

* chore: update CHANGELOG
  • Loading branch information
williamhbaker authored Apr 8, 2021
1 parent 7b2e122 commit 540bb66
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The prefix used for Prometheus metrics from the query controller has changed fro
1. [21006](https://github.com/influxdata/influxdb/pull/21006): Add `-p, --profilers` flag to `influx query` command.
1. [21090](https://github.com/influxdata/influxdb/pull/21090): Update UI to match InfluxDB Cloud.
1. [21127](https://github.com/influxdata/influxdb/pull/21127): Allow for disabling concurrency-limits in Flux controller.
1. [21158](https://github.com/influxdata/influxdb/pull/21158): Replace unique resource IDs (UI assets, backup shards) with slugs to reduce cardinality of telemetry data.

### Bug Fixes

Expand Down
56 changes: 56 additions & 0 deletions kit/transport/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -111,17 +112,32 @@ func UserAgent(r *http.Request) string {
return ua.Parse(header).Name
}

// Constants used for normalizing paths
const (
fileSlug = ":file_name"
shardSlug = ":shard_id"
)

func normalizePath(p string) string {
// Normalize any paths used during backup or restore processes
p = normalizeBackupAndRestore(p)

// Go through each part of the path and normalize IDs and UI assets
var parts []string
for head, tail := shiftPath(p); ; head, tail = shiftPath(tail) {
piece := head

// Normalize any ID's in the path as the ":id" slug
if len(piece) == platform.IDLength {
if _, err := platform.IDFromString(head); err == nil {
piece = ":id"
}
}
parts = append(parts, piece)

if tail == "/" {
// Normalize UI asset file names. The UI asset file is the last part of the path.
parts[len(parts)-1] = normalizeAssetFile(parts[len(parts)-1])
break
}
}
Expand All @@ -137,6 +153,46 @@ func shiftPath(p string) (head, tail string) {
return p[1:i], p[i:]
}

// Normalize the file name for a UI asset
// For example: 838442d56d.svg will return as :file_id.svg
// Files names that do not have one of the listed extensions will be returned unchanged
func normalizeAssetFile(f string) string {
exts := []string{
".js",
".svg",
".woff2",
".wasm",
".map",
".LICENSE",
}

for _, ext := range exts {
if strings.HasSuffix(f, ext) {
return fileSlug + ext
}
}

return f
}

// Normalize paths used during the backup and restore process.
// Paths not matching any of the patterns will be returned unchanged.
func normalizeBackupAndRestore(pth string) string {
patterns := map[string]string{
`restore/shards/\d+`: path.Join("restore/shards", shardSlug),
`backup/shards/\d+`: path.Join("backup/shards", shardSlug),
}

for p, s := range patterns {
re := regexp.MustCompile(p)
if re.MatchString(pth) {
return re.ReplaceAllString(pth, s)
}
}

return pth
}

type OrgContext string

const CtxOrgKey OrgContext = "orgID"
Expand Down
30 changes: 30 additions & 0 deletions kit/transport/http/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@ func Test_normalizePath(t *testing.T) {
path: path.Join("/api/v2/organizations", platform.ID(2).String(), "users", platform.ID(3).String()),
expected: "/api/v2/organizations/:id/users/:id",
},
{
name: "5",
path: "/838442d56d.svg",
expected: "/" + fileSlug + ".svg",
},
{
name: "6",
path: "/838442d56d.svg/extra",
expected: "/838442d56d.svg/extra",
},
{
name: "7",
path: "/api/v2/restore/shards/1001",
expected: path.Join("/api/v2/restore/shards/", shardSlug),
},
{
name: "8",
path: "/api/v2/restore/shards/1001/extra",
expected: path.Join("/api/v2/restore/shards/", shardSlug, "extra"),
},
{
name: "9",
path: "/api/v2/backup/shards/1005",
expected: path.Join("/api/v2/backup/shards/", shardSlug),
},
{
name: "10",
path: "/api/v2/backup/shards/1005/extra",
expected: path.Join("/api/v2/backup/shards/", shardSlug, "extra"),
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 540bb66

Please sign in to comment.