| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package observ // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal/observ" |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"google.golang.org/grpc/codes" |
| 12 |
"google.golang.org/grpc/status" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel" |
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal" |
| 17 |
mapi "go.opentelemetry.io/otel/metric" |
| 18 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 19 |
"go.opentelemetry.io/otel/sdk/metric" |
| 20 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 21 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 22 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 23 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
ID = 0 |
| 28 |
TARGET = "localhost:8080" |
| 29 |
) |
| 30 |
|
| 31 |
type errMeterProvider struct { |
| 32 |
mapi.MeterProvider |
| 33 |
|
| 34 |
err error |
| 35 |
} |
| 36 |
|
| 37 |
func (m *errMeterProvider) Meter(string, ...mapi.MeterOption) mapi.Meter { |
| 38 |
return &errMeter{err: m.err} |
| 39 |
} |
| 40 |
|
| 41 |
type errMeter struct { |
| 42 |
mapi.Meter |
| 43 |
|
| 44 |
err error |
| 45 |
} |
| 46 |
|
| 47 |
func (m *errMeter) Int64UpDownCounter(string, ...mapi.Int64UpDownCounterOption) (mapi.Int64UpDownCounter, error) { |
| 48 |
return nil, m.err |
| 49 |
} |
| 50 |
|
| 51 |
func (m *errMeter) Int64Counter(string, ...mapi.Int64CounterOption) (mapi.Int64Counter, error) { |
| 52 |
return nil, m.err |
| 53 |
} |
| 54 |
|
| 55 |
func (m *errMeter) Float64Histogram(string, ...mapi.Float64HistogramOption) (mapi.Float64Histogram, error) { |
| 56 |
return nil, m.err |
| 57 |
} |
| 58 |
|
| 59 |
func TestNewExporterMetrics(t *testing.T) { |
| 60 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 61 |
|
| 62 |
t.Run("No Error", func(t *testing.T) { |
| 63 |
em, err := NewInstrumentation(ID, "dns:///example.com:42") |
| 64 |
require.NoError(t, err) |
| 65 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 66 |
semconv.OTelComponentName(GetComponentName(ID)), |
| 67 |
semconv.OTelComponentTypeKey.String(string(otelconv.ComponentTypeOtlpGRPCLogExporter)), |
| 68 |
semconv.ServerAddress("example.com"), |
| 69 |
semconv.ServerPort(42), |
| 70 |
}, em.presetAttrs) |
| 71 |
|
| 72 |
assert.NotNil(t, em.logInflightMetric, "logInflightMetric should be created") |
| 73 |
assert.NotNil(t, em.logExportedMetric, "logExportedMetric should be created") |
| 74 |
assert.NotNil(t, em.logExportedDurationMetric, "logExportedDurationMetric should be created") |
| 75 |
}) |
| 76 |
|
| 77 |
t.Run("Error", func(t *testing.T) { |
| 78 |
orig := otel.GetMeterProvider() |
| 79 |
t.Cleanup(func() { otel.SetMeterProvider(orig) }) |
| 80 |
mp := &errMeterProvider{err: assert.AnError} |
| 81 |
otel.SetMeterProvider(mp) |
| 82 |
|
| 83 |
_, err := NewInstrumentation(ID, "dns:///:8080") |
| 84 |
require.ErrorIs(t, err, assert.AnError, "new instrument errors") |
| 85 |
|
| 86 |
assert.ErrorContains(t, err, "inflight metric") |
| 87 |
assert.ErrorContains(t, err, "log exported metric") |
| 88 |
assert.ErrorContains(t, err, "operation duration metric") |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
func TestServerAddrAttrs(t *testing.T) { |
| 93 |
testcases := []struct { |
| 94 |
name string |
| 95 |
target string |
| 96 |
want []attribute.KeyValue |
| 97 |
}{ |
| 98 |
{ |
| 99 |
name: "Unix socket", |
| 100 |
target: "unix:///tmp/grpc.sock", |
| 101 |
want: []attribute.KeyValue{semconv.ServerAddress("/tmp/grpc.sock")}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "DNS with port", |
| 105 |
target: "dns:///localhost:8080", |
| 106 |
want: []attribute.KeyValue{semconv.ServerAddress("localhost"), semconv.ServerPort(8080)}, |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "Dns with endpoint host:port", |
| 110 |
target: "dns://8.8.8.8/example.com:4", |
| 111 |
want: []attribute.KeyValue{semconv.ServerAddress("example.com"), semconv.ServerPort(4)}, |
| 112 |
}, |
| 113 |
} |
| 114 |
for _, tc := range testcases { |
| 115 |
t.Run(tc.name, func(t *testing.T) { |
| 116 |
attrs := ServerAddrAttrs(tc.target) |
| 117 |
assert.Equal(t, tc.want, attrs) |
| 118 |
}) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func set(err error) attribute.Set { |
| 123 |
attrs := []attribute.KeyValue{ |
| 124 |
semconv.OTelComponentName(GetComponentName(ID)), |
| 125 |
semconv.OTelComponentTypeKey.String(string(otelconv.ComponentTypeOtlpGRPCLogExporter)), |
| 126 |
} |
| 127 |
attrs = append(attrs, ServerAddrAttrs(TARGET)...) |
| 128 |
if err != nil { |
| 129 |
attrs = append(attrs, semconv.ErrorType(err)) |
| 130 |
} |
| 131 |
return attribute.NewSet(attrs...) |
| 132 |
} |
| 133 |
|
| 134 |
func logInflightMetrics() metricdata.Metrics { |
| 135 |
m := otelconv.SDKExporterLogInflight{} |
| 136 |
return metricdata.Metrics{ |
| 137 |
Name: m.Name(), |
| 138 |
Description: m.Description(), |
| 139 |
Unit: m.Unit(), |
| 140 |
Data: metricdata.Sum[int64]{ |
| 141 |
Temporality: metricdata.CumulativeTemporality, |
| 142 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 143 |
{Attributes: set(nil), Value: 0}, |
| 144 |
}, |
| 145 |
}, |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func logExportedMetrics(success, total int64, err error) metricdata.Metrics { |
| 150 |
dp := []metricdata.DataPoint[int64]{ |
| 151 |
{Attributes: set(nil), Value: success}, |
| 152 |
} |
| 153 |
|
| 154 |
if err != nil { |
| 155 |
dp = append(dp, metricdata.DataPoint[int64]{ |
| 156 |
Attributes: set(err), |
| 157 |
Value: total - success, |
| 158 |
}) |
| 159 |
} |
| 160 |
|
| 161 |
m := otelconv.SDKExporterLogExported{} |
| 162 |
return metricdata.Metrics{ |
| 163 |
Name: m.Name(), |
| 164 |
Description: m.Description(), |
| 165 |
Unit: m.Unit(), |
| 166 |
Data: metricdata.Sum[int64]{ |
| 167 |
Temporality: metricdata.CumulativeTemporality, |
| 168 |
IsMonotonic: true, |
| 169 |
DataPoints: dp, |
| 170 |
}, |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func logOperationDurationMetrics(err error, code codes.Code) metricdata.Metrics { |
| 175 |
attrs := []attribute.KeyValue{ |
| 176 |
semconv.OTelComponentName(GetComponentName(ID)), |
| 177 |
semconv.OTelComponentTypeKey.String(string(otelconv.ComponentTypeOtlpGRPCLogExporter)), |
| 178 |
semconv.RPCResponseStatusCode(code.String()), |
| 179 |
} |
| 180 |
attrs = append(attrs, ServerAddrAttrs(TARGET)...) |
| 181 |
if err != nil { |
| 182 |
attrs = append(attrs, semconv.ErrorType(err)) |
| 183 |
} |
| 184 |
|
| 185 |
m := otelconv.SDKExporterOperationDuration{} |
| 186 |
return metricdata.Metrics{ |
| 187 |
Name: m.Name(), |
| 188 |
Description: m.Description(), |
| 189 |
Unit: m.Unit(), |
| 190 |
Data: metricdata.Histogram[float64]{ |
| 191 |
Temporality: metricdata.CumulativeTemporality, |
| 192 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 193 |
{Attributes: attribute.NewSet(attrs...)}, |
| 194 |
}, |
| 195 |
}, |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func setup(t *testing.T) (*Instrumentation, func() metricdata.ScopeMetrics) { |
| 200 |
t.Helper() |
| 201 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 202 |
original := otel.GetMeterProvider() |
| 203 |
t.Cleanup(func() { |
| 204 |
otel.SetMeterProvider(original) |
| 205 |
}) |
| 206 |
|
| 207 |
r := metric.NewManualReader() |
| 208 |
mp := metric.NewMeterProvider(metric.WithReader(r)) |
| 209 |
otel.SetMeterProvider(mp) |
| 210 |
|
| 211 |
inst, err := NewInstrumentation(ID, TARGET) |
| 212 |
require.NoError(t, err) |
| 213 |
require.NotNil(t, inst) |
| 214 |
|
| 215 |
return inst, func() metricdata.ScopeMetrics { |
| 216 |
var rm metricdata.ResourceMetrics |
| 217 |
require.NoError(t, r.Collect(t.Context(), &rm)) |
| 218 |
require.Len(t, rm.ScopeMetrics, 1) |
| 219 |
return rm.ScopeMetrics[0] |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
var Scope = instrumentation.Scope{ |
| 224 |
Name: ScopeName, |
| 225 |
Version: Version, |
| 226 |
SchemaURL: semconv.SchemaURL, |
| 227 |
} |
| 228 |
|
| 229 |
func assertMetrics( |
| 230 |
t *testing.T, |
| 231 |
got metricdata.ScopeMetrics, |
| 232 |
spans int64, |
| 233 |
success int64, |
| 234 |
err error, |
| 235 |
code codes.Code, |
| 236 |
) { |
| 237 |
t.Helper() |
| 238 |
|
| 239 |
assert.Equal(t, Scope, got.Scope, "unexpected scope") |
| 240 |
|
| 241 |
m := got.Metrics |
| 242 |
require.Len(t, m, 3, "expected 3 metrics") |
| 243 |
|
| 244 |
o := metricdatatest.IgnoreTimestamp() |
| 245 |
want := logInflightMetrics() |
| 246 |
metricdatatest.AssertEqual(t, want, m[0], o) |
| 247 |
|
| 248 |
want = logExportedMetrics(success, spans, err) |
| 249 |
metricdatatest.AssertEqual(t, want, m[1], o) |
| 250 |
|
| 251 |
want = logOperationDurationMetrics(err, code) |
| 252 |
metricdatatest.AssertEqual(t, want, m[2], metricdatatest.IgnoreValue(), o) |
| 253 |
} |
| 254 |
|
| 255 |
func TestInstrumentationExportLogs(t *testing.T) { |
| 256 |
inst, collect := setup(t) |
| 257 |
const n = 10 |
| 258 |
inst.ExportLogs(t.Context(), n).End(nil) |
| 259 |
assertMetrics(t, collect(), n, n, nil, codes.OK) |
| 260 |
} |
| 261 |
|
| 262 |
func TestInstrumentationExportLogPartialErrors(t *testing.T) { |
| 263 |
inst, collect := setup(t) |
| 264 |
const n = 10 |
| 265 |
const success = 5 |
| 266 |
|
| 267 |
err := internal.PartialSuccess{RejectedItems: n - success} |
| 268 |
inst.ExportLogs(t.Context(), n).End(err) |
| 269 |
|
| 270 |
assertMetrics(t, collect(), n, success, err, status.Code(err)) |
| 271 |
} |
| 272 |
|
| 273 |
func TestInstrumentationExportLogAllErrors(t *testing.T) { |
| 274 |
inst, collect := setup(t) |
| 275 |
const n = 10 |
| 276 |
const success = 0 |
| 277 |
inst.ExportLogs(t.Context(), n).End(assert.AnError) |
| 278 |
|
| 279 |
assertMetrics(t, collect(), n, success, assert.AnError, status.Code(assert.AnError)) |
| 280 |
} |
| 281 |
|
| 282 |
func TestInstrumentationExportLogsInvalidPartialErrored(t *testing.T) { |
| 283 |
inst, collect := setup(t) |
| 284 |
const n = 10 |
| 285 |
err := internal.PartialSuccess{RejectedItems: -5} |
| 286 |
inst.ExportLogs(t.Context(), n).End(err) |
| 287 |
|
| 288 |
success := int64(n) |
| 289 |
assertMetrics(t, collect(), n, success, err, status.Code(err)) |
| 290 |
|
| 291 |
err.RejectedItems = n + 5 |
| 292 |
inst.ExportLogs(t.Context(), n).End(err) |
| 293 |
|
| 294 |
success += 0 |
| 295 |
assertMetrics(t, collect(), n+n, success, err, status.Code(err)) |
| 296 |
} |
| 297 |
|
| 298 |
func BenchmarkInstrumentationExportLogs(b *testing.B) { |
| 299 |
setup := func(tb *testing.B) *Instrumentation { |
| 300 |
tb.Helper() |
| 301 |
tb.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 302 |
inst, err := NewInstrumentation(ID, TARGET) |
| 303 |
if err != nil { |
| 304 |
tb.Fatalf("failed to create instrumentation: %v", err) |
| 305 |
} |
| 306 |
return inst |
| 307 |
} |
| 308 |
run := func(err error) func(*testing.B) { |
| 309 |
return func(b *testing.B) { |
| 310 |
inst := setup(b) |
| 311 |
b.ReportAllocs() |
| 312 |
b.ResetTimer() |
| 313 |
for b.Loop() { |
| 314 |
inst.ExportLogs(b.Context(), 10).End(err) |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
b.Run("NoError", run(nil)) |
| 319 |
b.Run("PartialError", run(&internal.PartialSuccess{RejectedItems: 6})) |
| 320 |
b.Run("FullError", run(assert.AnError)) |
| 321 |
} |
| 322 |
|
| 323 |
func BenchmarkSetPresetAttrs(b *testing.B) { |
| 324 |
b.ResetTimer() |
| 325 |
b.ReportAllocs() |
| 326 |
for i := range b.N { |
| 327 |
getPresetAttrs(int64(i), "dns:///192.168.1.1:8080") |
| 328 |
} |
| 329 |
} |