Files
opentf/internal/tracing/traceattrs/generic.go
Martin Atkins 0503163e28 tracing: Centralize our OpenTelemetry package imports
OpenTelemetry has various Go packages split across several Go modules that
often need to be carefully upgraded together. And in particular, we are
using the "semconv" package in conjunction with the OpenTelemetry SDK's
"resource" package in a way that requires that they both agree on which
version of the OpenTelemetry Semantic Conventions are being followed.

To help avoid "dependency hell" situations when upgrading, this centralizes
all of our direct calls into the OpenTelemetry SDK and tracing API into
packages under internal/tracing, by exposing a few thin wrapper functions
that other packages can use to access the same functionality indirectly.

We only use a relatively small subset of the OpenTelemetry library surface
area, so we don't need too many of these reexports and they should not
represent a significant additional maintenance burden.

For the semconv and resource interaction in particular this also factors
that out into a separate helper function with a unit test, so we should
notice quickly whenever they become misaligned. This complements the
end-to-end test previously added in opentofu/opentofu#3447 to give us
faster feedback about this particular problem, while the end-to-end test
has the broader scope of making sure there aren't any errors at all when
initializing OpenTelemetry tracing.

Finally, this also replaces the constants we previously had in package
traceaddrs with functions that return attribute.KeyValue values directly.
This matches the API style used by the OpenTelemetry semconv packages, and
makes the calls to these helpers from elsewhere in the system a little
more concise.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-10-30 13:27:10 -07:00

43 lines
1.6 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package traceattrs
import (
"go.opentelemetry.io/otel/attribute"
)
// String wraps [attribute.String] just so that we can keep most of our direct
// OpenTelemetry package imports centralized in this package where it's
// easier to keep our version selections consistent.
func String(name string, val string) attribute.KeyValue {
return attribute.String(name, val)
}
// StringSlice wraps [attribute.StringSlice] just so that we can keep most of
// our direct OpenTelemetry package imports centralized in this package where
// it's easier to keep our version selections consistent.
//
// If the items you want to report are not yet assembled into a string slice,
// consider using [tracing.StringSlice] with an [iter.Seq[string]] argument
// to skip constructing the slice when tracing isn't enabled.
func StringSlice(name string, val []string) attribute.KeyValue {
return attribute.StringSlice(name, val)
}
// Bool wraps [attribute.Bool] just so that we can keep most of our direct
// OpenTelemetry package imports centralized in this package where it's
// easier to keep our version selections consistent.
func Bool(name string, val bool) attribute.KeyValue {
return attribute.Bool(name, val)
}
// Int64 wraps [attribute.Int64] just so that we can keep most of our direct
// OpenTelemetry package imports centralized in this package where it's
// easier to keep our version selections consistent.
func Int64(name string, val int64) attribute.KeyValue {
return attribute.Int64(name, val)
}