mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 09:48:32 -05:00
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>
83 lines
3.4 KiB
Go
83 lines
3.4 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"
|
|
)
|
|
|
|
// This file contains some functions representing OpenTofu-specific semantic
|
|
// conventions, which we use alongside the general OpenTelemetry-specified
|
|
// semantic conventions.
|
|
//
|
|
// These functions tend to take strings that are expected to be the canonical
|
|
// string representation of some more specific type from elsewhere in OpenTofu,
|
|
// but we make the caller produce the string representation rather than doing it
|
|
// inline because this package needs to avoid importing any other packages
|
|
// from this codebase so that the rest of OpenTofu can use this package without
|
|
// creating import cycles.
|
|
//
|
|
// We only create functions in here for attribute names that we want to use
|
|
// consistently across many different callers. For one-off attribute names that
|
|
// are only used in a single kind of span, use the generic functions like
|
|
// [String], [StringSlice], etc, instead.
|
|
|
|
// OpenTofuProviderAddress returns an attribute definition for indicating
|
|
// which provider is relevant to a particular trace span.
|
|
//
|
|
// The given address should be the result of calling [addrs.Provider.String].
|
|
func OpenTofuProviderAddress(addr string) attribute.KeyValue {
|
|
return attribute.String("opentofu.provider.address", addr)
|
|
}
|
|
|
|
// OpenTofuProviderVersion returns an attribute definition for indicating
|
|
// which version of a provider is relevant to a particular trace span.
|
|
//
|
|
// The given address should be the result of calling
|
|
// [getproviders.Version.String]. This should typically be used alongside
|
|
// [OpenTofuProviderAddress] to indicate which provider the version number is
|
|
// for.
|
|
func OpenTofuProviderVersion(v string) attribute.KeyValue {
|
|
return attribute.String("opentofu.provider.version", v)
|
|
}
|
|
|
|
// OpenTofuTargetPlatform returns an attribute definition for indicating
|
|
// which target platform is relevant to a particular trace span.
|
|
//
|
|
// The given address should be the result of calling
|
|
// [getproviders.Platform.String].
|
|
func OpenTofuTargetPlatform(platform string) attribute.KeyValue {
|
|
return attribute.String("opentofu.target_platform", platform)
|
|
}
|
|
|
|
// OpenTofuModuleCallName returns an attribute definition for indicating
|
|
// the name of a module call that's relevant to a particular trace span.
|
|
//
|
|
// The given address should be something that would be valid in the
|
|
// [addrs.ModuleCall.Name] field.
|
|
func OpenTofuModuleCallName(name string) attribute.KeyValue {
|
|
return attribute.String("opentofu.module.name", name)
|
|
}
|
|
|
|
// OpenTofuModuleSource returns an attribute definition for indicating
|
|
// which module source address is relevant to a particular trace span.
|
|
//
|
|
// The given address should be the result of calling
|
|
// [addrs.ModuleSource.String], or any other syntax-compatible representation.
|
|
func OpenTofuModuleSource(addr string) attribute.KeyValue {
|
|
return attribute.String("opentofu.module.source", addr)
|
|
}
|
|
|
|
// OpenTofuModuleVersion returns an attribute definition for indicating
|
|
// which version of a module is relevant to a particular trace span.
|
|
//
|
|
// The given address should be either the result of calling
|
|
// [getproviders.Version.String], or the String method from the "Version" type
|
|
// from HashiCorp's "go-version" library.
|
|
func OpenTofuModuleVersion(v string) attribute.KeyValue {
|
|
return attribute.String("opentofu.module.version", v)
|
|
}
|