2020-03-24 14:15:01 +01:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
2021-08-10 07:27:27 +02:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-04-28 17:35:56 +02:00
|
|
|
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/gerrors"
|
2020-03-24 14:15:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Span struct {
|
2021-08-10 07:27:27 +02:00
|
|
|
span trace.Span
|
|
|
|
opts []trace.SpanEndOption
|
2020-03-24 14:15:01 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 07:27:27 +02:00
|
|
|
func CreateSpan(span trace.Span) *Span {
|
|
|
|
return &Span{span: span, opts: []trace.SpanEndOption{}}
|
2020-03-24 14:15:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Span) End() {
|
|
|
|
if s.span == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-20 07:57:39 +01:00
|
|
|
|
|
|
|
s.span.End(s.opts...)
|
2020-03-24 14:15:01 +01: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 07:27:27 +02:00
|
|
|
// trace.WithErrorStatus(codes.Error)
|
|
|
|
s.span.RecordError(err)
|
|
|
|
s.span.SetAttributes(
|
|
|
|
attribute.KeyValue{},
|
|
|
|
)
|
2020-03-24 14:15:01 +01:00
|
|
|
}
|
|
|
|
|
2023-12-08 16:30:55 +02:00
|
|
|
code, msg, id, _ := gerrors.ExtractZITADELError(err)
|
2021-08-10 07:27:27 +02:00
|
|
|
s.span.SetAttributes(attribute.Int("grpc_code", int(code)), attribute.String("grpc_msg", msg), attribute.String("error_id", id))
|
2020-03-24 14:15:01 +01:00
|
|
|
}
|