Skip to content

Commit

Permalink
Add support for scope attributes when translating traces & metrics re…
Browse files Browse the repository at this point in the history
…quests (#111)

## Which problem is this PR solving?
Adds support for recording scope level attributes when translating OTLP traces and metrics requests.

- Closes honeycombio/telemetry-team#322
- Unblocks #108 

## Short description of the changes
- Adds scope attributes to the resource attributes list when translating trace and metrics requests
- Add unit tests to verify the attributes are being recorded correctly.
  • Loading branch information
MikeGoldsmith authored Aug 16, 2022
1 parent b581d04 commit d7fa60f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions otlp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ func addAttributesToMap(attrs map[string]interface{}, attributes []*common.KeyVa
}
}

func addScopeToMap(attrs map[string]interface{}, scope *common.InstrumentationScope) {
if scope != nil {
if scope.Name != "" {
attrs["instrumentation_scope.name"] = scope.Name
}
if scope.Version != "" {
attrs["instrumentation_scope.version"] = scope.Version
}
if scope.Attributes != nil {
addAttributesToMap(attrs, scope.Attributes)
}
}
}

func getValue(value *common.AnyValue) interface{} {
switch value.Value.(type) {
case *common.AnyValue_StringValue:
Expand Down
1 change: 1 addition & 0 deletions otlp/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TranslateLogsRequest(request *collectorLogs.ExportLogsServiceRequest, ri Re

for _, scopeLog := range resourceLog.ScopeLogs {
scope := scopeLog.Scope
addScopeToMap(resourceAttrs, scope)

for _, log := range scopeLog.GetLogRecords() {
eventAttrs := map[string]interface{}{
Expand Down
15 changes: 15 additions & 0 deletions otlp/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func TestTranslateLogsRequest(t *testing.T) {
}},
},
ScopeLogs: []*logs.ScopeLogs{{
Scope: &common.InstrumentationScope{
Name: "instr_scope_name",
Version: "instr_scope_version",
Attributes: []*common.KeyValue{
{
Key: "scope_attr",
Value: &common.AnyValue{
Value: &common.AnyValue_StringValue{StringValue: "scope_attr_val"},
},
},
},
},
LogRecords: []*logs.LogRecord{{
TraceId: traceID,
SpanId: spanID,
Expand Down Expand Up @@ -83,6 +95,9 @@ func TestTranslateLogsRequest(t *testing.T) {
assert.Equal(t, "my-service", ev.Attributes["service.name"])
assert.Equal(t, "span_attr_val", ev.Attributes["span_attr"])
assert.Equal(t, "resource_attr_val", ev.Attributes["resource_attr"])
assert.Equal(t, "instr_scope_name", ev.Attributes["instrumentation_scope.name"])
assert.Equal(t, "instr_scope_version", ev.Attributes["instrumentation_scope.version"])
assert.Equal(t, "scope_attr_val", ev.Attributes["scope_attr"])
}

func TestTranslateClassicLogsRequest(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TranslateTraceRequest(request *collectorTrace.ExportTraceServiceRequest, ri

for _, scopeSpan := range resourceSpan.ScopeSpans {
scope := scopeSpan.Scope
addScopeToMap(resourceAttrs, scope)

for _, span := range scopeSpan.GetSpans() {
traceID := BytesToTraceID(span.TraceId)
Expand Down
21 changes: 21 additions & 0 deletions otlp/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func TestTranslateLegacyGrpcTraceRequest(t *testing.T) {
}},
},
ScopeSpans: []*trace.ScopeSpans{{
Scope: &common.InstrumentationScope{
Name: "instr_scope_name",
Version: "instr_scope_version",
Attributes: []*common.KeyValue{
{
Key: "scope_attr",
Value: &common.AnyValue{
Value: &common.AnyValue_StringValue{StringValue: "scope_attr_val"},
},
},
},
},
Spans: []*trace.Span{{
TraceId: traceID,
SpanId: spanID,
Expand Down Expand Up @@ -125,6 +137,9 @@ func TestTranslateLegacyGrpcTraceRequest(t *testing.T) {
assert.Equal(t, "resource_attr_val", ev.Attributes["resource_attr"])
assert.Equal(t, 1, ev.Attributes["span.num_links"])
assert.Equal(t, 1, ev.Attributes["span.num_events"])
assert.Equal(t, "instr_scope_name", ev.Attributes["instrumentation_scope.name"])
assert.Equal(t, "instr_scope_version", ev.Attributes["instrumentation_scope.version"])
assert.Equal(t, "scope_attr_val", ev.Attributes["scope_attr"])

// event
ev = events[1]
Expand All @@ -137,6 +152,9 @@ func TestTranslateLegacyGrpcTraceRequest(t *testing.T) {
assert.Equal(t, "span_event", ev.Attributes["meta.annotation_type"])
assert.Equal(t, "span_event_attr_val", ev.Attributes["span_event_attr"])
assert.Equal(t, "resource_attr_val", ev.Attributes["resource_attr"])
assert.Equal(t, "instr_scope_name", ev.Attributes["instrumentation_scope.name"])
assert.Equal(t, "instr_scope_version", ev.Attributes["instrumentation_scope.version"])
assert.Equal(t, "scope_attr_val", ev.Attributes["scope_attr"])

// link
ev = events[2]
Expand All @@ -151,6 +169,9 @@ func TestTranslateLegacyGrpcTraceRequest(t *testing.T) {
assert.Equal(t, "link", ev.Attributes["meta.annotation_type"])
assert.Equal(t, "span_link_attr_val", ev.Attributes["span_link_attr"])
assert.Equal(t, "resource_attr_val", ev.Attributes["resource_attr"])
assert.Equal(t, "instr_scope_name", ev.Attributes["instrumentation_scope.name"])
assert.Equal(t, "instr_scope_version", ev.Attributes["instrumentation_scope.version"])
assert.Equal(t, "scope_attr_val", ev.Attributes["scope_attr"])
}

func TestTranslateGrpcTraceRequest(t *testing.T) {
Expand Down

0 comments on commit d7fa60f

Please sign in to comment.