Skip to content

Commit

Permalink
[pkg/otlp] Handle OTel semconv 1.17.0 in span tag conversion (#16337)
Browse files Browse the repository at this point in the history
* [pkg/otlp] Handle OTel semconv 1.17.0 in span tag conversion

* Fix license

* Add more test
  • Loading branch information
songy23 authored Mar 29, 2023
1 parent 70396ec commit 47e3e53
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,7 @@ core,go.opentelemetry.io/collector/receiver/otlpreceiver/internal/logs,Apache-2.
core,go.opentelemetry.io/collector/receiver/otlpreceiver/internal/metrics,Apache-2.0,Copyright The OpenTelemetry Authors
core,go.opentelemetry.io/collector/receiver/otlpreceiver/internal/trace,Apache-2.0,Copyright The OpenTelemetry Authors
core,go.opentelemetry.io/collector/receiver/scrapererror,Apache-2.0,Copyright The OpenTelemetry Authors
core,go.opentelemetry.io/collector/semconv/v1.17.0,Apache-2.0,Copyright The OpenTelemetry Authors
core,go.opentelemetry.io/collector/semconv/v1.5.0,Apache-2.0,Copyright The OpenTelemetry Authors
core,go.opentelemetry.io/collector/semconv/v1.6.1,Apache-2.0,Copyright The OpenTelemetry Authors
core,go.opentelemetry.io/collector/service,Apache-2.0,Copyright The OpenTelemetry Authors
Expand Down
32 changes: 17 additions & 15 deletions pkg/trace/api/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
semconv117 "go.opentelemetry.io/collector/semconv/v1.17.0"
semconv "go.opentelemetry.io/collector/semconv/v1.6.1"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -196,10 +197,7 @@ func (o *OTLPReceiver) ReceiveResourceSpans(ctx context.Context, rspans ptrace.R
if lang == "" {
lang = fastHeaderGet(httpHeader, header.Lang)
}
containerID := rattr[string(semconv.AttributeContainerID)]
if containerID == "" {
containerID = rattr[string(semconv.AttributeK8SPodUID)]
}
containerID := getFirstFromMap(rattr, semconv.AttributeContainerID, semconv.AttributeK8SPodUID)
if containerID == "" {
containerID = o.cidProvider.GetContainerID(ctx, httpHeader)
}
Expand Down Expand Up @@ -241,12 +239,7 @@ func (o *OTLPReceiver) ReceiveResourceSpans(ctx context.Context, rspans ptrace.R
}
if containerID == "" {
// no cid at resource level, grab what we can
if v := ddspan.Meta[string(semconv.AttributeK8SPodUID)]; v != "" {
containerID = v
}
if v := ddspan.Meta[string(semconv.AttributeContainerID)]; v != "" {
containerID = v
}
containerID = getFirstFromMap(ddspan.Meta, semconv.AttributeContainerID, semconv.AttributeK8SPodUID)
}
if p, ok := ddspan.Metrics["_sampling_priority_v1"]; ok {
priorityByID[traceID] = sampler.SamplingPriority(p)
Expand Down Expand Up @@ -585,29 +578,38 @@ func (o *OTLPReceiver) convertSpan(rattr map[string]string, lib pcommon.Instrume
func resourceFromTags(meta map[string]string) string {
if m := meta[string(semconv.AttributeHTTPMethod)]; m != "" {
// use the HTTP method + route (if available)
if route := meta[string(semconv.AttributeHTTPRoute)]; route != "" {
return m + " " + route
} else if route := meta["grpc.path"]; route != "" {
if route := getFirstFromMap(meta, semconv.AttributeHTTPRoute, "grpc.path"); route != "" {
return m + " " + route
}
return m
} else if m := meta[string(semconv.AttributeMessagingOperation)]; m != "" {
// use the messaging operation
if dest := meta[string(semconv.AttributeMessagingDestination)]; dest != "" {
if dest := getFirstFromMap(meta, semconv.AttributeMessagingDestination, semconv117.AttributeMessagingDestinationName); dest != "" {
return m + " " + dest
}
return m
} else if m := meta[string(semconv.AttributeRPCMethod)]; m != "" {
// use the RPC method
if svc := meta[string(semconv.AttributeRPCService)]; svc != "" {
// ...and service if availabl
// ...and service if available
return m + " " + svc
}
return m
}
return ""
}

// getFirstFromMap checks each key in the given keys in the map and returns the first value whose
// key matches, or an empty string if none matches.
func getFirstFromMap(m map[string]string, keys ...string) string {
for _, key := range keys {
if val := m[key]; val != "" {
return val
}
}
return ""
}

// status2Error checks the given status and events and applies any potential error and messages
// to the given span attributes.
func status2Error(status ptrace.Status, events ptrace.SpanEventSlice, span *pb.Span) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/trace/api/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,14 @@ func TestOTLPHelpers(t *testing.T) {
meta: map[string]string{"messaging.operation": "DO", "messaging.destination": "OP"},
out: "DO OP",
},
{
meta: map[string]string{"messaging.operation": "DO", "messaging.destination.name": "OP"},
out: "DO OP",
},
{
meta: map[string]string{"messaging.operation": "process", "messaging.destination.name": "Queue1", "messaging.destination": "Queue2"},
out: "process Queue2",
},
{
meta: map[string]string{semconv.AttributeRPCService: "SVC", semconv.AttributeRPCMethod: "M"},
out: "M SVC",
Expand Down
11 changes: 11 additions & 0 deletions releasenotes/notes/otlp-semconv-1.17-88e28a30a0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
enhancements:
- |
Support OTel semconv 1.17.0 in OTLP ingest endpoint.

0 comments on commit 47e3e53

Please sign in to comment.