Debian Codegraph @

event/otel/trace.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package otel
6
7 import (
8 "context"
9
10 "go.opentelemetry.io/otel/trace"
11 "golang.org/x/exp/event"
12 )
13
14 type TraceHandler struct {
15 tracer trace.Tracer
16 }
17
18 func NewTraceHandler(t trace.Tracer) *TraceHandler {
19 return &TraceHandler{tracer: t}
20 }
21
22 type spanKey struct{}
23
24 func (t *TraceHandler) Event(ctx context.Context, ev *event.Event) context.Context {
25 switch ev.Kind {
26 case event.StartKind:
27 name, opts := labelsToSpanStartOptions(ev.Labels)
28 octx, span := t.tracer.Start(ctx, name, opts...)
29 return context.WithValue(octx, spanKey{}, span)
30 case event.EndKind:
31 span, ok := ctx.Value(spanKey{}).(trace.Span)
32 if !ok {
33 panic("End called on context with no span")
34 }
35 span.End()
36 return ctx
37 default:
38 return ctx
39 }
40 }
41
42 func labelsToSpanStartOptions(ls []event.Label) (string, []trace.SpanStartOption) {
43 var opts []trace.SpanStartOption
44 var name string
45 for _, l := range ls {
46 switch l.Name {
47 case "link":
48 opts = append(opts, trace.WithLinks(l.Interface().(trace.Link)))
49 case "newRoot":
50 opts = append(opts, trace.WithNewRoot())
51 case "spanKind":
52 opts = append(opts, trace.WithSpanKind(l.Interface().(trace.SpanKind)))
53 case "name":
54 name = l.String()
55 }
56 }
57 return name, opts
58 }