Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions m3/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
Expand Down
39 changes: 39 additions & 0 deletions m3/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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{
Expand Down
66 changes: 34 additions & 32 deletions m3/thrift/thrift.diff
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -54,19 +54,19 @@ 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{}
if err := p.Batch.Read(iprot); err != nil {
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 {
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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{}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
50 changes: 48 additions & 2 deletions m3/thrift/v2/ttypes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions m3/thrift/v2/v2.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct Metric {
struct MetricBatch {
1: required list<Metric> metrics
2: optional list<MetricTag> commonTags
3: optional string clientLibraryName
}

service M3 {
Expand Down
Loading