mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-31 13:04:23 +01:00
runtime: tracing: Change method for adding tags
In later versions of OpenTelemetry label.Any() is deprecated. Create addTag() to handle type assertions of values. Change AddTag() to variadic function that accepts multiple keys and values. Fixes #2547 Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
This commit is contained in:
@@ -112,7 +112,7 @@ func SetEphemeralStorageType(ociSpec specs.Spec) specs.Spec {
|
||||
func CreateSandbox(ctx context.Context, vci vc.VC, ociSpec specs.Spec, runtimeConfig oci.RuntimeConfig, rootFs vc.RootFs,
|
||||
containerID, bundlePath, console string, disableOutput, systemdCgroup bool) (_ vc.VCSandbox, _ vc.Process, err error) {
|
||||
span, ctx := katatrace.Trace(ctx, nil, "CreateSandbox", createTracingTags)
|
||||
katatrace.AddTag(span, "container_id", containerID)
|
||||
katatrace.AddTags(span, "container_id", containerID)
|
||||
defer span.End()
|
||||
|
||||
sandboxConfig, err := oci.SandboxConfig(ociSpec, runtimeConfig, bundlePath, containerID, console, disableOutput, systemdCgroup)
|
||||
@@ -167,7 +167,7 @@ func CreateSandbox(ctx context.Context, vci vc.VC, ociSpec specs.Spec, runtimeCo
|
||||
|
||||
sid := sandbox.ID()
|
||||
kataUtilsLogger = kataUtilsLogger.WithField("sandbox", sid)
|
||||
katatrace.AddTag(span, "sandbox_id", sid)
|
||||
katatrace.AddTags(span, "sandbox_id", sid)
|
||||
|
||||
containers := sandbox.GetAllContainers()
|
||||
if len(containers) != 1 {
|
||||
@@ -211,7 +211,7 @@ func CreateContainer(ctx context.Context, sandbox vc.VCSandbox, ociSpec specs.Sp
|
||||
var c vc.VCContainer
|
||||
|
||||
span, ctx := katatrace.Trace(ctx, nil, "CreateContainer", createTracingTags)
|
||||
katatrace.AddTag(span, "container_id", containerID)
|
||||
katatrace.AddTags(span, "container_id", containerID)
|
||||
defer span.End()
|
||||
|
||||
ociSpec = SetEphemeralStorageType(ociSpec)
|
||||
@@ -237,7 +237,7 @@ func CreateContainer(ctx context.Context, sandbox vc.VCSandbox, ociSpec specs.Sp
|
||||
return vc.Process{}, err
|
||||
}
|
||||
|
||||
katatrace.AddTag(span, "sandbox_id", sandboxID)
|
||||
katatrace.AddTags(span, "sandbox_id", sandboxID)
|
||||
|
||||
c, err = sandbox.CreateContainer(ctx, contConfig)
|
||||
if err != nil {
|
||||
|
||||
@@ -35,8 +35,7 @@ func hookLogger() *logrus.Entry {
|
||||
func runHook(ctx context.Context, hook specs.Hook, cid, bundlePath string) error {
|
||||
span, _ := katatrace.Trace(ctx, hookLogger(), "runHook", hookTracingTags)
|
||||
defer span.End()
|
||||
katatrace.AddTag(span, "path", hook.Path)
|
||||
katatrace.AddTag(span, "args", hook.Args)
|
||||
katatrace.AddTags(span, "path", hook.Path, "args", hook.Args)
|
||||
|
||||
state := specs.State{
|
||||
Pid: syscall.Gettid(),
|
||||
@@ -93,7 +92,7 @@ func runHook(ctx context.Context, hook specs.Hook, cid, bundlePath string) error
|
||||
|
||||
func runHooks(ctx context.Context, hooks []specs.Hook, cid, bundlePath, hookType string) error {
|
||||
span, ctx := katatrace.Trace(ctx, hookLogger(), "runHooks", hookTracingTags)
|
||||
katatrace.AddTag(span, "type", hookType)
|
||||
katatrace.AddTags(span, "type", hookType)
|
||||
defer span.End()
|
||||
|
||||
for _, hook := range hooks {
|
||||
|
||||
@@ -7,6 +7,7 @@ package katatrace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.opentelemetry.io/otel"
|
||||
@@ -163,8 +164,77 @@ func Trace(parent context.Context, logger *logrus.Entry, name string, tags ...ma
|
||||
return span, ctx
|
||||
}
|
||||
|
||||
// AddTag adds an additional key-value pair to a tracing span. This can be used to
|
||||
// provide dynamic tags that are determined at runtime.
|
||||
func AddTag(span otelTrace.Span, key string, value interface{}) {
|
||||
span.SetAttributes(label.Any(key, value))
|
||||
func addTag(span otelTrace.Span, key string, value interface{}) {
|
||||
// do not append tags if tracing is disabled
|
||||
if !tracing {
|
||||
return
|
||||
}
|
||||
if value == nil {
|
||||
span.SetAttributes(label.String(key, "nil"))
|
||||
return
|
||||
}
|
||||
|
||||
switch value := value.(type) {
|
||||
case string:
|
||||
span.SetAttributes(label.String(key, value))
|
||||
case bool:
|
||||
span.SetAttributes(label.Bool(key, value))
|
||||
case int:
|
||||
span.SetAttributes(label.Int(key, value))
|
||||
case int8:
|
||||
span.SetAttributes(label.Int(key, int(value)))
|
||||
case int16:
|
||||
span.SetAttributes(label.Int(key, int(value)))
|
||||
case int32:
|
||||
span.SetAttributes(label.Int32(key, value))
|
||||
case int64:
|
||||
span.SetAttributes(label.Int64(key, value))
|
||||
case uint:
|
||||
span.SetAttributes(label.Uint(key, value))
|
||||
case uint8:
|
||||
span.SetAttributes(label.Uint(key, uint(value)))
|
||||
case uint16:
|
||||
span.SetAttributes(label.Uint(key, uint(value)))
|
||||
case uint32:
|
||||
span.SetAttributes(label.Uint32(key, value))
|
||||
case uint64:
|
||||
span.SetAttributes(label.Uint64(key, value))
|
||||
case float32:
|
||||
span.SetAttributes(label.Float32(key, value))
|
||||
case float64:
|
||||
span.SetAttributes(label.Float64(key, value))
|
||||
default:
|
||||
content, err := json.Marshal(value)
|
||||
if content == nil && err == nil {
|
||||
span.SetAttributes(label.String(key, "nil"))
|
||||
} else if content != nil && err == nil {
|
||||
span.SetAttributes(label.String(key, string(content)))
|
||||
} else {
|
||||
kataTraceLogger.WithField("type", "bug").Error("span attribute value error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AddTag adds additional key-value pairs to a tracing span. This can be used to provide
|
||||
// dynamic tags that are determined at runtime and tags with a non-string value.
|
||||
// Must have an even number of keyValues with keys being strings.
|
||||
func AddTags(span otelTrace.Span, keyValues ...interface{}) {
|
||||
if !tracing {
|
||||
return
|
||||
}
|
||||
if len(keyValues) < 2 {
|
||||
kataTraceLogger.WithField("type", "bug").Error("not enough inputs for attributes")
|
||||
return
|
||||
} else if len(keyValues)%2 != 0 {
|
||||
kataTraceLogger.WithField("type", "bug").Error("number of attribute keyValues is not even")
|
||||
return
|
||||
}
|
||||
for i := 0; i < len(keyValues); i++ {
|
||||
if key, ok := keyValues[i].(string); ok {
|
||||
addTag(span, key, keyValues[i+1])
|
||||
} else {
|
||||
kataTraceLogger.WithField("type", "bug").Error("key in attributes is not a string")
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user