You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2026-06-03 18:35:08 +02:00
88f8c10823
### Description This PR adds a `depguard` linter rule to [.golangci.yml](file:///Users/ijasahammed/Documents/open_source/opentelemetry-go/.golangci.yml) to enforce the use of the latest semantic convention version (`v1.40.0`). This rule ensures that: - Any new code importing `otel/semconv` must use `v1.40.0`. - The `semconv/` directory itself is excluded from this check to avoid linting legacy definitions. - The `zipkin exporter` is excluded from this check - Updated `semconv` version to v1.40.0 in the `sdk/log/logger_bench_test.go` test. ### Verification Results I verified this change locally using the following steps: 1. **Full Linter Run**: Ran `make precommit` (which executes `golangci-lint run`). It passed with **0 issues** across the entire repository. 2. **Smoke Test**: Added a temporary import of `go.opentelemetry.io/otel/semconv/v1.20.0` in trace.go. The linter correctly reported the violation: > `trace.go:7:2: import 'go.opentelemetry.io/otel/semconv/v1.20.0' is not allowed from list 'semconv': Use go.opentelemetry.io/otel/semconv/v1.40.0 instead. (depguard)` This change is not performance-critical and does not affect the library's runtime behavior. ### Checklist - [x] Signed CLA - [x] `make precommit` passes. - [x] CHANGELOG.md updated. (Not required) Fixes #7842 --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Damien Mathieu <42@dmathieu.com>
158 lines
3.5 KiB
Go
158 lines
3.5 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package log // import "go.opentelemetry.io/otel/sdk/log"
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/log"
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
"go.opentelemetry.io/otel/sdk/metric"
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
|
|
)
|
|
|
|
func BenchmarkLoggerEmit(b *testing.B) {
|
|
logger := newTestLogger(b)
|
|
|
|
r := log.Record{}
|
|
r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
|
|
r.SetObservedTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
|
|
r.SetBody(log.StringValue("testing body value"))
|
|
r.SetSeverity(log.SeverityInfo)
|
|
r.SetSeverityText("testing text")
|
|
|
|
r.AddAttributes(
|
|
log.String("k1", "str"),
|
|
log.Float64("k2", 1.0),
|
|
log.Int("k3", 2),
|
|
log.Bool("k4", true),
|
|
log.Bytes("k5", []byte{1}),
|
|
)
|
|
|
|
r10 := r
|
|
r10.AddAttributes(
|
|
log.String("k6", "str"),
|
|
log.Float64("k7", 1.0),
|
|
log.Int("k8", 2),
|
|
log.Bool("k9", true),
|
|
log.Bytes("k10", []byte{1}),
|
|
)
|
|
|
|
require.Equal(b, 5, r.AttributesLen())
|
|
require.Equal(b, 10, r10.AttributesLen())
|
|
|
|
b.Run("5 attributes", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
logger.Emit(b.Context(), r)
|
|
}
|
|
})
|
|
})
|
|
|
|
b.Run("10 attributes", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
logger.Emit(b.Context(), r10)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
func BenchmarkLoggerEmitObservability(b *testing.B) {
|
|
r := log.Record{}
|
|
|
|
orig := otel.GetMeterProvider()
|
|
b.Cleanup(func() { otel.SetMeterProvider(orig) })
|
|
reader := metric.NewManualReader()
|
|
mp := metric.NewMeterProvider(metric.WithReader(reader))
|
|
otel.SetMeterProvider(mp)
|
|
|
|
run := func(logger *logger) func(b *testing.B) {
|
|
return func(b *testing.B) {
|
|
b.ResetTimer()
|
|
b.ReportAllocs()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
logger.Emit(b.Context(), r)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
lp := NewLoggerProvider()
|
|
scope := instrumentation.Scope{}
|
|
|
|
b.Run("Disabled", run(newLogger(lp, scope)))
|
|
|
|
b.Run("Enabled", func(b *testing.B) {
|
|
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
|
|
|
|
run(newLogger(lp, scope))(b)
|
|
})
|
|
|
|
var rm metricdata.ResourceMetrics
|
|
err := reader.Collect(b.Context(), &rm)
|
|
require.NoError(b, err)
|
|
require.Len(b, rm.ScopeMetrics, 1)
|
|
}
|
|
|
|
func BenchmarkLoggerEnabled(b *testing.B) {
|
|
logger := newTestLogger(b)
|
|
ctx := b.Context()
|
|
param := log.EnabledParameters{Severity: log.SeverityDebug}
|
|
var enabled bool
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
enabled = logger.Enabled(ctx, param)
|
|
}
|
|
|
|
_ = enabled
|
|
}
|
|
|
|
func BenchmarkLoggerSetErrAndEmit(b *testing.B) {
|
|
logger := newTestLogger(b)
|
|
err := errors.New("boom")
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
r := log.Record{}
|
|
r.SetErr(err)
|
|
logger.Emit(b.Context(), r)
|
|
}
|
|
}
|
|
|
|
func BenchmarkLoggerSetExceptionAttributesAndEmit(b *testing.B) {
|
|
logger := newTestLogger(b)
|
|
err := errors.New("boom")
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
r := log.Record{}
|
|
r.AddAttributes(log.String(string(semconv.ExceptionMessageKey), err.Error()))
|
|
r.AddAttributes(log.String(string(semconv.ExceptionTypeKey), errorType(err)))
|
|
logger.Emit(b.Context(), r)
|
|
}
|
|
}
|
|
|
|
func newTestLogger(t testing.TB) log.Logger {
|
|
provider := NewLoggerProvider(
|
|
WithProcessor(newFltrProcessor("0", false)),
|
|
WithProcessor(newFltrProcessor("1", true)),
|
|
)
|
|
return provider.Logger(t.Name())
|
|
}
|