blob: 14f853319e5b9756e03a7c4c7f1c6f606dfc3135 [file] [log] [blame]
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package otel
import (
type TraceHandler struct {
tracer trace.Tracer
func NewTraceHandler(t trace.Tracer) *TraceHandler {
return &TraceHandler{tracer: t}
type spanKey struct{}
func (t *TraceHandler) Log(ctx context.Context, ev *event.Event) {}
func (t *TraceHandler) Annotate(ctx context.Context, ev *event.Event) {}
func (t *TraceHandler) Metric(ctx context.Context, ev *event.Event) {}
func (t *TraceHandler) Start(ctx context.Context, ev *event.Event) context.Context {
opts := labelsToSpanOptions(ev.Labels)
octx, span := t.tracer.Start(ctx, ev.Name, opts...)
return context.WithValue(octx, spanKey{}, span)
func (t *TraceHandler) End(ctx context.Context, e *event.Event) {
span, ok := ctx.Value(spanKey{}).(trace.Span)
if !ok {
panic("End called on context with no span")
func labelsToSpanOptions(ls []event.Label) []trace.SpanOption {
var opts []trace.SpanOption
for _, l := range ls {
switch l.Name {
case "link":
opts = append(opts, trace.WithLinks(l.Value.Interface().(trace.Link)))
case "newRoot":
opts = append(opts, trace.WithNewRoot())
case "spanKind":
opts = append(opts, trace.WithSpanKind(l.Value.Interface().(trace.SpanKind)))
return opts