Skip to content

Commit e35336e

Browse files
authored
fix(origin): reduce blobrefresh namespace metric tag cardinality (#575)
1 parent b1cc7f1 commit e35336e

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

lib/blobrefresh/refresher.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package blobrefresh
1616
import (
1717
"errors"
1818
"fmt"
19+
"strings"
1920
"time"
2021

2122
"github.com/uber/kraken/core"
@@ -112,14 +113,13 @@ func (r *Refresher) Refresh(namespace string, d core.Digest, hooks ...PostHook)
112113
return err
113114
}
114115
t := time.Since(start)
115-
r.stats.Tagged(map[string]string{"namespace": namespace}).
116-
Timer("download_remote_blob").Record(t)
116+
stats := r.stats.Tagged(map[string]string{"namespace": extractPrefix(namespace)})
117+
stats.Timer("download_remote_blob").Record(t)
118+
stats.Counter("downloads").Inc(1)
117119
log.With(
118120
"namespace", namespace,
119121
"name", d.Hex(),
120122
"download_time", t).Info("Downloaded remote blob")
121-
r.stats.Tagged(map[string]string{"namespace": namespace}).
122-
Counter("downloads").Inc(1)
123123
for _, h := range hooks {
124124
h.Run(d)
125125
}
@@ -143,3 +143,14 @@ func (r *Refresher) download(client backend.Client, namespace string, d core.Dig
143143
return client.Download(namespace, name, w)
144144
}, pieceLength)
145145
}
146+
147+
// extractPrefix extracts the prefix from the namespace to decrease the cardinality of metrics.
148+
func extractPrefix(namespace string) string {
149+
if prefix, _, ok := strings.Cut(namespace, "/"); ok {
150+
return prefix
151+
}
152+
if prefix, _, ok := strings.Cut(namespace, "_"); ok {
153+
return prefix
154+
}
155+
return namespace
156+
}

lib/blobrefresh/refresher_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,41 @@ func TestRefreshWithMemoryCache(t *testing.T) {
212212
require.NoError(err)
213213
require.Equal(blob.Content, diskData)
214214
}
215+
216+
func TestExtractPrefix(t *testing.T) {
217+
tests := map[string]struct {
218+
give string
219+
want string
220+
}{
221+
"slash delimiter returns prefix": {
222+
give: "foo/bar",
223+
want: "foo",
224+
},
225+
"underscore delimiter returns prefix": {
226+
give: "foo_bar",
227+
want: "foo",
228+
},
229+
"no delimiter returns whole string": {
230+
give: "foo",
231+
want: "foo",
232+
},
233+
"empty prefix returns empty string": {
234+
give: "/foo",
235+
want: "",
236+
},
237+
"empty string returns empty string": {
238+
give: "",
239+
want: "",
240+
},
241+
}
242+
243+
for name, tt := range tests {
244+
t.Run(name, func(t *testing.T) {
245+
require := require.New(t)
246+
247+
prefix := extractPrefix(tt.give)
248+
249+
require.Equal(tt.want, prefix)
250+
})
251+
}
252+
}

0 commit comments

Comments
 (0)