Skip to content

Commit 4d2a324

Browse files
committed
update to go.opentelemetry.io/otel/semconv/v1.21.0, remove "httpconv" uses
This commit switches our code to use semconv 1.21, which is the version matching the OTEL modules, as well as the containerd code. The BuildKit 0.12.x module currently uses an older version of the OTEL modules, and uses the semconv 0.17 schema. Mixing schema-versions is problematic, but we still want to consume BuildKit's "detect" package to wire-up other parts of OTEL. To align the versions in our code, this patch sets the BuildKit detect.Resource with the correct semconv version. It's worth noting that the BuildKit package has a custom "serviceNameDetector"; https://github.com/moby/buildkit/blob/v0.12.4/util/tracing/detect/detect.go#L153-L169 Whith is merged with OTEL's default resource: https://github.com/moby/buildkit/blob/v0.12.4/util/tracing/detect/detect.go#L100-L107 There's no need to duplicate that code, as OTEL's `resource.Default()` already provides this functionality: - It uses fromEnv{} detector internally: https://github.com/open-telemetry/opentelemetry-go/blob/v1.19.0/sdk/resource/resource.go#L208 - fromEnv{} detector reads OTEL_SERVICE_NAME: https://github.com/open-telemetry/opentelemetry-go/blob/v1.19.0/sdk/resource/env.go#L53 This patch also removes uses of the httpconv package, which is no longer included in semconv 1.21 and now an internal package. Removing the use of this package means that hijacked connections will not have the HTTP attributes on the Moby client span, which isn't ideal, but a limited loss that'd impact exec/attach. The span itself will still exist, it just won't the additional attributes that are added by that package. Alternatively, the httpconv call COULD remain - it will not error and will send syntactically valid spans but we would be mixing & matching semconv versions, so won't be compliant. Some parts of the httpconv package were preserved through a very minimal local implementation; a variant of `httpconv.ClientStatus(resp.StatusCode))` is added to set the span status (`span.SetStatus()`). The `httpconv` package has complex logic for this, but mostly drills down to HTTP status range (1xx/2xx/3xx/4xx/5xx) to determine if the status was successfull or non-successful (4xx/5xx). The additional logic it provided was to validate actual status-codes, and to convert "bogus" status codes in "success" ranges (1xx, 2xx) into an error. That code seemed over-reaching (and not accounting for potential future _valid_ status codes). Let's assume we only get valid status codes. - https://github.com/open-telemetry/opentelemetry-go/blob/v1.21.0/semconv/v1.17.0/httpconv/http.go#L85-L89 - https://github.com/open-telemetry/opentelemetry-go/blob/v1.21.0/semconv/internal/v2/http.go#L322-L330 - https://github.com/open-telemetry/opentelemetry-go/blob/v1.21.0/semconv/internal/v2/http.go#L356-L404 Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 7d991b6 commit 4d2a324

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

client/hijack.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"go.opentelemetry.io/otel"
1717
"go.opentelemetry.io/otel/codes"
1818
"go.opentelemetry.io/otel/propagation"
19-
"go.opentelemetry.io/otel/semconv/v1.17.0/httpconv"
2019
"go.opentelemetry.io/otel/trace"
2120
)
2221

@@ -66,7 +65,8 @@ func (cli *Client) setupHijackConn(req *http.Request, proto string) (_ net.Conn,
6665
}
6766

