/
util.go
44 lines (37 loc) · 1.22 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package tracing
import (
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func SetSpanErr(span trace.Span, err error) {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
type objectIdentityExtactor struct {
groupVersionKindForFn func(object runtime.Object) (schema.GroupVersionKind, error)
}
func (o *objectIdentityExtactor) handleClientObjectAttrs(span trace.Span, isSubResource bool, obj client.Object) {
objectType := "object"
if isSubResource {
objectType = "subresource"
}
if ns := obj.GetNamespace(); ns != "" {
span.SetAttributes(attribute.String("object.namespace", ns))
}
span.SetAttributes(attribute.String("object.name", obj.GetName()))
gvk, err := o.groupVersionKindForFn(obj)
if err != nil {
span.RecordError(fmt.Errorf("failed to get GVK for object: %s", err))
} else {
span.SetAttributes(
attribute.String(fmt.Sprintf("%s.group", objectType), gvk.Group),
attribute.String(fmt.Sprintf("%s.version", objectType), gvk.Version),
attribute.String(fmt.Sprintf("%s.kind", objectType), gvk.Kind),
)
}
}