2020-03-24 13:15:01 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
2020-11-20 06:57:39 +00:00
|
|
|
grpc_errs "github.com/caos/zitadel/internal/api/grpc/errors"
|
2021-08-10 05:27:27 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-03-24 13:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Span struct {
|
2021-08-10 05:27:27 +00:00
|
|
|
span trace.Span
|
|
|
|
opts []trace.SpanEndOption
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 05:27:27 +00:00
|
|
|
func CreateSpan(span trace.Span) *Span {
|
|
|
|
return &Span{span: span, opts: []trace.SpanEndOption{}}
|
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 {
|
2021-08-10 05:27:27 +00:00
|
|
|
// trace.WithErrorStatus(codes.Error)
|
|
|
|
s.span.RecordError(err)
|
|
|
|
s.span.SetAttributes(
|
|
|
|
attribute.KeyValue{},
|
|
|
|
)
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 06:57:39 +00:00
|
|
|
code, msg, id, _ := grpc_errs.ExtractCaosError(err)
|
2021-08-10 05:27:27 +00:00
|
|
|
s.span.SetAttributes(attribute.Int("grpc_code", int(code)), attribute.String("grpc_msg", msg), attribute.String("error_id", id))
|
2020-03-24 13:15:01 +00:00
|
|
|
}
|