From 01bef622c9ee5aa28f62af9d99ed170a03bb98a5 Mon Sep 17 00:00:00 2001 From: muicoder Date: Mon, 14 Oct 2024 11:09:31 +0800 Subject: [PATCH] tracing --- go.mod | 4 ++-- integration/client/go.mod | 2 +- reference/docker/reference_deprecated.go | 4 ++-- remotes/docker/resolver.go | 11 ++++++++--- services/server/server.go | 3 ++- tracing/tracing.go | 22 +++++----------------- 6 files changed, 20 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 5a4aa880122d7..968c6741f3c23 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/containernetworking/plugins v1.2.0 github.com/coreos/go-systemd/v22 v22.5.0 github.com/davecgh/go-spew v1.1.1 - github.com/distribution/reference v0.6.0 + github.com/distribution/reference v0.5.0 github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c github.com/docker/go-metrics v0.0.1 github.com/docker/go-units v0.5.0 @@ -63,7 +63,7 @@ require ( github.com/urfave/cli v1.22.12 github.com/vishvananda/netlink v1.2.1-beta.2 go.etcd.io/bbolt v1.3.10 - go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.45.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 go.opentelemetry.io/otel v1.21.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 diff --git a/integration/client/go.mod b/integration/client/go.mod index c3c2a0b0911bd..356eed6993782 100644 --- a/integration/client/go.mod +++ b/integration/client/go.mod @@ -36,7 +36,7 @@ require ( github.com/containerd/fifo v1.1.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/distribution/reference v0.6.0 // indirect + github.com/distribution/reference v0.5.0 // indirect github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect github.com/docker/go-units v0.5.0 // indirect github.com/felixge/httpsnoop v1.0.3 // indirect diff --git a/reference/docker/reference_deprecated.go b/reference/docker/reference_deprecated.go index 409ba0f282e56..0262a40c56351 100644 --- a/reference/docker/reference_deprecated.go +++ b/reference/docker/reference_deprecated.go @@ -27,8 +27,8 @@ import ( const ( // NameTotalLengthMax is the maximum total number of characters in a repository name. // - // Deprecated: use [reference.RepositoryNameTotalLengthMax]. - NameTotalLengthMax = reference.RepositoryNameTotalLengthMax + // Deprecated: use [reference.NameTotalLengthMax]. + NameTotalLengthMax = reference.NameTotalLengthMax ) var ( diff --git a/remotes/docker/resolver.go b/remotes/docker/resolver.go index 4ca2b921ebc4b..fe8a4399b102b 100644 --- a/remotes/docker/resolver.go +++ b/remotes/docker/resolver.go @@ -586,13 +586,18 @@ func (r *request) do(ctx context.Context) (*http.Response, error) { return nil } } - - tracing.UpdateHTTPClient(client, tracing.Name("remotes.docker.resolver", "HTTPRequest")) - + _, httpSpan := tracing.StartSpan( + ctx, + tracing.Name("remotes.docker.resolver", "HTTPRequest"), + tracing.WithHTTPRequest(req), + ) + defer httpSpan.End() resp, err := client.Do(req) if err != nil { + httpSpan.SetStatus(err) return nil, fmt.Errorf("failed to do request: %w", err) } + httpSpan.SetAttributes(tracing.HTTPStatusCodeAttributes(resp.StatusCode)...) log.G(ctx).WithFields(responseFields(resp)).Debug("fetch response received") return resp, nil } diff --git a/services/server/server.go b/services/server/server.go index 4285da84e1ae5..484ce11f151c0 100644 --- a/services/server/server.go +++ b/services/server/server.go @@ -135,12 +135,13 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) { } serverOpts := []grpc.ServerOption{ - grpc.StatsHandler(otelgrpc.NewServerHandler()), grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( + otelgrpc.StreamServerInterceptor(), grpc_prometheus.StreamServerInterceptor, streamNamespaceInterceptor, )), grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( + otelgrpc.UnaryServerInterceptor(), grpc_prometheus.UnaryServerInterceptor, unaryNamespaceInterceptor, )), diff --git a/tracing/tracing.go b/tracing/tracing.go index 80d2b95c0e15b..7fe7bfd5bf228 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -20,11 +20,11 @@ import ( "context" "net/http" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" - semconv "go.opentelemetry.io/otel/semconv/v1.21.0" + semconv "go.opentelemetry.io/otel/semconv/v1.17.0" + httpconv "go.opentelemetry.io/otel/semconv/v1.17.0/httpconv" "go.opentelemetry.io/otel/trace" ) @@ -37,27 +37,15 @@ type SpanOpt func(config *StartConfig) // WithHTTPRequest marks span as a HTTP request operation from client to server. // It'll append attributes from the HTTP request object and mark it with `SpanKindClient` type. -// -// Deprecated: use upstream functionality from otelhttp directly instead. This function is kept for API compatibility -// but no longer works as expected due to required functionality no longer exported in OpenTelemetry libraries. -func WithHTTPRequest(_ *http.Request) SpanOpt { +func WithHTTPRequest(request *http.Request) SpanOpt { return func(config *StartConfig) { config.spanOpts = append(config.spanOpts, - trace.WithSpanKind(trace.SpanKindClient), // A client making a request to a server + trace.WithSpanKind(trace.SpanKindClient), // A client making a request to a server + trace.WithAttributes(httpconv.ClientRequest(request)...), // Add HTTP attributes ) } } -// UpdateHTTPClient updates the http client with the necessary otel transport -func UpdateHTTPClient(client *http.Client, name string) { - client.Transport = otelhttp.NewTransport( - client.Transport, - otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string { - return name - }), - ) -} - // StartSpan starts child span in a context. func StartSpan(ctx context.Context, opName string, opts ...SpanOpt) (context.Context, *Span) { config := StartConfig{}