Skip to content

Commit

Permalink
add support to anonymize internal tags
Browse files Browse the repository at this point in the history
  • Loading branch information
shaan420 committed Oct 4, 2023
1 parent a5fc9af commit 0985f9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
12 changes: 9 additions & 3 deletions m3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ type Configuration struct {
// with the histogram bucket bound values.
HistogramBucketTagPrecision uint `yaml:"histogramBucketTagPrecision"`

// CommonTagsInternal are tags that should be added to all internal metrics
// InternalTags are tags that should be added to all internal metrics
// emitted by the reporter.
CommonTagsInternal map[string]string `yaml:"commonTagsInternal"`
InternalTags map[string]string `yaml:"internalTags"`

// InternalTagsToAnonymize are tags that should be anonymized in all internal
// metrics emitted by the reporter. The values of these tags will be replaced
// with the string "global".
InternalTagsToAnonymize []string `yaml:"internalTagsToAnonymize"`
}

// NewReporter creates a new M3 reporter from this configuration.
Expand All @@ -70,6 +75,7 @@ func (c Configuration) NewReporter() (Reporter, error) {
MaxPacketSizeBytes: c.PacketSize,
IncludeHost: c.IncludeHost,
HistogramBucketTagPrecision: c.HistogramBucketTagPrecision,
InternalTags: c.CommonTagsInternal,
InternalTags: c.InternalTags,
InternalTagsToAnonymize: c.InternalTagsToAnonymize,
})
}
2 changes: 1 addition & 1 deletion m3/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestConfigSimple(t *testing.T) {
assert.True(t, ok)
assert.True(t, tagEquals(reporter.commonTags, "service", "my-service"))
assert.True(t, tagEquals(reporter.commonTags, "env", "test"))
assert.Equal(t, 0, len(c.CommonTagsInternal))
assert.Equal(t, 0, len(c.InternalTags))
}

func TestConfigMulti(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions m3/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type Options struct {
HistogramBucketName string
HistogramBucketTagPrecision uint
InternalTags map[string]string
InternalTagsToAnonymize []string
}

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

for _, toAnonomize := range opts.InternalTagsToAnonymize {
// override if present, set if not.
internalTags[toAnonomize] = "global"
}

r.batchSizeHistogram = r.AllocateHistogram("tally.internal.batch-size", internalTags, buckets)
r.numBatchesCounter = r.AllocateCounter("tally.internal.num-batches", internalTags)
r.numMetricsCounter = r.AllocateCounter("tally.internal.num-metrics", internalTags)
Expand Down
16 changes: 11 additions & 5 deletions m3/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ func TestReporterCommmonTagsInternal(t *testing.T) {
IncludeHost: true,
MaxPacketSizeBytes: maxPacketSize,
InternalTags: internalTags,
InternalTagsToAnonymize: []string{
"host",
"instance",
},
})
require.NoError(t, err)
defer r.Close()
Expand All @@ -598,17 +602,19 @@ func TestReporterCommmonTagsInternal(t *testing.T) {
numInternalMetricsActual++
for k, v := range internalTags {
require.True(t, tagEquals(metric.Tags, k, v))
require.True(t, tagEquals(metric.Tags, "host", "global"))
require.True(t, tagEquals(metric.Tags, "instance", "global"))
}
} else {
require.Equal(t, "testCounter1", metric.Name)
require.False(t, tagIncluded(metric.Tags, "internal1"))
require.False(t, tagIncluded(metric.Tags, "internal2"))
// The following tags should not be present as part of the individual metrics
// as they are common tags.
require.False(t, tagIncluded(metric.Tags, "host"))
require.False(t, tagIncluded(metric.Tags, "instance"))
require.False(t, tagIncluded(metric.Tags, "service"))
}
// The following tags should not be present as part of the individual metrics
// as they are common tags.
require.False(t, tagIncluded(metric.Tags, "host"))
require.False(t, tagIncluded(metric.Tags, "instance"))
require.False(t, tagIncluded(metric.Tags, "service"))
}
require.Equal(t, internalMetrics, numInternalMetricsActual)
}
Expand Down

0 comments on commit 0985f9e

Please sign in to comment.