zitadel/internal/telemetry/tracing/span.go

45 lines
916 B
Go
Raw Normal View History

2020-03-24 13:15:01 +00:00
package tracing
import (
"context"
2020-03-24 13:15:01 +00:00
grpc_errs "github.com/caos/zitadel/internal/api/grpc/errors"
api_trace "go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
2020-03-24 13:15:01 +00:00
)
type Span struct {
span api_trace.Span
opts []api_trace.SpanOption
2020-03-24 13:15:01 +00:00
}
func CreateSpan(span api_trace.Span) *Span {
return &Span{span: span, opts: []api_trace.SpanOption{}}
2020-03-24 13:15:01 +00:00
}
func (s *Span) End() {
if s.span == nil {
return
}
s.span.End(s.opts...)
2020-03-24 13:15:01 +00:00
}
func (s *Span) EndWithError(err error) {
s.SetStatusByError(err)
s.End()
}
func (s *Span) SetStatusByError(err error) {
if s.span == nil {
return
}
if err != nil {
s.span.RecordError(context.TODO(), err, api_trace.WithErrorStatus(codes.Error))
2020-03-24 13:15:01 +00:00
}
code, msg, id, _ := grpc_errs.ExtractCaosError(err)
s.span.SetAttributes(label.Uint32("grpc_code", uint32(code)), label.String("grpc_msg", msg), label.String("error_id", id))
2020-03-24 13:15:01 +00:00
}