1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/tracing/helpers.go
Charles-Edouard Brétéché 718f80c5a1
refactor: tracing package (#5643)
* refactor: tracing package

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* child span

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2022-12-11 21:39:37 +01:00

45 lines
986 B
Go

package tracing
import (
"context"
"net/http"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
)
func SetSpanStatus(span trace.Span, err error) {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
} else {
span.SetStatus(codes.Ok, "")
}
}
func SetStatus(ctx context.Context, err error) {
SetSpanStatus(trace.SpanFromContext(ctx), err)
}
func SetHttpStatus(ctx context.Context, err error, code int) {
span := trace.SpanFromContext(ctx)
if err != nil {
span.RecordError(err)
}
span.SetAttributes(semconv.HTTPStatusCodeKey.Int(code))
if code >= 400 {
span.SetStatus(codes.Error, http.StatusText(code))
} else {
span.SetStatus(codes.Ok, http.StatusText(code))
}
}
func IsInSpan(ctx context.Context) bool {
span := trace.SpanFromContext(ctx)
return span.IsRecording()
}
func CurrentSpan(ctx context.Context) trace.Span {
return trace.SpanFromContext(ctx)
}