From b1994ea123c16b3b9d09250905531fb6f5039bb9 Mon Sep 17 00:00:00 2001 From: adamjeanlaurent Date: Mon, 12 Oct 2026 19:16:45 +0000 Subject: [PATCH] sent client library name --- m3/reporter.go | 17 ++++++++--- m3/reporter_test.go | 39 +++++++++++++++++++++++++ m3/thrift/thrift.diff | 66 ++++++++++++++++++++++-------------------- m3/thrift/v2/ttypes.go | 50 ++++++++++++++++++++++++++++++-- m3/thrift/v2/v2.thrift | 1 + 5 files changed, 135 insertions(+), 38 deletions(-) diff --git a/m3/reporter.go b/m3/reporter.go index 52eb6c7b..8b4676b4 100644 --- a/m3/reporter.go +++ b/m3/reporter.go @@ -58,6 +58,9 @@ const ( EnvTag = "env" // HostTag is the name of the M3 host tag. HostTag = "host" + // ClientLibraryName is the client library name that the reporter sends + // in the clientLibraryName field of each metric batch. + ClientLibraryName = "oss_go_tally" // DefaultMaxQueueSize is the default M3 reporter queue size. DefaultMaxQueueSize = 4096 // DefaultMaxPacketSize is the default M3 reporter max packet size. @@ -79,6 +82,10 @@ const ( var ( _maxInt64 = int64(math.MaxInt64) _maxFloat64 = math.MaxFloat64 + + // _clientLibraryName lets each batch point to the name without an + // allocation per flush. + _clientLibraryName = ClientLibraryName ) type metricType int @@ -240,8 +247,9 @@ func NewReporter(opts Options) (Reporter, error) { // Calculate size of common tags var ( batch = m3thrift.MetricBatch{ - Metrics: resourcePool.getMetricSlice(), - CommonTags: tags, + Metrics: resourcePool.getMetricSlice(), + CommonTags: tags, + ClientLibraryName: &_clientLibraryName, } proto = resourcePool.getProto() ) @@ -661,8 +669,9 @@ func (r *reporter) flush(mets []m3thrift.Metric) []m3thrift.Metric { r.numBatches.Inc() err := r.client.EmitMetricBatchV2(m3thrift.MetricBatch{ - Metrics: mets, - CommonTags: r.commonTags, + Metrics: mets, + CommonTags: r.commonTags, + ClientLibraryName: &_clientLibraryName, }) if err != nil { r.numWriteErrors.Inc() diff --git a/m3/reporter_test.go b/m3/reporter_test.go index a9ecb47e..6e163983 100644 --- a/m3/reporter_test.go +++ b/m3/reporter_test.go @@ -106,6 +106,8 @@ func TestReporter(t *testing.T) { // Validate common tags for _, batch := range batches { require.NotNil(t, batch) + require.True(t, batch.IsSetClientLibraryName()) + require.Equal(t, ClientLibraryName, batch.GetClientLibraryName()) require.True(t, batch.IsSetCommonTags()) require.Equal(t, len(commonTags)+1, len(batch.GetCommonTags())) for _, tag := range batch.GetCommonTags() { @@ -147,6 +149,43 @@ func TestReporter(t *testing.T) { } // TestMultiReporter tests the multi Reporter works as expected +func TestMetricBatchClientLibraryNameRoundTrip(t *testing.T) { + name := "custom_client" + tests := []struct { + desc string + name *string + }{ + {desc: "set", name: &name}, + {desc: "unset", name: nil}, + } + + factories := map[string]thrift.TProtocolFactory{ + "binary": thrift.NewTBinaryProtocolFactoryDefault(), + "compact": thrift.NewTCompactProtocolFactory(), + } + + for protoName, factory := range factories { + for _, tt := range tests { + t.Run(protoName+"/"+tt.desc, func(t *testing.T) { + var ( + buf = thrift.NewTMemoryBuffer() + proto = factory.GetProtocol(buf) + in = m3thrift.MetricBatch{ + Metrics: []m3thrift.Metric{{Name: "foo"}}, + ClientLibraryName: tt.name, + } + out m3thrift.MetricBatch + ) + require.NoError(t, in.Write(proto)) + require.NoError(t, out.Read(proto)) + require.Equal(t, tt.name != nil, out.IsSetClientLibraryName()) + require.Equal(t, in.GetClientLibraryName(), out.GetClientLibraryName()) + require.Equal(t, "foo", out.GetMetrics()[0].GetName()) + }) + } + } +} + func TestMultiReporter(t *testing.T) { dests := []string{"127.0.0.1:9052", "127.0.0.1:9053"} commonTags := map[string]string{ diff --git a/m3/thrift/thrift.diff b/m3/thrift/thrift.diff index 24d32c18..a45ec974 100644 --- a/m3/thrift/thrift.diff +++ b/m3/thrift/thrift.diff @@ -1,5 +1,5 @@ diff --git a/m3/thrift/v2/m3.go b/m3/thrift/v2/m3.go -index fc46a31..f0a288c 100644 +index f8a4434..0da14bd 100644 --- a/m3/thrift/v2/m3.go +++ b/m3/thrift/v2/m3.go @@ -17,7 +17,7 @@ var _ = bytes.Equal @@ -9,10 +9,10 @@ index fc46a31..f0a288c 100644 - EmitMetricBatchV2(batch *MetricBatch) (err error) + EmitMetricBatchV2(batch MetricBatch) (err error) } - + type M3Client struct { @@ -48,14 +48,14 @@ func NewM3ClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thri - + // Parameters: // - Batch -func (p *M3Client) EmitMetricBatchV2(batch *MetricBatch) (err error) { @@ -22,27 +22,27 @@ index fc46a31..f0a288c 100644 } return } - + -func (p *M3Client) sendEmitMetricBatchV2(batch *MetricBatch) (err error) { +func (p *M3Client) sendEmitMetricBatchV2(batch MetricBatch) (err error) { oprot := p.OutputProtocol if oprot == nil { oprot = p.ProtocolFactory.GetProtocol(p.Transport) -@@ -145,23 +145,23 @@ func (p *m3ProcessorEmitMetricBatchV2) Process(seqId int32, iprot, oprot thrift.TP +@@ -145,23 +145,23 @@ func (p *m3ProcessorEmitMetricBatchV2) Process(seqId int32, iprot, oprot thrift. // Attributes: // - Batch type M3EmitMetricBatchV2Args struct { - Batch *MetricBatch `thrift:"batch,1" json:"batch"` + Batch MetricBatch `thrift:"batch,1" json:"batch"` } - + func NewM3EmitMetricBatchV2Args() *M3EmitMetricBatchV2Args { return &M3EmitMetricBatchV2Args{} } - + -var M3EmitMetricBatchV2Args_Batch_DEFAULT *MetricBatch +var M3EmitMetricBatchV2Args_Batch_DEFAULT MetricBatch - + -func (p *M3EmitMetricBatchV2Args) GetBatch() *MetricBatch { +func (p *M3EmitMetricBatchV2Args) GetBatch() MetricBatch { if !p.IsSetBatch() { @@ -54,11 +54,11 @@ index fc46a31..f0a288c 100644 - return p.Batch != nil + return p.Batch.Metrics != nil || p.Batch.CommonTags != nil } - + func (p *M3EmitMetricBatchV2Args) Read(iprot thrift.TProtocol) error { @@ -198,7 +198,7 @@ func (p *M3EmitMetricBatchV2Args) Read(iprot thrift.TProtocol) error { } - + func (p *M3EmitMetricBatchV2Args) readField1(iprot thrift.TProtocol) error { - p.Batch = &MetricBatch{} + p.Batch = MetricBatch{} @@ -66,7 +66,7 @@ index fc46a31..f0a288c 100644 return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Batch), err) } diff --git a/m3/thrift/v2/ttypes.go b/m3/thrift/v2/ttypes.go -index 5f83caa..3e6c33b 100644 +index 759107c..ae8b68c 100644 --- a/m3/thrift/v2/ttypes.go +++ b/m3/thrift/v2/ttypes.go @@ -429,10 +429,10 @@ func (p *MetricTag) String() string { @@ -82,15 +82,15 @@ index 5f83caa..3e6c33b 100644 + Timestamp int64 `thrift:"timestamp,3,required" json:"timestamp"` + Tags []MetricTag `thrift:"tags,4" json:"tags,omitempty"` } - + func NewMetric() *Metric { @@ -443,9 +443,9 @@ func (p *Metric) GetName() string { return p.Name } - + -var Metric_Value_DEFAULT *MetricValue +var Metric_Value_DEFAULT MetricValue - + -func (p *Metric) GetValue() *MetricValue { +func (p *Metric) GetValue() MetricValue { if !p.IsSetValue() { @@ -99,10 +99,10 @@ index 5f83caa..3e6c33b 100644 @@ -456,13 +456,13 @@ func (p *Metric) GetTimestamp() int64 { return p.Timestamp } - + -var Metric_Tags_DEFAULT []*MetricTag +var Metric_Tags_DEFAULT []MetricTag - + -func (p *Metric) GetTags() []*MetricTag { +func (p *Metric) GetTags() []MetricTag { return p.Tags @@ -111,11 +111,11 @@ index 5f83caa..3e6c33b 100644 - return p.Value != nil + return p.Value.GetMetricType() != MetricType_INVALID || p.Value.Count != 0 || p.Value.Gauge != 0 || p.Value.Timer != 0 } - + func (p *Metric) IsSetTags() bool { @@ -540,7 +540,7 @@ func (p *Metric) readField1(iprot thrift.TProtocol) error { } - + func (p *Metric) readField2(iprot thrift.TProtocol) error { - p.Value = &MetricValue{} + p.Value = MetricValue{} @@ -135,34 +135,36 @@ index 5f83caa..3e6c33b 100644 if err := _elem0.Read(iprot); err != nil { return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _elem0), err) } -@@ -674,21 +674,21 @@ func (p *Metric) String() string { - // - Metrics +@@ -675,22 +675,22 @@ func (p *Metric) String() string { // - CommonTags + // - ClientLibraryName type MetricBatch struct { -- Metrics []*Metric `thrift:"metrics,1,required" json:"metrics"` -- CommonTags []*MetricTag `thrift:"commonTags,2" json:"commonTags,omitempty"` -+ Metrics []Metric `thrift:"metrics,1,required" json:"metrics"` -+ CommonTags []MetricTag `thrift:"commonTags,2" json:"commonTags,omitempty"` - } - +- Metrics []*Metric `thrift:"metrics,1,required" json:"metrics"` +- CommonTags []*MetricTag `thrift:"commonTags,2" json:"commonTags,omitempty"` +- ClientLibraryName *string `thrift:"clientLibraryName,3" json:"clientLibraryName,omitempty"` ++ Metrics []Metric `thrift:"metrics,1,required" json:"metrics"` ++ CommonTags []MetricTag `thrift:"commonTags,2" json:"commonTags,omitempty"` ++ ClientLibraryName *string `thrift:"clientLibraryName,3" json:"clientLibraryName,omitempty"` + } + func NewMetricBatch() *MetricBatch { return &MetricBatch{} } - + -func (p *MetricBatch) GetMetrics() []*Metric { +func (p *MetricBatch) GetMetrics() []Metric { return p.Metrics } - + -var MetricBatch_CommonTags_DEFAULT []*MetricTag +var MetricBatch_CommonTags_DEFAULT []MetricTag - + -func (p *MetricBatch) GetCommonTags() []*MetricTag { +func (p *MetricBatch) GetCommonTags() []MetricTag { return p.CommonTags } - func (p *MetricBatch) IsSetCommonTags() bool { -@@ -743,10 +743,10 @@ func (p *MetricBatch) readField1(iprot thrift.TProtocol) error { + +@@ -762,10 +762,10 @@ func (p *MetricBatch) readField1(iprot thrift.TProtocol) error { if err != nil { return thrift.PrependError("error reading list begin: ", err) } @@ -175,7 +177,7 @@ index 5f83caa..3e6c33b 100644 if err := _elem1.Read(iprot); err != nil { return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _elem1), err) } -@@ -763,10 +763,10 @@ func (p *MetricBatch) readField2(iprot thrift.TProtocol) error { +@@ -782,10 +782,10 @@ func (p *MetricBatch) readField2(iprot thrift.TProtocol) error { if err != nil { return thrift.PrependError("error reading list begin: ", err) } diff --git a/m3/thrift/v2/ttypes.go b/m3/thrift/v2/ttypes.go index cc890fe6..ae8b68cc 100644 --- a/m3/thrift/v2/ttypes.go +++ b/m3/thrift/v2/ttypes.go @@ -673,9 +673,11 @@ func (p *Metric) String() string { // Attributes: // - Metrics // - CommonTags +// - ClientLibraryName type MetricBatch struct { - Metrics []Metric `thrift:"metrics,1,required" json:"metrics"` - CommonTags []MetricTag `thrift:"commonTags,2" json:"commonTags,omitempty"` + Metrics []Metric `thrift:"metrics,1,required" json:"metrics"` + CommonTags []MetricTag `thrift:"commonTags,2" json:"commonTags,omitempty"` + ClientLibraryName *string `thrift:"clientLibraryName,3" json:"clientLibraryName,omitempty"` } func NewMetricBatch() *MetricBatch { @@ -691,10 +693,23 @@ var MetricBatch_CommonTags_DEFAULT []MetricTag func (p *MetricBatch) GetCommonTags() []MetricTag { return p.CommonTags } + +var MetricBatch_ClientLibraryName_DEFAULT string + +func (p *MetricBatch) GetClientLibraryName() string { + if !p.IsSetClientLibraryName() { + return MetricBatch_ClientLibraryName_DEFAULT + } + return *p.ClientLibraryName +} func (p *MetricBatch) IsSetCommonTags() bool { return p.CommonTags != nil } +func (p *MetricBatch) IsSetClientLibraryName() bool { + return p.ClientLibraryName != nil +} + func (p *MetricBatch) Read(iprot thrift.TProtocol) error { if _, err := iprot.ReadStructBegin(); err != nil { return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) @@ -720,6 +735,10 @@ func (p *MetricBatch) Read(iprot thrift.TProtocol) error { if err := p.readField2(iprot); err != nil { return err } + case 3: + if err := p.readField3(iprot); err != nil { + return err + } default: if err := iprot.Skip(fieldTypeId); err != nil { return err @@ -778,6 +797,15 @@ func (p *MetricBatch) readField2(iprot thrift.TProtocol) error { return nil } +func (p *MetricBatch) readField3(iprot thrift.TProtocol) error { + if v, err := iprot.ReadString(); err != nil { + return thrift.PrependError("error reading field 3: ", err) + } else { + p.ClientLibraryName = &v + } + return nil +} + func (p *MetricBatch) Write(oprot thrift.TProtocol) error { if err := oprot.WriteStructBegin("MetricBatch"); err != nil { return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) @@ -788,6 +816,9 @@ func (p *MetricBatch) Write(oprot thrift.TProtocol) error { if err := p.writeField2(oprot); err != nil { return err } + if err := p.writeField3(oprot); err != nil { + return err + } if err := oprot.WriteFieldStop(); err != nil { return thrift.PrependError("write field stop error: ", err) } @@ -841,6 +872,21 @@ func (p *MetricBatch) writeField2(oprot thrift.TProtocol) (err error) { return err } +func (p *MetricBatch) writeField3(oprot thrift.TProtocol) (err error) { + if p.IsSetClientLibraryName() { + if err := oprot.WriteFieldBegin("clientLibraryName", thrift.STRING, 3); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 3:clientLibraryName: ", p), err) + } + if err := oprot.WriteString(string(*p.ClientLibraryName)); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.clientLibraryName (3) field write error: ", p), err) + } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 3:clientLibraryName: ", p), err) + } + } + return err +} + func (p *MetricBatch) String() string { if p == nil { return "" diff --git a/m3/thrift/v2/v2.thrift b/m3/thrift/v2/v2.thrift index ab2b12f3..c122eb3c 100644 --- a/m3/thrift/v2/v2.thrift +++ b/m3/thrift/v2/v2.thrift @@ -27,6 +27,7 @@ struct Metric { struct MetricBatch { 1: required list metrics 2: optional list commonTags + 3: optional string clientLibraryName } service M3 {