Skip to content

Commit 0985f9e

Browse files
committed
add support to anonymize internal tags
1 parent a5fc9af commit 0985f9e

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

m3/config.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ type Configuration struct {
5050
// with the histogram bucket bound values.
5151
HistogramBucketTagPrecision uint `yaml:"histogramBucketTagPrecision"`
5252

53-
// CommonTagsInternal are tags that should be added to all internal metrics
53+
// InternalTags are tags that should be added to all internal metrics
5454
// emitted by the reporter.
55-
CommonTagsInternal map[string]string `yaml:"commonTagsInternal"`
55+
InternalTags map[string]string `yaml:"internalTags"`
56+
57+
// InternalTagsToAnonymize are tags that should be anonymized in all internal
58+
// metrics emitted by the reporter. The values of these tags will be replaced
59+
// with the string "global".
60+
InternalTagsToAnonymize []string `yaml:"internalTagsToAnonymize"`
5661
}
5762

5863
// NewReporter creates a new M3 reporter from this configuration.
@@ -70,6 +75,7 @@ func (c Configuration) NewReporter() (Reporter, error) {
7075
MaxPacketSizeBytes: c.PacketSize,
7176
IncludeHost: c.IncludeHost,
7277
HistogramBucketTagPrecision: c.HistogramBucketTagPrecision,
73-
InternalTags: c.CommonTagsInternal,
78+
InternalTags: c.InternalTags,
79+
InternalTagsToAnonymize: c.InternalTagsToAnonymize,
7480
})
7581
}

m3/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestConfigSimple(t *testing.T) {
4242
assert.True(t, ok)
4343
assert.True(t, tagEquals(reporter.commonTags, "service", "my-service"))
4444
assert.True(t, tagEquals(reporter.commonTags, "env", "test"))
45-
assert.Equal(t, 0, len(c.CommonTagsInternal))
45+
assert.Equal(t, 0, len(c.InternalTags))
4646
}
4747

4848
func TestConfigMulti(t *testing.T) {

m3/reporter.go

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ type Options struct {
150150
HistogramBucketName string
151151
HistogramBucketTagPrecision uint
152152
InternalTags map[string]string
153+
InternalTagsToAnonymize []string
153154
}
154155

155156
// NewReporter creates a new M3 reporter.
@@ -294,6 +295,11 @@ func NewReporter(opts Options) (Reporter, error) {
294295
internalTags[k] = v
295296
}
296297

298+
for _, toAnonomize := range opts.InternalTagsToAnonymize {
299+
// override if present, set if not.
300+
internalTags[toAnonomize] = "global"
301+
}
302+
297303
r.batchSizeHistogram = r.AllocateHistogram("tally.internal.batch-size", internalTags, buckets)
298304
r.numBatchesCounter = r.AllocateCounter("tally.internal.num-batches", internalTags)
299305
r.numMetricsCounter = r.AllocateCounter("tally.internal.num-metrics", internalTags)

m3/reporter_test.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,10 @@ func TestReporterCommmonTagsInternal(t *testing.T) {
580580
IncludeHost: true,
581581
MaxPacketSizeBytes: maxPacketSize,
582582
InternalTags: internalTags,
583+
InternalTagsToAnonymize: []string{
584+
"host",
585+
"instance",
586+
},
583587
})
584588
require.NoError(t, err)
585589
defer r.Close()
@@ -598,17 +602,19 @@ func TestReporterCommmonTagsInternal(t *testing.T) {
598602
numInternalMetricsActual++
599603
for k, v := range internalTags {
600604
require.True(t, tagEquals(metric.Tags, k, v))
605+
require.True(t, tagEquals(metric.Tags, "host", "global"))
606+
require.True(t, tagEquals(metric.Tags, "instance", "global"))
601607
}
602608
} else {
603609
require.Equal(t, "testCounter1", metric.Name)
604610
require.False(t, tagIncluded(metric.Tags, "internal1"))
605611
require.False(t, tagIncluded(metric.Tags, "internal2"))
612+
// The following tags should not be present as part of the individual metrics
613+
// as they are common tags.
614+
require.False(t, tagIncluded(metric.Tags, "host"))
615+
require.False(t, tagIncluded(metric.Tags, "instance"))
616+
require.False(t, tagIncluded(metric.Tags, "service"))
606617
}
607-
// The following tags should not be present as part of the individual metrics
608-
// as they are common tags.
609-
require.False(t, tagIncluded(metric.Tags, "host"))
610-
require.False(t, tagIncluded(metric.Tags, "instance"))
611-
require.False(t, tagIncluded(metric.Tags, "service"))
612618
}
613619
require.Equal(t, internalMetrics, numInternalMetricsActual)
614620
}

0 commit comments

Comments
 (0)