Skip to content

Commit

Permalink
No otel
Browse files Browse the repository at this point in the history
Signed-off-by: Nghia Tran <[email protected]>
  • Loading branch information
tcnghia committed Mar 22, 2024
1 parent 49358ec commit ba8a371
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
31 changes: 19 additions & 12 deletions gcp/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"sync"
"time"

"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

func insideTest() bool {
Expand Down Expand Up @@ -95,20 +95,24 @@ func extractProjectID() string {
return projectID
}

func gcpTraceUri(projectID, traceHeader string) string {
traceParts := strings.Split(traceHeader, "/")
if len(traceParts) > 0 && len(traceParts[0]) > 0 {
return fmt.Sprintf("projects/%s/traces/%s", projectID, traceParts[0])
}
return ""
}

// WithCloudTraceContext returns an http.handler that adds the GCP Cloud Trace
// ID to the context. This is used to correlate the structured logs with the
// request log.
func WithCloudTraceContext(h http.Handler) http.Handler {
projectID := extractProjectID()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if projectID != "" {
var trace string
traceHeader := r.Header.Get("X-Cloud-Trace-Context")
traceParts := strings.Split(traceHeader, "/")
if len(traceParts) > 0 && len(traceParts[0]) > 0 {
trace = fmt.Sprintf("projects/%s/traces/%s", projectID, traceParts[0])
if trace := gcpTraceUri(projectID, r.Header.Get("X-Cloud-Trace-Context")); trace != "" {
r = r.WithContext(context.WithValue(r.Context(), "trace", trace))
}
r = r.WithContext(context.WithValue(r.Context(), "trace", trace))
}
h.ServeHTTP(w, r)
})
Expand All @@ -120,11 +124,14 @@ func WithCloudTraceContext(h http.Handler) http.Handler {
func CloudTraceContextUnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
projectID := extractProjectID()
if projectID != "" {
// extract the trace header from the incoming request
span := trace.SpanFromContext(ctx)
if traceId := span.SpanContext().TraceID().String(); traceId != "" {
trace := fmt.Sprintf("projects/%s/traces/%s", projectID, traceId)
ctx = context.WithValue(ctx, "trace", trace)
if md, ok := metadata.FromIncomingContext(ctx); ok {
traceHeader := ""
if x := md.Get("x-cloud-trace-context"); len(x) > 0 {
traceHeader = x[0]
}
if trace := gcpTraceUri(projectID, traceHeader); trace != "" {
ctx = context.WithValue(ctx, "trace", trace)
}
}
}
return handler(ctx, req)
Expand Down
6 changes: 3 additions & 3 deletions gcp/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/chainguard-dev/clog"
"golang.org/x/net/trace"
"google.golang.org/grpc/metadata"
)

func TestTrace(t *testing.T) {
Expand Down Expand Up @@ -72,8 +72,8 @@ func TestTrace(t *testing.T) {

// Set up a server that logs a message with trace context added.
slog.SetDefault(slog.New(NewHandler(slog.LevelDebug)))
ctx := trace.NewContext(context.Background(), trace.New("family", "title"))

md := metadata.New(map[string]string{"x-cloud-trace-context": "trace/id/yay"})
ctx := metadata.NewIncomingContext(context.Background(), md)
_, _ = CloudTraceContextUnaryInterceptor(ctx, nil, nil, func(ctx context.Context, req any) (any, error) {
clog.InfoContext(ctx, "hello world")
if found := ctx.Value("trace") != nil; found != c.wantTrace {
Expand Down
9 changes: 1 addition & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ module github.com/chainguard-dev/clog

go 1.21.2

require (
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
google.golang.org/grpc v1.62.1
)
require google.golang.org/grpc v1.62.1

require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 h1:AjyfHzEPEFp/NpvfN5g+KDla3EMojjhRVZc1i7cj+oM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

0 comments on commit ba8a371

Please sign in to comment.