Skip to content

Commit d083654

Browse files
committed
add custom tags to internal metrics (#231)
* add custom tags to internal metrics * make unit test stricter, update version
1 parent 9e0a0b8 commit d083654

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

m3/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type Configuration struct {
4949
// HistogramBucketTagPrecision is precision to use when formatting the metric tag
5050
// with the histogram bucket bound values.
5151
HistogramBucketTagPrecision uint `yaml:"histogramBucketTagPrecision"`
52+
53+
// CommonTagsInternal are tags that should be added to all internal metrics
54+
// emitted by the reporter.
55+
CommonTagsInternal map[string]string `yaml:"commonTagsInternal"`
5256
}
5357

5458
// NewReporter creates a new M3 reporter from this configuration.
@@ -66,5 +70,6 @@ func (c Configuration) NewReporter() (Reporter, error) {
6670
MaxPacketSizeBytes: c.PacketSize,
6771
IncludeHost: c.IncludeHost,
6872
HistogramBucketTagPrecision: c.HistogramBucketTagPrecision,
73+
InternalTags: c.CommonTagsInternal,
6974
})
7075
}

m3/config_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestConfigSimple(t *testing.T) {
4343
assert.True(t, ok)
4444
assert.True(t, tagEquals(reporter.commonTags, "service", "my-service"))
4545
assert.True(t, tagEquals(reporter.commonTags, "env", "test"))
46+
assert.Equal(t, 0, len(c.CommonTagsInternal))
4647
}
4748

4849
func TestConfigMulti(t *testing.T) {

m3/reporter.go

+6
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ type Options struct {
149149
HistogramBucketIDName string
150150
HistogramBucketName string
151151
HistogramBucketTagPrecision uint
152+
InternalTags map[string]string
152153
}
153154

154155
// NewReporter creates a new M3 reporter.
@@ -288,6 +289,11 @@ func NewReporter(opts Options) (Reporter, error) {
288289
internalTags := map[string]string{
289290
"version": tally.Version,
290291
}
292+
293+
for k, v := range opts.InternalTags {
294+
internalTags[k] = v
295+
}
296+
291297
r.batchSizeHistogram = r.AllocateHistogram("tally.internal.batch-size", internalTags, buckets)
292298
r.numBatchesCounter = r.AllocateCounter("tally.internal.num-batches", internalTags)
293299
r.numMetricsCounter = r.AllocateCounter("tally.internal.num-metrics", internalTags)

m3/reporter_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,58 @@ func TestReporterResetTagsAfterReturnToPool(t *testing.T) {
559559
require.Equal(t, 0, len(filtered[1].GetTags()))
560560
}
561561

562+
func TestReporterCommmonTagsInternal(t *testing.T) {
563+
var wg sync.WaitGroup
564+
server := newFakeM3Server(t, &wg, false, Compact)
565+
go server.Serve()
566+
defer server.Close()
567+
568+
internalTags := map[string]string{
569+
"internal1": "test1",
570+
"internal2": "test2",
571+
}
572+
573+
r, err := NewReporter(Options{
574+
HostPorts: []string{server.Addr},
575+
Service: "test-service",
576+
CommonTags: defaultCommonTags,
577+
MaxQueueSize: queueSize,
578+
IncludeHost: true,
579+
MaxPacketSizeBytes: maxPacketSize,
580+
InternalTags: internalTags,
581+
})
582+
require.NoError(t, err)
583+
defer r.Close()
584+
585+
c := r.AllocateCounter("testCounter1", nil)
586+
c.ReportCount(1)
587+
wg.Add(internalMetrics + 1)
588+
r.Flush()
589+
wg.Wait()
590+
591+
numInternalMetricsActual := 0
592+
metrics := server.Service.getMetrics()
593+
require.Equal(t, internalMetrics+1, len(metrics))
594+
for _, metric := range metrics {
595+
if strings.HasPrefix(metric.Name, "tally.internal") {
596+
numInternalMetricsActual++
597+
for k, v := range internalTags {
598+
require.True(t, tagEquals(metric.Tags, k, v))
599+
}
600+
} else {
601+
require.Equal(t, "testCounter1", metric.Name)
602+
require.False(t, tagIncluded(metric.Tags, "internal1"))
603+
require.False(t, tagIncluded(metric.Tags, "internal2"))
604+
}
605+
// The following tags should not be present as part of the individual metrics
606+
// as they are common tags.
607+
require.False(t, tagIncluded(metric.Tags, "host"))
608+
require.False(t, tagIncluded(metric.Tags, "instance"))
609+
require.False(t, tagIncluded(metric.Tags, "service"))
610+
}
611+
require.Equal(t, internalMetrics, numInternalMetricsActual)
612+
}
613+
562614
func TestReporterHasReportingAndTaggingCapability(t *testing.T) {
563615
r, err := NewReporter(Options{
564616
HostPorts: []string{"127.0.0.1:9052"},
@@ -587,6 +639,13 @@ type fakeM3ServerPackets struct {
587639
values [][]byte
588640
}
589641

642+
// newFakeM3Server creates a new fake M3 server that listens on a random port
643+
// and returns the server.
644+
// The server will wait for the given wait group to be done before returning.
645+
// If countBatches is true, the server will wait consider the wg.Add()s to be
646+
// representing batches and will do a eg.Done() for each encountered batch.
647+
// But if countBatches is false, the server will do the same thing but for individual
648+
// metrics instead of batches.
590649
func newFakeM3Server(t *testing.T, wg *sync.WaitGroup, countBatches bool, protocol Protocol) *fakeM3Server {
591650
service := newFakeM3Service(wg, countBatches)
592651
processor := m3thrift.NewM3Processor(service)

0 commit comments

Comments
 (0)