2020-03-24 13:15:01 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
2020-11-20 06:57:39 +00:00
|
|
|
"context"
|
2020-03-24 13:15:01 +00:00
|
|
|
|
2020-11-20 06:57:39 +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 {
|
2020-11-20 06:57:39 +00:00
|
|
|
span api_trace.Span
|
|
|
|
opts []api_trace.SpanOption
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 06:57:39 +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
|
|
|
|
}
|
2020-11-20 06:57:39 +00:00
|
|
|
|
|
|
|
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 {
|
2020-11-20 06:57:39 +00:00
|
|
|
s.span.RecordError(context.TODO(), err, api_trace.WithErrorStatus(codes.Error))
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 06:57:39 +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
|
|
|
}
|