Skip to content

Commit 3853e46

Browse files
shaan420vdarulis
andauthored
Prepare v3.5.6 (#232)
* Set default reporting interval (#226) * Enforce minimum reporting interval * Add new RootScope constructor with default interval * Test with go >= 1.18 (#228) * add custom tags to internal metrics (#231) * add custom tags to internal metrics * make unit test stricter, update version * update version to v3.5.6 --------- Co-authored-by: Vytenis Darulis <[email protected]>
1 parent 58ed96f commit 3853e46

8 files changed

+104
-8
lines changed

.travis.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
language: go
22
sudo: false
33
go:
4-
- 1.14.x
5-
- 1.15.x
6-
- 1.16.x
7-
env:
8-
global:
9-
- GO15VENDOREXPERIMENT=1
4+
- 1.18.x
5+
- 1.19.x
6+
- 1.20.x
7+
- 1.21.x
108
cache:
119
directories:
1210
- vendor

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)

scope.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const (
3939
// OmitInternalMetrics turns off internal metrics submission.
4040
OmitInternalMetrics
4141

42-
_defaultInitialSliceSize = 16
42+
_defaultInitialSliceSize = 16
43+
_defaultReportingInterval = 2 * time.Second
4344
)
4445

4546
var (
@@ -127,6 +128,12 @@ func NewRootScope(opts ScopeOptions, interval time.Duration) (Scope, io.Closer)
127128
return s, s
128129
}
129130

131+
// NewRootScopeWithDefaultInterval invokes NewRootScope with the default
132+
// reporting interval of 2s.
133+
func NewRootScopeWithDefaultInterval(opts ScopeOptions) (Scope, io.Closer) {
134+
return NewRootScope(opts, _defaultReportingInterval)
135+
}
136+
130137
// NewTestScope creates a new Scope without a stats reporter with the
131138
// given prefix and adds the ability to take snapshots of metrics emitted
132139
// to it.

scope_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@ func TestWriteReportLoop(t *testing.T) {
397397
r.WaitAll()
398398
}
399399

400+
func TestWriteReportLoopDefaultInterval(t *testing.T) {
401+
r := newTestStatsReporter()
402+
s, closer := NewRootScopeWithDefaultInterval(
403+
ScopeOptions{Reporter: r, MetricsOption: OmitInternalMetrics},
404+
)
405+
defer closer.Close()
406+
407+
r.cg.Add(1)
408+
s.Counter("bar").Inc(1)
409+
r.gg.Add(1)
410+
s.Gauge("zed").Update(1)
411+
r.tg.Add(1)
412+
s.Timer("ticky").Record(time.Millisecond * 175)
413+
r.hg.Add(1)
414+
s.Histogram("baz", MustMakeLinearValueBuckets(0, 10, 10)).
415+
RecordValue(42.42)
416+
417+
r.WaitAll()
418+
}
419+
400420
func TestCachedReportLoop(t *testing.T) {
401421
r := newTestStatsReporter()
402422
s, closer := NewRootScope(ScopeOptions{CachedReporter: r, MetricsOption: OmitInternalMetrics}, 10)

version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
package tally
2222

2323
// Version is the current version of the library.
24-
const Version = "3.5.5"
24+
const Version = "3.5.6"

0 commit comments

Comments
 (0)