Skip to content

Commit b9c3aea

Browse files
authored
otelgrpc: support OTEL_SEMCONV_STABILITY_OPT_IN (#8726)
Signed-off-by: David Ashpole <dashpole@google.com>
1 parent c191508 commit b9c3aea

File tree

7 files changed

+610
-146
lines changed

7 files changed

+610
-146
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1515
- Added support for `attribute_count_limit` and `attribute_value_length_limit` in tracer provider configuration in `go.opentelemetry.io/contrib/otelconf`. (#8687)
1616
- Added support for `attribute_count_limit` and `attribute_value_length_limit` in logger provider configuration in `go.opentelemetry.io/contrib/otelconf`. (#8686)
1717
- Added support for `server.address` and `server.port` attributes in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#8723)
18+
- Support `OTEL_SEMCONV_STABILITY_OPT_IN` in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. Supported values are `rpc` (default), `rpc/dup` and `rpc/old`. (#8726)
1819

1920
### Changed
2021

instrumentation/google.golang.org/grpc/otelgrpc/config.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package otelgrpc // import "go.opentelemetry.io/contrib/instrumentation/google.g
55

66
import (
77
"context"
8+
"os"
9+
"strings"
810

911
"go.opentelemetry.io/otel"
1012
"go.opentelemetry.io/otel/attribute"
@@ -29,6 +31,14 @@ type InterceptorFilter func(*InterceptorInfo) bool
2931
// A Filter must return true if the request should be instrumented.
3032
type Filter func(*stats.RPCTagInfo) bool
3133

34+
type semconvMode int
35+
36+
const (
37+
semconvModeNew semconvMode = iota // Default
38+
semconvModeOld
39+
semconvModeDup
40+
)
41+
3242
// config is a group of options for this instrumentation.
3343
type config struct {
3444
Filter Filter
@@ -47,6 +57,8 @@ type config struct {
4757

4858
ReceivedEvent bool
4959
SentEvent bool
60+
61+
semconvMode semconvMode
5062
}
5163

5264
// Option applies an option value for a config.
@@ -66,13 +78,33 @@ func newConfig(opts []Option) *config {
6678
Propagators: otel.GetTextMapPropagator(),
6779
TracerProvider: otel.GetTracerProvider(),
6880
MeterProvider: otel.GetMeterProvider(),
81+
semconvMode: parseSemconvMode(),
6982
}
7083
for _, o := range opts {
7184
o.apply(c)
7285
}
86+
7387
return c
7488
}
7589

90+
func parseSemconvMode() semconvMode {
91+
val := os.Getenv("OTEL_SEMCONV_STABILITY_OPT_IN")
92+
if val == "" {
93+
return semconvModeNew
94+
}
95+
parts := strings.SplitSeq(val, ",")
96+
for p := range parts {
97+
p = strings.TrimSpace(p)
98+
if p == "rpc/dup" {
99+
return semconvModeDup
100+
}
101+
if p == "rpc/old" {
102+
return semconvModeOld
103+
}
104+
}
105+
return semconvModeNew
106+
}
107+
76108
// WithPublicEndpoint configures the Handler to link the span with an incoming
77109
// span context. If this option is not provided, then the association is a child
78110
// association instead of a link.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package otelgrpc
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestParseSemconvMode(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
val string
16+
want semconvMode
17+
}{
18+
{
19+
name: "empty",
20+
val: "",
21+
want: semconvModeNew,
22+
},
23+
{
24+
name: "old",
25+
val: "rpc/old",
26+
want: semconvModeOld,
27+
},
28+
{
29+
name: "dup",
30+
val: "rpc/dup",
31+
want: semconvModeDup,
32+
},
33+
{
34+
name: "unknown",
35+
val: "unknown",
36+
want: semconvModeNew,
37+
},
38+
{
39+
name: "multiple with valid",
40+
val: "foo, rpc/dup",
41+
want: semconvModeDup,
42+
},
43+
{
44+
name: "multiple with old first",
45+
val: "rpc/old, rpc/dup",
46+
want: semconvModeOld,
47+
},
48+
{
49+
name: "spaces",
50+
val: " rpc/old ",
51+
want: semconvModeOld,
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
t.Setenv("OTEL_SEMCONV_STABILITY_OPT_IN", tt.val)
58+
got := parseSemconvMode()
59+
assert.Equal(t, tt.want, got)
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)