6867
ctx, span := tp.Tracer("").Start(ctx, req.Method+" "+req.URL.Path, trace.WithSpanKind(trace.SpanKindClient))
69-
span.SetAttributes(httpconv.ClientRequest(req)...)
68+
// FIXME(thaJeztah): httpconv.ClientRequest is now an internal package; replace this with alternative for semconv v1.21
69+
// span.SetAttributes(httpconv.ClientRequest(req)...)
7070
defer func() {
7171
if retErr != nil {
7272
span.RecordError(retErr)
@@ -98,7 +98,27 @@ func (cli *Client) setupHijackConn(req *http.Request, proto string) (_ net.Conn,
9898
// Server hijacks the connection, error 'connection closed' expected
9999
resp, err := clientconn.Do(req)
100100
if resp != nil {
101-
span.SetStatus(httpconv.ClientStatus(resp.StatusCode))
101+
// This is a simplified variant of "httpconv.ClientStatus(resp.StatusCode))";
102+
//
103+
// The main purpose of httpconv.ClientStatus() is to detect whether the
104+
// status was successful (1xx, 2xx, 3xx) or non-successful (4xx/5xx).
105+
//
106+
// It also provides complex logic to *validate* status-codes against
107+
// a hard-coded list meant to exclude "bogus" status codes in "success"
108+
// ranges (1xx, 2xx) and convert them into an error status. That code
109+
// seemed over-reaching (and not accounting for potential future valid
110+
// status codes). We assume we only get valid status codes, and only
111+
// look at status-code ranges.
112+
//
113+
// For reference, see:
114+
// https://github.com/open-telemetry/opentelemetry-go/blob/v1.21.0/semconv/v1.17.0/httpconv/http.go#L85-L89
115+
// https://github.com/open-telemetry/opentelemetry-go/blob/v1.21.0/semconv/internal/v2/http.go#L322-L330
116+
// https://github.com/open-telemetry/opentelemetry-go/blob/v1.21.0/semconv/internal/v2/http.go#L356-L404
117+
code := codes.Unset
118+
if resp.StatusCode >= http.StatusBadRequest {
119+
code = codes.Error
120+
}
121+
span.SetStatus(code, "")
102122
}
103123

104124
//nolint:staticcheck // ignore SA1019 for connecting to old (pre go1.8) daemons

cmd/dockerd/daemon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import (
6363
"github.com/spf13/pflag"
6464
"go.opentelemetry.io/otel"
6565
"go.opentelemetry.io/otel/propagation"
66+
"go.opentelemetry.io/otel/sdk/resource"
6667
"tags.cncf.io/container-device-interface/pkg/cdi"
6768
)
6869

@@ -238,6 +239,10 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
238239

239240
setOTLPProtoDefault()
240241
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
242+
243+
// Override BuildKit's default Resource so that it matches the semconv
244+
// version that is used in our code.
245+
detect.Resource = resource.Default()
241246
detect.Recorder = detect.NewTraceRecorder()
242247

243248
tp, err := detect.TracerProvider()

testutil/helpers.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"go.opentelemetry.io/otel/propagation"
1717
"go.opentelemetry.io/otel/sdk/resource"
1818
"go.opentelemetry.io/otel/sdk/trace"
19-
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
19+
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
2020
"gotest.tools/v3/icmd"
2121
)
2222

@@ -34,7 +34,7 @@ func (d devZero) Read(p []byte) (n int, err error) {
3434

3535
var tracingOnce sync.Once
3636

37-
// configureTracing sets up an OTLP tracing exporter for use in tests.
37+
// ConfigureTracing sets up an OTLP tracing exporter for use in tests.
3838
func ConfigureTracing() func(context.Context) {
3939
if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" {
4040
// No OTLP endpoint configured, so don't bother setting up tracing.
@@ -52,9 +52,7 @@ func ConfigureTracing() func(context.Context) {
5252
tp = trace.NewTracerProvider(
5353
trace.WithSpanProcessor(sp),
5454
trace.WithSampler(trace.AlwaysSample()),
55-
trace.WithResource(resource.NewSchemaless(
56-
attribute.KeyValue{Key: semconv.ServiceNameKey, Value: attribute.StringValue("integration-test-client")},
57-
)),
55+
trace.WithResource(resource.NewSchemaless(semconv.ServiceName("integration-test-client"))),
5856
)
5957
otel.SetTracerProvider(tp)
6058

0 commit comments

Comments
 (0)