Files
temporal/common/telemetry/env.go
Kent Gruber 96c3aaef6b Use better string splitting techniques where possible (#8226)
## What changed?

This PR aims to avoid usage of
[`strings.Split`](https://pkg.go.dev/strings#Split) where possible in
favor of better string splitting techniques, speficially:
[`strings.SplitN`](https://pkg.go.dev/strings#SplitN) and
[`strings.SplitSeq`](https://pkg.go.dev/strings#SplitSeq) where
appropriate.

There was also a [`strings.Fields`](https://pkg.go.dev/strings#Fields)
change I made to use
[`strings.FieldsSeq`](https://pkg.go.dev/strings#FieldsSeq) instead, and
another for S3 to use the [`path`](https://pkg.go.dev/path) package
instead of [`strings.Split`](https://pkg.go.dev/strings#Split).

## Why?

[`strings.SplitN`](https://pkg.go.dev/strings#SplitN) and
[`strings.SplitSeq`](https://pkg.go.dev/strings#SplitSeq) are often
better options in many cases, and can be _partially_ detected using
[`modernize`](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize):
> `stringsseq`: replace Split in "for range strings.Split(...)" by
go1.24's more efficient `SplitSeq`, or `Fields` with `FieldSeq`.

## How did you test it?
- [X] built
- [X] run locally and tested manually
- [x] covered by existing tests
- [x] added new unit test(s)
- [ ] added new functional test(s)

## Potential risks

There are lots of potentially subtle behaviors from the `strings.Split`
(and `strings.Fields`) usage that should be accounted for. If our
existing tests don't cover those subtleties, there's risk for
introducing an unintended bug. More intricate handling/parsing
previously using the `strings` package should get extra attention from
reviewers. I've attempted to break up my changes into logical commit
chunks to aid in review / help spot potentially concerning changes.
2025-08-26 13:23:40 -04:00

80 lines
2.5 KiB
Go

package telemetry
import (
"errors"
"fmt"
"strings"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
otelsdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.temporal.io/server/common/primitives"
)
var (
unsupportedTraceExporter = errors.New("unsupported OTEL exporter")
unsupportedTraceExporterProtocol = errors.New("unsupported OTEL exporter protocol")
)
const (
OtelServiceNameEnvKey = "OTEL_SERVICE_NAME"
OtelTracesExporterTypesEnvKey = "OTEL_TRACES_EXPORTER"
OtelTracesOtlpExporterType = SpanExporterType("otlp")
OtelExporterOtlpTracesProtocolEnvKey = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
OtelExporterOtlpTracesGrcpProtocol = "grpc"
)
type envVarLookup = func(string) (string, bool)
// SpanExportersFromEnv creates OTEL span exporters from environment variables.
func SpanExportersFromEnv(
envVars envVarLookup,
) (map[SpanExporterType]otelsdktrace.SpanExporter, error) {
exporters := map[SpanExporterType]otelsdktrace.SpanExporter{}
exporterTypes, ok := envVars(OtelTracesExporterTypesEnvKey)
if !ok {
return exporters, nil
}
for exporterType := range strings.SplitSeq(exporterTypes, ",") {
switch SpanExporterType(exporterType) {
case OtelTracesOtlpExporterType:
// only grpc is supported; fail if user requests a different protocol
if protocol, exists := envVars(OtelExporterOtlpTracesProtocolEnvKey); exists {
isSupported := protocol == OtelExporterOtlpTracesGrcpProtocol
if !isSupported {
return nil, fmt.Errorf("%w: %v=%v", unsupportedTraceExporterProtocol, OtelExporterOtlpTracesProtocolEnvKey, protocol)
}
}
// other OTEL configuration env variables are picked up automatically by the exporter itself
exporters[OtelTracesOtlpExporterType] = otlptracegrpc.NewUnstarted()
case "none":
// ignored
default:
return nil, fmt.Errorf("%w: %v=%v", unsupportedTraceExporter, OtelTracesExporterTypesEnvKey, exporterType)
}
}
return exporters, nil
}
// ResourceServiceName returns the OpenTelemetry tracing service name for a Temporal service.
func ResourceServiceName(
rsn primitives.ServiceName,
envVars envVarLookup,
) string {
// map "internal-frontend" to "frontend" for the purpose of tracing
if rsn == primitives.InternalFrontendService {
rsn = primitives.FrontendService
}
// allow custom prefix via env vars
serviceNamePrefix := "io.temporal"
if customServicePrefix, found := envVars(OtelServiceNameEnvKey); found {
serviceNamePrefix = customServicePrefix
}
return fmt.Sprintf("%s.%s", serviceNamePrefix, string(rsn))
}