| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package observ |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"sync" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel" |
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
mapi "go.opentelemetry.io/otel/metric" |
| 17 |
"go.opentelemetry.io/otel/metric/noop" |
| 18 |
"go.opentelemetry.io/otel/sdk" |
| 19 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 20 |
"go.opentelemetry.io/otel/sdk/metric" |
| 21 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 22 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 23 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 24 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 25 |
) |
| 26 |
|
| 27 |
func TestNextExporterID(t *testing.T) { |
| 28 |
SetSimpleProcessorID(0) |
| 29 |
|
| 30 |
var expected int64 |
| 31 |
for range 10 { |
| 32 |
id := NextSimpleProcessorID() |
| 33 |
assert.Equal(t, expected, id) |
| 34 |
expected++ |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestSetExporterID(t *testing.T) { |
| 39 |
SetSimpleProcessorID(0) |
| 40 |
|
| 41 |
prev := SetSimpleProcessorID(42) |
| 42 |
assert.Equal(t, int64(0), prev) |
| 43 |
|
| 44 |
id := NextSimpleProcessorID() |
| 45 |
assert.Equal(t, int64(42), id) |
| 46 |
} |
| 47 |
|
| 48 |
func TestNextExporterIDConcurrentSafe(t *testing.T) { |
| 49 |
SetSimpleProcessorID(0) |
| 50 |
|
| 51 |
const goroutines = 100 |
| 52 |
const increments = 10 |
| 53 |
|
| 54 |
var wg sync.WaitGroup |
| 55 |
wg.Add(goroutines) |
| 56 |
|
| 57 |
for range goroutines { |
| 58 |
go func() { |
| 59 |
defer wg.Done() |
| 60 |
for range increments { |
| 61 |
NextSimpleProcessorID() |
| 62 |
} |
| 63 |
}() |
| 64 |
} |
| 65 |
|
| 66 |
wg.Wait() |
| 67 |
|
| 68 |
expected := int64(goroutines * increments) |
| 69 |
id := NextSimpleProcessorID() |
| 70 |
assert.Equal(t, expected, id) |
| 71 |
} |
| 72 |
|
| 73 |
type errMeterProvider struct { |
| 74 |
mapi.MeterProvider |
| 75 |
err error |
| 76 |
} |
| 77 |
|
| 78 |
func (m *errMeterProvider) Meter(string, ...mapi.MeterOption) mapi.Meter { |
| 79 |
return &errMeter{err: m.err} |
| 80 |
} |
| 81 |
|
| 82 |
type errMeter struct { |
| 83 |
mapi.Meter |
| 84 |
err error |
| 85 |
} |
| 86 |
|
| 87 |
func (m *errMeter) Int64Counter(string, ...mapi.Int64CounterOption) (mapi.Int64Counter, error) { |
| 88 |
return nil, m.err |
| 89 |
} |
| 90 |
|
| 91 |
func (m *errMeter) Float64Histogram(string, ...mapi.Float64HistogramOption) (mapi.Float64Histogram, error) { |
| 92 |
return nil, m.err |
| 93 |
} |
| 94 |
|
| 95 |
const slpComponentID = 0 |
| 96 |
|
| 97 |
func TestNewSLPError(t *testing.T) { |
| 98 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 99 |
orig := otel.GetMeterProvider() |
| 100 |
t.Cleanup(func() { otel.SetMeterProvider(orig) }) |
| 101 |
|
| 102 |
errMp := &errMeterProvider{err: assert.AnError} |
| 103 |
otel.SetMeterProvider(errMp) |
| 104 |
|
| 105 |
_, err := NewSLP(slpComponentID) |
| 106 |
require.ErrorIs(t, err, assert.AnError) |
| 107 |
assert.ErrorContains(t, err, "failed to create a processed log metric") |
| 108 |
} |
| 109 |
|
| 110 |
func TestNewSLPDisabled(t *testing.T) { |
| 111 |
// Do not set OTEL_GO_X_OBSERVABILITY |
| 112 |
bsp, err := NewSLP(slpComponentID) |
| 113 |
assert.NoError(t, err) |
| 114 |
assert.Nil(t, bsp) |
| 115 |
} |
| 116 |
|
| 117 |
func setup(t *testing.T) (*SLP, func() metricdata.ScopeMetrics) { |
| 118 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 119 |
|
| 120 |
orig := otel.GetMeterProvider() |
| 121 |
t.Cleanup(func() { |
| 122 |
otel.SetMeterProvider(orig) |
| 123 |
}) |
| 124 |
|
| 125 |
reader := metric.NewManualReader() |
| 126 |
mp := metric.NewMeterProvider(metric.WithReader(reader)) |
| 127 |
otel.SetMeterProvider(mp) |
| 128 |
|
| 129 |
slp, err := NewSLP(slpComponentID) |
| 130 |
require.NoError(t, err) |
| 131 |
require.NotNil(t, slp) |
| 132 |
|
| 133 |
return slp, func() metricdata.ScopeMetrics { |
| 134 |
var got metricdata.ResourceMetrics |
| 135 |
require.NoError(t, reader.Collect(t.Context(), &got)) |
| 136 |
require.Len(t, got.ScopeMetrics, 1) |
| 137 |
return got.ScopeMetrics[0] |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func processedMetric(err error) metricdata.Metrics { |
| 142 |
processed := &otelconv.SDKProcessorLogProcessed{} |
| 143 |
|
| 144 |
attrs := []attribute.KeyValue{ |
| 145 |
GetSLPComponentName(slpComponentID), |
| 146 |
processed.AttrComponentType(otelconv.ComponentTypeSimpleLogProcessor), |
| 147 |
} |
| 148 |
|
| 149 |
if err != nil { |
| 150 |
attrs = append(attrs, semconv.ErrorType(err)) |
| 151 |
} |
| 152 |
|
| 153 |
dp := []metricdata.DataPoint[int64]{ |
| 154 |
{ |
| 155 |
Attributes: attribute.NewSet(attrs...), |
| 156 |
Value: 1, |
| 157 |
}, |
| 158 |
} |
| 159 |
|
| 160 |
return metricdata.Metrics{ |
| 161 |
Name: processed.Name(), |
| 162 |
Description: processed.Description(), |
| 163 |
Unit: processed.Unit(), |
| 164 |
Data: metricdata.Sum[int64]{ |
| 165 |
Temporality: metricdata.CumulativeTemporality, |
| 166 |
IsMonotonic: true, |
| 167 |
DataPoints: dp, |
| 168 |
}, |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
var Scope = instrumentation.Scope{ |
| 173 |
Name: ScopeName, |
| 174 |
Version: sdk.Version(), |
| 175 |
SchemaURL: semconv.SchemaURL, |
| 176 |
} |
| 177 |
|
| 178 |
func assertMetric(t *testing.T, got metricdata.ScopeMetrics, err error) { |
| 179 |
t.Helper() |
| 180 |
assert.Equal(t, Scope, got.Scope, "unexpected scope") |
| 181 |
m := got.Metrics |
| 182 |
require.Len(t, m, 1, "expected 1 metrics") |
| 183 |
|
| 184 |
o := metricdatatest.IgnoreTimestamp() |
| 185 |
want := processedMetric(err) |
| 186 |
|
| 187 |
metricdatatest.AssertEqual(t, want, m[0], o) |
| 188 |
} |
| 189 |
|
| 190 |
func TestSLP(t *testing.T) { |
| 191 |
t.Run("NoError", func(t *testing.T) { |
| 192 |
slp, collect := setup(t) |
| 193 |
slp.LogProcessed(t.Context(), nil) |
| 194 |
assertMetric(t, collect(), nil) |
| 195 |
}) |
| 196 |
|
| 197 |
t.Run("Error", func(t *testing.T) { |
| 198 |
processErr := errors.New("error processing log") |
| 199 |
slp, collect := setup(t) |
| 200 |
slp.LogProcessed(t.Context(), processErr) |
| 201 |
assertMetric(t, collect(), processErr) |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
func BenchmarkSLP(b *testing.B) { |
| 206 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 207 |
|
| 208 |
newSLP := func(b *testing.B) *SLP { |
| 209 |
b.Helper() |
| 210 |
slp, err := NewSLP(slpComponentID) |
| 211 |
require.NoError(b, err) |
| 212 |
require.NotNil(b, slp) |
| 213 |
return slp |
| 214 |
} |
| 215 |
|
| 216 |
b.Run("LogProcessed", func(b *testing.B) { |
| 217 |
orig := otel.GetMeterProvider() |
| 218 |
b.Cleanup(func() { |
| 219 |
otel.SetMeterProvider(orig) |
| 220 |
}) |
| 221 |
|
| 222 |
otel.SetMeterProvider(noop.NewMeterProvider()) |
| 223 |
|
| 224 |
ssp := newSLP(b) |
| 225 |
ctx := b.Context() |
| 226 |
|
| 227 |
b.ResetTimer() |
| 228 |
b.ReportAllocs() |
| 229 |
b.RunParallel(func(pb *testing.PB) { |
| 230 |
for pb.Next() { |
| 231 |
ssp.LogProcessed(ctx, nil) |
| 232 |
} |
| 233 |
}) |
| 234 |
}) |
| 235 |
|
| 236 |
b.Run("LogProcessedWithError", func(b *testing.B) { |
| 237 |
orig := otel.GetMeterProvider() |
| 238 |
b.Cleanup(func() { |
| 239 |
otel.SetMeterProvider(orig) |
| 240 |
}) |
| 241 |
otel.SetMeterProvider(noop.NewMeterProvider()) |
| 242 |
slp := newSLP(b) |
| 243 |
ctx := b.Context() |
| 244 |
|
| 245 |
processErr := errors.New("error processing log") |
| 246 |
|
| 247 |
b.ResetTimer() |
| 248 |
b.ReportAllocs() |
| 249 |
b.RunParallel(func(pb *testing.PB) { |
| 250 |
for pb.Next() { |
| 251 |
slp.LogProcessed(ctx, processErr) |
| 252 |
} |
| 253 |
}) |
| 254 |
}) |
| 255 |
} |