| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package observ |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel" |
| 13 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog/internal" |
| 14 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 16 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 17 |
|
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
"go.opentelemetry.io/otel/metric" |
| 20 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 21 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 22 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 23 |
) |
| 24 |
|
| 25 |
const ( |
| 26 |
ID = 0 |
| 27 |
) |
| 28 |
|
| 29 |
type errMeterProvider struct { |
| 30 |
metric.MeterProvider |
| 31 |
err error |
| 32 |
} |
| 33 |
|
| 34 |
func (m *errMeterProvider) Meter(string, ...metric.MeterOption) metric.Meter { |
| 35 |
return &errMeter{err: m.err} |
| 36 |
} |
| 37 |
|
| 38 |
type errMeter struct { |
| 39 |
metric.Meter |
| 40 |
err error |
| 41 |
} |
| 42 |
|
| 43 |
func (e *errMeter) Int64UpDownCounter(string, ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) { |
| 44 |
return nil, e.err |
| 45 |
} |
| 46 |
|
| 47 |
func (e *errMeter) Int64Counter(string, ...metric.Int64CounterOption) (metric.Int64Counter, error) { |
| 48 |
return nil, e.err |
| 49 |
} |
| 50 |
|
| 51 |
func (e *errMeter) Float64Histogram(string, ...metric.Float64HistogramOption) (metric.Float64Histogram, error) { |
| 52 |
return nil, e.err |
| 53 |
} |
| 54 |
|
| 55 |
func TestNewInstrumentation(t *testing.T) { |
| 56 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 57 |
t.Run("NoError", func(t *testing.T) { |
| 58 |
inst, err := NewInstrumentation(ID) |
| 59 |
require.NoError(t, err) |
| 60 |
assert.NotNil(t, inst.inflight, "logInflightMetric should be created") |
| 61 |
assert.NotNil(t, inst.exported, "logExportedMetric should be created") |
| 62 |
assert.NotNil(t, inst.duration, "logExportedDurationMetric should be created") |
| 63 |
}) |
| 64 |
|
| 65 |
t.Run("error", func(t *testing.T) { |
| 66 |
orig := otel.GetMeterProvider() |
| 67 |
t.Cleanup(func() { |
| 68 |
otel.SetMeterProvider(orig) |
| 69 |
}) |
| 70 |
otel.SetMeterProvider(&errMeterProvider{ |
| 71 |
err: assert.AnError, |
| 72 |
}) |
| 73 |
_, err := NewInstrumentation(ID) |
| 74 |
require.ErrorIs(t, err, assert.AnError, "new instrument errors") |
| 75 |
assert.ErrorContains(t, err, "inflight metric") |
| 76 |
assert.ErrorContains(t, err, "exported metric") |
| 77 |
assert.ErrorContains(t, err, "duration metric") |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
func set(err error) attribute.Set { |
| 82 |
attrs := []attribute.KeyValue{ |
| 83 |
semconv.OTelComponentName(GetComponentName(ID)), |
| 84 |
semconv.OTelComponentNameKey.String(ComponentType), |
| 85 |
} |
| 86 |
if err != nil { |
| 87 |
attrs = append(attrs, semconv.ErrorType(err)) |
| 88 |
} |
| 89 |
return attribute.NewSet(attrs...) |
| 90 |
} |
| 91 |
|
| 92 |
func logInflight() metricdata.Metrics { |
| 93 |
inflight := otelconv.SDKExporterLogInflight{} |
| 94 |
return metricdata.Metrics{ |
| 95 |
Name: inflight.Name(), |
| 96 |
Description: inflight.Description(), |
| 97 |
Unit: inflight.Unit(), |
| 98 |
Data: metricdata.Sum[int64]{ |
| 99 |
Temporality: metricdata.CumulativeTemporality, |
| 100 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 101 |
{Attributes: set(nil), Value: 0}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func logExported(success, total int64, err error) metricdata.Metrics { |
| 108 |
dp := []metricdata.DataPoint[int64]{ |
| 109 |
{Attributes: set(nil), Value: success}, |
| 110 |
} |
| 111 |
|
| 112 |
if err != nil { |
| 113 |
dp = append(dp, metricdata.DataPoint[int64]{ |
| 114 |
Attributes: set(err), |
| 115 |
Value: total - success, |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
exported := otelconv.SDKExporterLogExported{} |
| 120 |
return metricdata.Metrics{ |
| 121 |
Name: exported.Name(), |
| 122 |
Description: exported.Description(), |
| 123 |
Unit: exported.Unit(), |
| 124 |
Data: metricdata.Sum[int64]{ |
| 125 |
Temporality: metricdata.CumulativeTemporality, |
| 126 |
IsMonotonic: true, |
| 127 |
DataPoints: dp, |
| 128 |
}, |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func logExportedDuration(err error) metricdata.Metrics { |
| 133 |
attrs := set(err) |
| 134 |
|
| 135 |
duration := otelconv.SDKExporterOperationDuration{} |
| 136 |
return metricdata.Metrics{ |
| 137 |
Name: duration.Name(), |
| 138 |
Description: duration.Description(), |
| 139 |
Unit: duration.Unit(), |
| 140 |
Data: metricdata.Histogram[float64]{ |
| 141 |
Temporality: metricdata.CumulativeTemporality, |
| 142 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 143 |
{Attributes: attrs}, |
| 144 |
}, |
| 145 |
}, |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func setup(t *testing.T) (*Instrumentation, func() metricdata.ScopeMetrics) { |
| 150 |
t.Helper() |
| 151 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 152 |
|
| 153 |
original := otel.GetMeterProvider() |
| 154 |
t.Cleanup(func() { |
| 155 |
otel.SetMeterProvider(original) |
| 156 |
}) |
| 157 |
|
| 158 |
reader := sdkmetric.NewManualReader() |
| 159 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 160 |
otel.SetMeterProvider(mp) |
| 161 |
|
| 162 |
inst, err := NewInstrumentation(ID) |
| 163 |
require.NoError(t, err) |
| 164 |
require.NotNil(t, inst) |
| 165 |
|
| 166 |
return inst, func() metricdata.ScopeMetrics { |
| 167 |
var rm metricdata.ResourceMetrics |
| 168 |
require.NoError(t, reader.Collect(t.Context(), &rm)) |
| 169 |
require.Len(t, rm.ScopeMetrics, 1) |
| 170 |
return rm.ScopeMetrics[0] |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
var Scope = instrumentation.Scope{ |
| 175 |
Name: ScopeName, |
| 176 |
Version: internal.Version, |
| 177 |
SchemaURL: semconv.SchemaURL, |
| 178 |
} |
| 179 |
|
| 180 |
func assertMetrics( |
| 181 |
t *testing.T, |
| 182 |
got metricdata.ScopeMetrics, |
| 183 |
logs int64, |
| 184 |
success int64, |
| 185 |
err error, |
| 186 |
) { |
| 187 |
t.Helper() |
| 188 |
assert.Equal(t, Scope, got.Scope) |
| 189 |
|
| 190 |
m := got.Metrics |
| 191 |
require.Len(t, m, 3) |
| 192 |
|
| 193 |
o := metricdatatest.IgnoreTimestamp() |
| 194 |
want := logInflight() |
| 195 |
metricdatatest.AssertEqual(t, want, m[0], o) |
| 196 |
|
| 197 |
want = logExported(success, logs, err) |
| 198 |
metricdatatest.AssertEqual(t, want, m[1], o) |
| 199 |
|
| 200 |
want = logExportedDuration(err) |
| 201 |
metricdatatest.AssertEqual(t, want, m[2], metricdatatest.IgnoreValue(), o) |
| 202 |
} |
| 203 |
|
| 204 |
func TestInstrumentationExportLogs(t *testing.T) { |
| 205 |
inst, collect := setup(t) |
| 206 |
const n = 10 |
| 207 |
inst.ExportLogs(t.Context(), n).End(nil) |
| 208 |
assertMetrics(t, collect(), n, n, nil) |
| 209 |
} |
| 210 |
|
| 211 |
func TestInstrumentationExportLogPartialErrors(t *testing.T) { |
| 212 |
inst, collect := setup(t) |
| 213 |
const n = 10 |
| 214 |
const success = 5 |
| 215 |
|
| 216 |
err := internal.PartialSuccess{RejectedItems: n - success} |
| 217 |
inst.ExportLogs(t.Context(), n).End(err) |
| 218 |
|
| 219 |
assertMetrics(t, collect(), n, success, err) |
| 220 |
} |
| 221 |
|
| 222 |
func TestInstrumentationExportLogAllErrors(t *testing.T) { |
| 223 |
inst, collect := setup(t) |
| 224 |
const n = 10 |
| 225 |
const success = 0 |
| 226 |
inst.ExportLogs(t.Context(), n).End(assert.AnError) |
| 227 |
|
| 228 |
assertMetrics(t, collect(), n, success, assert.AnError) |
| 229 |
} |
| 230 |
|
| 231 |
func TestInstrumentationExportLogsInvalidPartialErrored(t *testing.T) { |
| 232 |
inst, collect := setup(t) |
| 233 |
const n = 10 |
| 234 |
err := internal.PartialSuccess{RejectedItems: -5} |
| 235 |
inst.ExportLogs(t.Context(), n).End(err) |
| 236 |
|
| 237 |
success := int64(n) |
| 238 |
assertMetrics(t, collect(), n, success, err) |
| 239 |
|
| 240 |
err.RejectedItems = n + 5 |
| 241 |
inst.ExportLogs(t.Context(), n).End(err) |
| 242 |
|
| 243 |
success += 0 |
| 244 |
assertMetrics(t, collect(), n+n, success, err) |
| 245 |
} |
| 246 |
|
| 247 |
func BenchmarkInstrumentationExportLogs(b *testing.B) { |
| 248 |
setup := func(tb *testing.B) *Instrumentation { |
| 249 |
tb.Helper() |
| 250 |
tb.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 251 |
inst, err := NewInstrumentation(ID) |
| 252 |
if err != nil { |
| 253 |
tb.Fatalf("failed to create instrumentation: %v", err) |
| 254 |
} |
| 255 |
return inst |
| 256 |
} |
| 257 |
run := func(err error) func(*testing.B) { |
| 258 |
return func(b *testing.B) { |
| 259 |
inst := setup(b) |
| 260 |
b.ReportAllocs() |
| 261 |
b.ResetTimer() |
| 262 |
for b.Loop() { |
| 263 |
inst.ExportLogs(b.Context(), 10).End(err) |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
b.Run("NoError", run(nil)) |
| 268 |
b.Run("PartialError", run(&internal.PartialSuccess{RejectedItems: 6})) |
| 269 |
b.Run("FullError", run(assert.AnError)) |
| 270 |
} |