Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 4351271

Browse files
draffenspergersongy23
authored andcommitted
Don't overwrite agent label if already set (#132)
1 parent aed34d9 commit 4351271

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

trace_proto.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,16 @@ func protoFromSpanData(s *trace.SpanData, projectID string, mr *monitoredrespb.M
105105
AttributeMap: make(map[string]*tracepb.AttributeValue),
106106
}
107107
}
108-
sp.Attributes.AttributeMap[agentLabel] = &tracepb.AttributeValue{
109-
Value: &tracepb.AttributeValue_StringValue{
110-
StringValue: trunc(userAgent, maxAttributeStringValue),
111-
},
108+
109+
// Only set the agent label if it is not already set. That enables the
110+
// OpenCensus agent/collector to set the agent label based on the library that
111+
// sent the span to the agent.
112+
if _, hasAgent := sp.Attributes.AttributeMap[agentLabel]; !hasAgent {
113+
sp.Attributes.AttributeMap[agentLabel] = &tracepb.AttributeValue{
114+
Value: &tracepb.AttributeValue_StringValue{
115+
StringValue: trunc(userAgent, maxAttributeStringValue),
116+
},
117+
}
112118
}
113119

114120
es := s.MessageEvents

trace_proto_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func generateSpan() {
7575
span2.Annotate(nil, big.NewRat(2, 4).String())
7676
span2.AddAttributes(
7777
trace.StringAttribute("key1", "value1"),
78-
trace.StringAttribute("key2", "value2"))
78+
trace.StringAttribute("key2", "value2"),
79+
trace.StringAttribute(agentLabel, "custom-agent"))
7980
span2.AddAttributes(
8081
trace.Int64Attribute("key1", 100),
8182
// TODO [rghetia]: uncomment the test case after go.opencensus.io/trace@v0.20.0 is released.
@@ -138,7 +139,7 @@ func createExpectedSpans() spans {
138139
"key1": {Value: &tracepb.AttributeValue_IntValue{IntValue: 100}},
139140
// TODO [rghetia]: uncomment the test case after go.opencensus.io/trace@v0.20.0 is released.
140141
//"key3": {Value: &tracepb.AttributeValue_StringValue{StringValue: trunc("100.001", 256)}},
141-
agentLabel: {Value: &tracepb.AttributeValue_StringValue{StringValue: ua}},
142+
agentLabel: {Value: &tracepb.AttributeValue_StringValue{StringValue: trunc("custom-agent", 256)}},
142143
},
143144
},
144145
TimeEvents: &tracepb.Span_TimeEvents{

0 commit comments

Comments
 (0)