| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelconf |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"errors" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
lognoop "go.opentelemetry.io/otel/log/noop" |
| 16 |
metricnoop "go.opentelemetry.io/otel/metric/noop" |
| 17 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 18 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 19 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 20 |
tracenoop "go.opentelemetry.io/otel/trace/noop" |
| 21 |
) |
| 22 |
|
| 23 |
func TestNewSDK(t *testing.T) { |
| 24 |
tests := []struct { |
| 25 |
name string |
| 26 |
cfg []ConfigurationOption |
| 27 |
wantTracerProvider any |
| 28 |
wantMeterProvider any |
| 29 |
wantLoggerProvider any |
| 30 |
wantErr error |
| 31 |
wantShutdownErr error |
| 32 |
}{ |
| 33 |
{ |
| 34 |
name: "no-configuration", |
| 35 |
wantTracerProvider: tracenoop.NewTracerProvider(), |
| 36 |
wantMeterProvider: metricnoop.NewMeterProvider(), |
| 37 |
wantLoggerProvider: lognoop.NewLoggerProvider(), |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "with-configuration", |
| 41 |
cfg: []ConfigurationOption{ |
| 42 |
WithContext(t.Context()), |
| 43 |
WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{ |
| 44 |
TracerProvider: &TracerProvider{}, |
| 45 |
MeterProvider: &MeterProvider{}, |
| 46 |
LoggerProvider: &LoggerProvider{}, |
| 47 |
}), |
| 48 |
}, |
| 49 |
wantTracerProvider: &sdktrace.TracerProvider{}, |
| 50 |
wantMeterProvider: &sdkmetric.MeterProvider{}, |
| 51 |
wantLoggerProvider: &sdklog.LoggerProvider{}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "with-sdk-disabled", |
| 55 |
cfg: []ConfigurationOption{ |
| 56 |
WithContext(t.Context()), |
| 57 |
WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{ |
| 58 |
Disabled: ptr(true), |
| 59 |
TracerProvider: &TracerProvider{}, |
| 60 |
MeterProvider: &MeterProvider{}, |
| 61 |
LoggerProvider: &LoggerProvider{}, |
| 62 |
}), |
| 63 |
}, |
| 64 |
wantTracerProvider: tracenoop.NewTracerProvider(), |
| 65 |
wantMeterProvider: metricnoop.NewMeterProvider(), |
| 66 |
wantLoggerProvider: lognoop.NewLoggerProvider(), |
| 67 |
}, |
| 68 |
} |
| 69 |
for _, tt := range tests { |
| 70 |
sdk, err := NewSDK(tt.cfg...) |
| 71 |
require.Equal(t, tt.wantErr, err) |
| 72 |
assert.IsType(t, tt.wantTracerProvider, sdk.TracerProvider()) |
| 73 |
assert.IsType(t, tt.wantMeterProvider, sdk.MeterProvider()) |
| 74 |
assert.IsType(t, tt.wantLoggerProvider, sdk.LoggerProvider()) |
| 75 |
require.Equal(t, tt.wantShutdownErr, sdk.Shutdown(t.Context())) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
var v02OpenTelemetryConfig = OpenTelemetryConfiguration{ |
| 80 |
Disabled: ptr(false), |
| 81 |
FileFormat: "0.2", |
| 82 |
AttributeLimits: &AttributeLimits{ |
| 83 |
AttributeCountLimit: ptr(128), |
| 84 |
AttributeValueLengthLimit: ptr(4096), |
| 85 |
}, |
| 86 |
LoggerProvider: &LoggerProvider{ |
| 87 |
Limits: &LogRecordLimits{ |
| 88 |
AttributeCountLimit: ptr(128), |
| 89 |
AttributeValueLengthLimit: ptr(4096), |
| 90 |
}, |
| 91 |
Processors: []LogRecordProcessor{ |
| 92 |
{ |
| 93 |
Batch: &BatchLogRecordProcessor{ |
| 94 |
ExportTimeout: ptr(30000), |
| 95 |
Exporter: LogRecordExporter{ |
| 96 |
OTLP: &OTLP{ |
| 97 |
Certificate: ptr("/app/cert.pem"), |
| 98 |
ClientCertificate: ptr("/app/cert.pem"), |
| 99 |
ClientKey: ptr("/app/cert.pem"), |
| 100 |
Compression: ptr("gzip"), |
| 101 |
Endpoint: "http://localhost:4318", |
| 102 |
Headers: Headers{ |
| 103 |
"api-key": "1234", |
| 104 |
}, |
| 105 |
Insecure: ptr(false), |
| 106 |
Protocol: "http/protobuf", |
| 107 |
Timeout: ptr(10000), |
| 108 |
}, |
| 109 |
}, |
| 110 |
MaxExportBatchSize: ptr(512), |
| 111 |
MaxQueueSize: ptr(2048), |
| 112 |
ScheduleDelay: ptr(5000), |
| 113 |
}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
Simple: &SimpleLogRecordProcessor{ |
| 117 |
Exporter: LogRecordExporter{ |
| 118 |
Console: Console{}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
}, |
| 124 |
MeterProvider: &MeterProvider{ |
| 125 |
Readers: []MetricReader{ |
| 126 |
{ |
| 127 |
Pull: &PullMetricReader{ |
| 128 |
Exporter: MetricExporter{ |
| 129 |
Prometheus: &Prometheus{ |
| 130 |
Host: ptr("localhost"), |
| 131 |
Port: ptr(9464), |
| 132 |
WithResourceConstantLabels: &IncludeExclude{ |
| 133 |
Excluded: []string{"service.attr1"}, |
| 134 |
Included: []string{"service*"}, |
| 135 |
}, |
| 136 |
WithoutScopeInfo: ptr(false), |
| 137 |
WithoutTypeSuffix: ptr(false), |
| 138 |
WithoutUnits: ptr(false), |
| 139 |
}, |
| 140 |
}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
Periodic: &PeriodicMetricReader{ |
| 145 |
Exporter: MetricExporter{ |
| 146 |
OTLP: &OTLPMetric{ |
| 147 |
Certificate: ptr("/app/cert.pem"), |
| 148 |
ClientCertificate: ptr("/app/cert.pem"), |
| 149 |
ClientKey: ptr("/app/cert.pem"), |
| 150 |
Compression: ptr("gzip"), |
| 151 |
DefaultHistogramAggregation: ptr(OTLPMetricDefaultHistogramAggregationBase2ExponentialBucketHistogram), |
| 152 |
Endpoint: "http://localhost:4318", |
| 153 |
Headers: Headers{ |
| 154 |
"api-key": "1234", |
| 155 |
}, |
| 156 |
Insecure: ptr(false), |
| 157 |
Protocol: "http/protobuf", |
| 158 |
TemporalityPreference: ptr("delta"), |
| 159 |
Timeout: ptr(10000), |
| 160 |
}, |
| 161 |
}, |
| 162 |
Interval: ptr(5000), |
| 163 |
Timeout: ptr(30000), |
| 164 |
}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
Periodic: &PeriodicMetricReader{ |
| 168 |
Exporter: MetricExporter{ |
| 169 |
Console: Console{}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
}, |
| 174 |
Views: []View{ |
| 175 |
{ |
| 176 |
Selector: &ViewSelector{ |
| 177 |
InstrumentName: ptr("my-instrument"), |
| 178 |
InstrumentType: ptr(ViewSelectorInstrumentTypeHistogram), |
| 179 |
MeterName: ptr("my-meter"), |
| 180 |
MeterSchemaUrl: ptr("https://opentelemetry.io/schemas/1.16.0"), |
| 181 |
MeterVersion: ptr("1.0.0"), |
| 182 |
Unit: ptr("ms"), |
| 183 |
}, |
| 184 |
Stream: &ViewStream{ |
| 185 |
Aggregation: &ViewStreamAggregation{ |
| 186 |
ExplicitBucketHistogram: &ViewStreamAggregationExplicitBucketHistogram{ |
| 187 |
Boundaries: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}, |
| 188 |
RecordMinMax: ptr(true), |
| 189 |
}, |
| 190 |
}, |
| 191 |
AttributeKeys: []string{"key1", "key2"}, |
| 192 |
Description: ptr("new_description"), |
| 193 |
Name: ptr("new_instrument_name"), |
| 194 |
}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
}, |
| 198 |
Propagator: &Propagator{ |
| 199 |
Composite: []string{"tracecontext", "baggage", "b3", "b3multi", "jaeger", "xray", "ottrace"}, |
| 200 |
}, |
| 201 |
Resource: &Resource{ |
| 202 |
Attributes: Attributes{ |
| 203 |
"service.name": "unknown_service", |
| 204 |
}, |
| 205 |
Detectors: &Detectors{ |
| 206 |
Attributes: &DetectorsAttributes{ |
| 207 |
Excluded: []string{"process.command_args"}, |
| 208 |
Included: []string{"process.*"}, |
| 209 |
}, |
| 210 |
}, |
| 211 |
SchemaUrl: ptr("https://opentelemetry.io/schemas/1.16.0"), |
| 212 |
}, |
| 213 |
TracerProvider: &TracerProvider{ |
| 214 |
Limits: &SpanLimits{ |
| 215 |
AttributeCountLimit: ptr(128), |
| 216 |
AttributeValueLengthLimit: ptr(4096), |
| 217 |
EventCountLimit: ptr(128), |
| 218 |
EventAttributeCountLimit: ptr(128), |
| 219 |
LinkCountLimit: ptr(128), |
| 220 |
LinkAttributeCountLimit: ptr(128), |
| 221 |
}, |
| 222 |
Processors: []SpanProcessor{ |
| 223 |
{ |
| 224 |
Batch: &BatchSpanProcessor{ |
| 225 |
ExportTimeout: ptr(30000), |
| 226 |
Exporter: SpanExporter{ |
| 227 |
OTLP: &OTLP{ |
| 228 |
Certificate: ptr("/app/cert.pem"), |
| 229 |
ClientCertificate: ptr("/app/cert.pem"), |
| 230 |
ClientKey: ptr("/app/cert.pem"), |
| 231 |
Compression: ptr("gzip"), |
| 232 |
Endpoint: "http://localhost:4318", |
| 233 |
Headers: Headers{ |
| 234 |
"api-key": "1234", |
| 235 |
}, |
| 236 |
Insecure: ptr(false), |
| 237 |
Protocol: "http/protobuf", |
| 238 |
Timeout: ptr(10000), |
| 239 |
}, |
| 240 |
}, |
| 241 |
MaxExportBatchSize: ptr(512), |
| 242 |
MaxQueueSize: ptr(2048), |
| 243 |
ScheduleDelay: ptr(5000), |
| 244 |
}, |
| 245 |
}, |
| 246 |
{ |
| 247 |
Batch: &BatchSpanProcessor{ |
| 248 |
Exporter: SpanExporter{ |
| 249 |
Zipkin: &Zipkin{ |
| 250 |
Endpoint: "http://localhost:9411/api/v2/spans", |
| 251 |
Timeout: ptr(10000), |
| 252 |
}, |
| 253 |
}, |
| 254 |
}, |
| 255 |
}, |
| 256 |
{ |
| 257 |
Simple: &SimpleSpanProcessor{ |
| 258 |
Exporter: SpanExporter{ |
| 259 |
Console: Console{}, |
| 260 |
}, |
| 261 |
}, |
| 262 |
}, |
| 263 |
}, |
| 264 |
Sampler: &Sampler{ |
| 265 |
ParentBased: &SamplerParentBased{ |
| 266 |
LocalParentNotSampled: &Sampler{ |
| 267 |
AlwaysOff: SamplerAlwaysOff{}, |
| 268 |
}, |
| 269 |
LocalParentSampled: &Sampler{ |
| 270 |
AlwaysOn: SamplerAlwaysOn{}, |
| 271 |
}, |
| 272 |
RemoteParentNotSampled: &Sampler{ |
| 273 |
AlwaysOff: SamplerAlwaysOff{}, |
| 274 |
}, |
| 275 |
RemoteParentSampled: &Sampler{ |
| 276 |
AlwaysOn: SamplerAlwaysOn{}, |
| 277 |
}, |
| 278 |
Root: &Sampler{ |
| 279 |
TraceIDRatioBased: &SamplerTraceIDRatioBased{ |
| 280 |
Ratio: ptr(0.0001), |
| 281 |
}, |
| 282 |
}, |
| 283 |
}, |
| 284 |
}, |
| 285 |
}, |
| 286 |
} |
| 287 |
|
| 288 |
func TestParseYAML(t *testing.T) { |
| 289 |
tests := []struct { |
| 290 |
name string |
| 291 |
input string |
| 292 |
wantErr error |
| 293 |
wantType any |
| 294 |
}{ |
| 295 |
{ |
| 296 |
name: "valid YAML config", |
| 297 |
input: `valid_empty.yaml`, |
| 298 |
wantErr: nil, |
| 299 |
wantType: &OpenTelemetryConfiguration{ |
| 300 |
Disabled: ptr(false), |
| 301 |
FileFormat: "0.1", |
| 302 |
}, |
| 303 |
}, |
| 304 |
{ |
| 305 |
name: "invalid config", |
| 306 |
input: "invalid_bool.yaml", |
| 307 |
wantErr: errors.New(`yaml: unmarshal errors: |
| 308 |
line 2: cannot unmarshal !!str ` + "`notabool`" + ` into bool`), |
| 309 |
}, |
| 310 |
{ |
| 311 |
name: "valid v0.2 config", |
| 312 |
input: "v0.2.yaml", |
| 313 |
wantType: &v02OpenTelemetryConfig, |
| 314 |
}, |
| 315 |
} |
| 316 |
|
| 317 |
for _, tt := range tests { |
| 318 |
t.Run(tt.name, func(t *testing.T) { |
| 319 |
b, err := os.ReadFile(filepath.Join("..", "testdata", tt.input)) |
| 320 |
require.NoError(t, err) |
| 321 |
|
| 322 |
got, err := ParseYAML(b) |
| 323 |
if tt.wantErr != nil { |
| 324 |
require.Equal(t, tt.wantErr.Error(), err.Error()) |
| 325 |
} else { |
| 326 |
require.NoError(t, err) |
| 327 |
assert.Equal(t, tt.wantType, got) |
| 328 |
} |
| 329 |
}) |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
func TestSerializeJSON(t *testing.T) { |
| 334 |
tests := []struct { |
| 335 |
name string |
| 336 |
input string |
| 337 |
wantErr error |
| 338 |
wantType any |
| 339 |
}{ |
| 340 |
{ |
| 341 |
name: "valid JSON config", |
| 342 |
input: `valid_empty.json`, |
| 343 |
wantErr: nil, |
| 344 |
wantType: OpenTelemetryConfiguration{ |
| 345 |
Disabled: ptr(false), |
| 346 |
FileFormat: "0.1", |
| 347 |
}, |
| 348 |
}, |
| 349 |
{ |
| 350 |
name: "invalid config", |
| 351 |
input: "invalid_bool.json", |
| 352 |
wantErr: errors.New(`json: cannot unmarshal string into Go struct field Plain.disabled of type bool`), |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "valid v0.2 config", |
| 356 |
input: "v0.2.json", |
| 357 |
wantType: v02OpenTelemetryConfig, |
| 358 |
}, |
| 359 |
} |
| 360 |
|
| 361 |
for _, tt := range tests { |
| 362 |
t.Run(tt.name, func(t *testing.T) { |
| 363 |
b, err := os.ReadFile(filepath.Join("..", "testdata", tt.input)) |
| 364 |
require.NoError(t, err) |
| 365 |
|
| 366 |
var got OpenTelemetryConfiguration |
| 367 |
err = json.Unmarshal(b, &got) |
| 368 |
|
| 369 |
if tt.wantErr != nil { |
| 370 |
require.Equal(t, tt.wantErr.Error(), err.Error()) |
| 371 |
} else { |
| 372 |
require.NoError(t, err) |
| 373 |
assert.Equal(t, tt.wantType, got) |
| 374 |
} |
| 375 |
}) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
func ptr[T any](v T) *T { |
| 380 |
return &v |
| 381 |
} |