| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelconf |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
"crypto/tls" |
| 10 |
"crypto/x509" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"net" |
| 14 |
"net/http" |
| 15 |
"net/http/httptest" |
| 16 |
"os" |
| 17 |
"path/filepath" |
| 18 |
"reflect" |
| 19 |
"runtime" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/stretchr/testify/assert" |
| 24 |
"github.com/stretchr/testify/require" |
| 25 |
"go.opentelemetry.io/otel/attribute" |
| 26 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" |
| 27 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 28 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
| 29 |
"go.opentelemetry.io/otel/metric" |
| 30 |
"go.opentelemetry.io/otel/metric/noop" |
| 31 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 32 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 33 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 34 |
"go.opentelemetry.io/otel/sdk/resource" |
| 35 |
v1 "go.opentelemetry.io/proto/otlp/collector/metrics/v1" |
| 36 |
"google.golang.org/grpc" |
| 37 |
"google.golang.org/grpc/credentials" |
| 38 |
) |
| 39 |
|
| 40 |
func TestMeterProvider(t *testing.T) { |
| 41 |
tests := []struct { |
| 42 |
name string |
| 43 |
cfg configOptions |
| 44 |
wantProvider metric.MeterProvider |
| 45 |
wantErr error |
| 46 |
}{ |
| 47 |
{ |
| 48 |
name: "no-meter-provider-configured", |
| 49 |
wantProvider: noop.NewMeterProvider(), |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "error-in-config", |
| 53 |
cfg: configOptions{ |
| 54 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 55 |
MeterProvider: &MeterProvider{ |
| 56 |
Readers: []MetricReader{ |
| 57 |
{ |
| 58 |
Periodic: &PeriodicMetricReader{}, |
| 59 |
Pull: &PullMetricReader{}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
wantProvider: noop.NewMeterProvider(), |
| 66 |
wantErr: newErrInvalid("must not specify multiple metric reader type"), |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "multiple-errors-in-config", |
| 70 |
cfg: configOptions{ |
| 71 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 72 |
MeterProvider: &MeterProvider{ |
| 73 |
Readers: []MetricReader{ |
| 74 |
{ |
| 75 |
Periodic: &PeriodicMetricReader{}, |
| 76 |
Pull: &PullMetricReader{}, |
| 77 |
}, |
| 78 |
{ |
| 79 |
Periodic: &PeriodicMetricReader{ |
| 80 |
Exporter: PushMetricExporter{ |
| 81 |
Console: &ConsoleMetricExporter{}, |
| 82 |
OTLPGrpc: &OTLPGrpcMetricExporter{}, |
| 83 |
}, |
| 84 |
}, |
| 85 |
}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
wantProvider: noop.NewMeterProvider(), |
| 91 |
wantErr: newErrInvalid("must not specify multiple metric reader type"), |
| 92 |
}, |
| 93 |
} |
| 94 |
for _, tt := range tests { |
| 95 |
t.Run(tt.name, func(t *testing.T) { |
| 96 |
mp, shutdown, err := meterProvider(tt.cfg, resource.Default()) |
| 97 |
require.Equal(t, tt.wantProvider, mp) |
| 98 |
assert.ErrorIs(t, err, tt.wantErr) |
| 99 |
require.NoError(t, shutdown(t.Context())) |
| 100 |
}) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestMeterProviderOptions(t *testing.T) { |
| 105 |
var calls int |
| 106 |
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 107 |
calls++ |
| 108 |
})) |
| 109 |
defer srv.Close() |
| 110 |
|
| 111 |
cfg := OpenTelemetryConfiguration{ |
| 112 |
MeterProvider: &MeterProvider{ |
| 113 |
Readers: []MetricReader{{ |
| 114 |
Periodic: &PeriodicMetricReader{ |
| 115 |
Exporter: PushMetricExporter{ |
| 116 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 117 |
Endpoint: ptr(srv.URL), |
| 118 |
}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
}}, |
| 122 |
}, |
| 123 |
} |
| 124 |
|
| 125 |
var buf bytes.Buffer |
| 126 |
stdoutmetricExporter, err := stdoutmetric.New(stdoutmetric.WithWriter(&buf)) |
| 127 |
require.NoError(t, err) |
| 128 |
|
| 129 |
res := resource.NewSchemaless(attribute.String("foo", "bar")) |
| 130 |
sdk, err := NewSDK( |
| 131 |
WithOpenTelemetryConfiguration(cfg), |
| 132 |
WithMeterProviderOptions(sdkmetric.WithReader(sdkmetric.NewPeriodicReader(stdoutmetricExporter))), |
| 133 |
WithMeterProviderOptions(sdkmetric.WithResource(res)), |
| 134 |
) |
| 135 |
require.NoError(t, err) |
| 136 |
defer func() { |
| 137 |
assert.NoError(t, sdk.Shutdown(t.Context())) |
| 138 |
// The exporter, which we passed in as an extra option to NewSDK, |
| 139 |
// should be wired up to the provider in addition to the |
| 140 |
// configuration-based OTLP exporter. |
| 141 |
assert.NotZero(t, buf) |
| 142 |
assert.Equal(t, 1, calls) // flushed on shutdown |
| 143 |
|
| 144 |
// Options provided by WithMeterProviderOptions may be overridden |
| 145 |
// by configuration, e.g. the resource is always defined via |
| 146 |
// configuration. |
| 147 |
assert.NotContains(t, buf.String(), "foo") |
| 148 |
}() |
| 149 |
|
| 150 |
counter, _ := sdk.MeterProvider().Meter("test").Int64Counter("counter") |
| 151 |
counter.Add(t.Context(), 1) |
| 152 |
} |
| 153 |
|
| 154 |
func TestReader(t *testing.T) { |
| 155 |
consoleExporter, err := stdoutmetric.New( |
| 156 |
stdoutmetric.WithPrettyPrint(), |
| 157 |
) |
| 158 |
require.NoError(t, err) |
| 159 |
ctx := t.Context() |
| 160 |
otlpGRPCExporter, err := otlpmetricgrpc.New(ctx) |
| 161 |
require.NoError(t, err) |
| 162 |
otlpHTTPExporter, err := otlpmetrichttp.New(ctx) |
| 163 |
require.NoError(t, err) |
| 164 |
testCases := []struct { |
| 165 |
name string |
| 166 |
reader MetricReader |
| 167 |
args any |
| 168 |
wantErrT error |
| 169 |
wantReader sdkmetric.Reader |
| 170 |
}{ |
| 171 |
{ |
| 172 |
name: "no reader", |
| 173 |
wantErrT: newErrInvalid("no valid metric reader"), |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "pull/no-exporter", |
| 177 |
reader: MetricReader{ |
| 178 |
Pull: &PullMetricReader{}, |
| 179 |
}, |
| 180 |
wantErrT: newErrInvalid("no valid metric exporter"), |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "periodic/otlp-grpc-exporter", |
| 184 |
reader: MetricReader{ |
| 185 |
Periodic: &PeriodicMetricReader{ |
| 186 |
Exporter: PushMetricExporter{ |
| 187 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 188 |
Endpoint: ptr("http://localhost:4318"), |
| 189 |
Compression: ptr("gzip"), |
| 190 |
Timeout: ptr(1000), |
| 191 |
Headers: []NameStringValuePair{ |
| 192 |
{Name: "test", Value: ptr("test1")}, |
| 193 |
}, |
| 194 |
}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
}, |
| 198 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 199 |
}, |
| 200 |
{ |
| 201 |
name: "periodic/otlp-grpc-exporter-with-path", |
| 202 |
reader: MetricReader{ |
| 203 |
Periodic: &PeriodicMetricReader{ |
| 204 |
Exporter: PushMetricExporter{ |
| 205 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 206 |
Endpoint: ptr("http://localhost:4318/path/123"), |
| 207 |
Compression: ptr("gzip"), |
| 208 |
Timeout: ptr(1000), |
| 209 |
Headers: []NameStringValuePair{ |
| 210 |
{Name: "test", Value: ptr("test1")}, |
| 211 |
}, |
| 212 |
}, |
| 213 |
}, |
| 214 |
}, |
| 215 |
}, |
| 216 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "periodic/otlp-grpc-good-ca-certificate", |
| 220 |
reader: MetricReader{ |
| 221 |
Periodic: &PeriodicMetricReader{ |
| 222 |
Exporter: PushMetricExporter{ |
| 223 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 224 |
Endpoint: ptr("https://localhost:4317"), |
| 225 |
Compression: ptr("gzip"), |
| 226 |
Timeout: ptr(1000), |
| 227 |
Tls: &GrpcTls{ |
| 228 |
CaFile: ptr(filepath.Join("testdata", "ca.crt")), |
| 229 |
}, |
| 230 |
}, |
| 231 |
}, |
| 232 |
}, |
| 233 |
}, |
| 234 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 235 |
}, |
| 236 |
{ |
| 237 |
name: "periodic/otlp-grpc-bad-ca-certificate", |
| 238 |
reader: MetricReader{ |
| 239 |
Periodic: &PeriodicMetricReader{ |
| 240 |
Exporter: PushMetricExporter{ |
| 241 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 242 |
Endpoint: ptr("https://localhost:4317"), |
| 243 |
Compression: ptr("gzip"), |
| 244 |
Timeout: ptr(1000), |
| 245 |
Tls: &GrpcTls{ |
| 246 |
CaFile: ptr(filepath.Join("testdata", "bad_cert.crt")), |
| 247 |
}, |
| 248 |
}, |
| 249 |
}, |
| 250 |
}, |
| 251 |
}, |
| 252 |
wantErrT: newErrInvalid("tls configuration"), |
| 253 |
}, |
| 254 |
{ |
| 255 |
name: "periodic/otlp-grpc-bad-client-certificate", |
| 256 |
reader: MetricReader{ |
| 257 |
Periodic: &PeriodicMetricReader{ |
| 258 |
Exporter: PushMetricExporter{ |
| 259 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 260 |
Endpoint: ptr("localhost:4317"), |
| 261 |
Compression: ptr("gzip"), |
| 262 |
Timeout: ptr(1000), |
| 263 |
Tls: &GrpcTls{ |
| 264 |
KeyFile: ptr(filepath.Join("testdata", "bad_cert.crt")), |
| 265 |
CertFile: ptr(filepath.Join("testdata", "bad_cert.crt")), |
| 266 |
}, |
| 267 |
}, |
| 268 |
}, |
| 269 |
}, |
| 270 |
}, |
| 271 |
wantErrT: newErrInvalid("tls configuration"), |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "periodic/otlp-grpc-bad-headerslist", |
| 275 |
reader: MetricReader{ |
| 276 |
Periodic: &PeriodicMetricReader{ |
| 277 |
Exporter: PushMetricExporter{ |
| 278 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 279 |
Endpoint: ptr("localhost:4317"), |
| 280 |
Compression: ptr("gzip"), |
| 281 |
Timeout: ptr(1000), |
| 282 |
HeadersList: ptr("==="), |
| 283 |
}, |
| 284 |
}, |
| 285 |
}, |
| 286 |
}, |
| 287 |
wantErrT: newErrInvalid("invalid headers_list"), |
| 288 |
}, |
| 289 |
{ |
| 290 |
name: "periodic/otlp-grpc-exporter-no-endpoint", |
| 291 |
reader: MetricReader{ |
| 292 |
Periodic: &PeriodicMetricReader{ |
| 293 |
Exporter: PushMetricExporter{ |
| 294 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 295 |
Compression: ptr("gzip"), |
| 296 |
Timeout: ptr(1000), |
| 297 |
Headers: []NameStringValuePair{ |
| 298 |
{Name: "test", Value: ptr("test1")}, |
| 299 |
}, |
| 300 |
}, |
| 301 |
}, |
| 302 |
}, |
| 303 |
}, |
| 304 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: "periodic/otlp-grpc-exporter-socket-endpoint", |
| 308 |
reader: MetricReader{ |
| 309 |
Periodic: &PeriodicMetricReader{ |
| 310 |
Exporter: PushMetricExporter{ |
| 311 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 312 |
Endpoint: ptr("unix:collector.sock"), |
| 313 |
Compression: ptr("gzip"), |
| 314 |
Timeout: ptr(1000), |
| 315 |
Headers: []NameStringValuePair{ |
| 316 |
{Name: "test", Value: ptr("test1")}, |
| 317 |
}, |
| 318 |
}, |
| 319 |
}, |
| 320 |
}, |
| 321 |
}, |
| 322 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 323 |
}, |
| 324 |
{ |
| 325 |
name: "periodic/otlp-grpc-exporter-no-scheme", |
| 326 |
reader: MetricReader{ |
| 327 |
Periodic: &PeriodicMetricReader{ |
| 328 |
Exporter: PushMetricExporter{ |
| 329 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 330 |
Endpoint: ptr("localhost:4318"), |
| 331 |
Compression: ptr("gzip"), |
| 332 |
Timeout: ptr(1000), |
| 333 |
Headers: []NameStringValuePair{ |
| 334 |
{Name: "test", Value: ptr("test1")}, |
| 335 |
}, |
| 336 |
}, |
| 337 |
}, |
| 338 |
}, |
| 339 |
}, |
| 340 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 341 |
}, |
| 342 |
{ |
| 343 |
name: "periodic/otlp-grpc-invalid-endpoint", |
| 344 |
reader: MetricReader{ |
| 345 |
Periodic: &PeriodicMetricReader{ |
| 346 |
Exporter: PushMetricExporter{ |
| 347 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 348 |
Endpoint: ptr(" "), |
| 349 |
Compression: ptr("gzip"), |
| 350 |
Timeout: ptr(1000), |
| 351 |
Headers: []NameStringValuePair{ |
| 352 |
{Name: "test", Value: ptr("test1")}, |
| 353 |
}, |
| 354 |
}, |
| 355 |
}, |
| 356 |
}, |
| 357 |
}, |
| 358 |
wantErrT: newErrInvalid("endpoint parsing failed"), |
| 359 |
}, |
| 360 |
{ |
| 361 |
name: "periodic/otlp-grpc-none-compression", |
| 362 |
reader: MetricReader{ |
| 363 |
Periodic: &PeriodicMetricReader{ |
| 364 |
Exporter: PushMetricExporter{ |
| 365 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 366 |
Endpoint: ptr("localhost:4318"), |
| 367 |
Compression: ptr("none"), |
| 368 |
Timeout: ptr(1000), |
| 369 |
Headers: []NameStringValuePair{ |
| 370 |
{Name: "test", Value: ptr("test1")}, |
| 371 |
}, |
| 372 |
}, |
| 373 |
}, |
| 374 |
}, |
| 375 |
}, |
| 376 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 377 |
}, |
| 378 |
{ |
| 379 |
name: "periodic/otlp-grpc-delta-temporality", |
| 380 |
reader: MetricReader{ |
| 381 |
Periodic: &PeriodicMetricReader{ |
| 382 |
Exporter: PushMetricExporter{ |
| 383 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 384 |
Endpoint: ptr("localhost:4318"), |
| 385 |
Compression: ptr("none"), |
| 386 |
Timeout: ptr(1000), |
| 387 |
Headers: []NameStringValuePair{ |
| 388 |
{Name: "test", Value: ptr("test1")}, |
| 389 |
}, |
| 390 |
TemporalityPreference: ptr(ExporterTemporalityPreferenceDelta), |
| 391 |
}, |
| 392 |
}, |
| 393 |
}, |
| 394 |
}, |
| 395 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 396 |
}, |
| 397 |
{ |
| 398 |
name: "periodic/otlp-grpc-cumulative-temporality", |
| 399 |
reader: MetricReader{ |
| 400 |
Periodic: &PeriodicMetricReader{ |
| 401 |
Exporter: PushMetricExporter{ |
| 402 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 403 |
Endpoint: ptr("localhost:4318"), |
| 404 |
Compression: ptr("none"), |
| 405 |
Timeout: ptr(1000), |
| 406 |
Headers: []NameStringValuePair{ |
| 407 |
{Name: "test", Value: ptr("test1")}, |
| 408 |
}, |
| 409 |
TemporalityPreference: ptr(ExporterTemporalityPreferenceCumulative), |
| 410 |
}, |
| 411 |
}, |
| 412 |
}, |
| 413 |
}, |
| 414 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "periodic/otlp-grpc-lowmemory-temporality", |
| 418 |
reader: MetricReader{ |
| 419 |
Periodic: &PeriodicMetricReader{ |
| 420 |
Exporter: PushMetricExporter{ |
| 421 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 422 |
Endpoint: ptr("localhost:4318"), |
| 423 |
Compression: ptr("none"), |
| 424 |
Timeout: ptr(1000), |
| 425 |
Headers: []NameStringValuePair{ |
| 426 |
{Name: "test", Value: ptr("test1")}, |
| 427 |
}, |
| 428 |
TemporalityPreference: ptr(ExporterTemporalityPreferenceLowMemory), |
| 429 |
}, |
| 430 |
}, |
| 431 |
}, |
| 432 |
}, |
| 433 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 434 |
}, |
| 435 |
{ |
| 436 |
name: "periodic/otlp-grpc-invalid-temporality", |
| 437 |
reader: MetricReader{ |
| 438 |
Periodic: &PeriodicMetricReader{ |
| 439 |
Exporter: PushMetricExporter{ |
| 440 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 441 |
Endpoint: ptr("localhost:4318"), |
| 442 |
Compression: ptr("none"), |
| 443 |
Timeout: ptr(1000), |
| 444 |
Headers: []NameStringValuePair{ |
| 445 |
{Name: "test", Value: ptr("test1")}, |
| 446 |
}, |
| 447 |
TemporalityPreference: (*ExporterTemporalityPreference)(ptr("invalid")), |
| 448 |
}, |
| 449 |
}, |
| 450 |
}, |
| 451 |
}, |
| 452 |
wantErrT: newErrInvalid("unsupported temporality preference \"invalid\""), |
| 453 |
}, |
| 454 |
{ |
| 455 |
name: "periodic/otlp-grpc-invalid-compression", |
| 456 |
reader: MetricReader{ |
| 457 |
Periodic: &PeriodicMetricReader{ |
| 458 |
Exporter: PushMetricExporter{ |
| 459 |
OTLPGrpc: &OTLPGrpcMetricExporter{ |
| 460 |
Endpoint: ptr("localhost:4318"), |
| 461 |
Compression: ptr("invalid"), |
| 462 |
Timeout: ptr(1000), |
| 463 |
Headers: []NameStringValuePair{ |
| 464 |
{Name: "test", Value: ptr("test1")}, |
| 465 |
}, |
| 466 |
}, |
| 467 |
}, |
| 468 |
}, |
| 469 |
}, |
| 470 |
wantErrT: newErrInvalid("unsupported compression \"invalid\""), |
| 471 |
}, |
| 472 |
{ |
| 473 |
name: "periodic/otlp-http-exporter", |
| 474 |
reader: MetricReader{ |
| 475 |
Periodic: &PeriodicMetricReader{ |
| 476 |
Exporter: PushMetricExporter{ |
| 477 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 478 |
Endpoint: ptr("http://localhost:4318"), |
| 479 |
Compression: ptr("gzip"), |
| 480 |
Timeout: ptr(1000), |
| 481 |
Headers: []NameStringValuePair{ |
| 482 |
{Name: "test", Value: ptr("test1")}, |
| 483 |
}, |
| 484 |
}, |
| 485 |
}, |
| 486 |
}, |
| 487 |
}, |
| 488 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 489 |
}, |
| 490 |
{ |
| 491 |
name: "periodic/otlp-http-good-ca-certificate", |
| 492 |
reader: MetricReader{ |
| 493 |
Periodic: &PeriodicMetricReader{ |
| 494 |
Exporter: PushMetricExporter{ |
| 495 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 496 |
Endpoint: ptr("https://localhost:4317"), |
| 497 |
Compression: ptr("gzip"), |
| 498 |
Timeout: ptr(1000), |
| 499 |
Tls: &HttpTls{ |
| 500 |
CaFile: ptr(filepath.Join("testdata", "ca.crt")), |
| 501 |
}, |
| 502 |
}, |
| 503 |
}, |
| 504 |
}, |
| 505 |
}, |
| 506 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 507 |
}, |
| 508 |
{ |
| 509 |
name: "periodic/otlp-http-bad-ca-certificate", |
| 510 |
reader: MetricReader{ |
| 511 |
Periodic: &PeriodicMetricReader{ |
| 512 |
Exporter: PushMetricExporter{ |
| 513 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 514 |
Endpoint: ptr("https://localhost:4317"), |
| 515 |
Compression: ptr("gzip"), |
| 516 |
Timeout: ptr(1000), |
| 517 |
Tls: &HttpTls{ |
| 518 |
CaFile: ptr(filepath.Join("testdata", "bad_cert.crt")), |
| 519 |
}, |
| 520 |
}, |
| 521 |
}, |
| 522 |
}, |
| 523 |
}, |
| 524 |
wantErrT: newErrInvalid("tls configuration"), |
| 525 |
}, |
| 526 |
{ |
| 527 |
name: "periodic/otlp-http-bad-client-certificate", |
| 528 |
reader: MetricReader{ |
| 529 |
Periodic: &PeriodicMetricReader{ |
| 530 |
Exporter: PushMetricExporter{ |
| 531 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 532 |
Endpoint: ptr("localhost:4317"), |
| 533 |
Compression: ptr("gzip"), |
| 534 |
Timeout: ptr(1000), |
| 535 |
Tls: &HttpTls{ |
| 536 |
KeyFile: ptr(filepath.Join("testdata", "bad_cert.crt")), |
| 537 |
CertFile: ptr(filepath.Join("testdata", "bad_cert.crt")), |
| 538 |
}, |
| 539 |
}, |
| 540 |
}, |
| 541 |
}, |
| 542 |
}, |
| 543 |
wantErrT: newErrInvalid("tls configuration"), |
| 544 |
}, |
| 545 |
{ |
| 546 |
name: "periodic/otlp-http-bad-headerslist", |
| 547 |
reader: MetricReader{ |
| 548 |
Periodic: &PeriodicMetricReader{ |
| 549 |
Exporter: PushMetricExporter{ |
| 550 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 551 |
Endpoint: ptr("localhost:4317"), |
| 552 |
Compression: ptr("gzip"), |
| 553 |
Timeout: ptr(1000), |
| 554 |
HeadersList: ptr("==="), |
| 555 |
}, |
| 556 |
}, |
| 557 |
}, |
| 558 |
}, |
| 559 |
wantErrT: newErrInvalid("invalid headers_list"), |
| 560 |
}, |
| 561 |
{ |
| 562 |
name: "periodic/otlp-http-exporter-with-path", |
| 563 |
reader: MetricReader{ |
| 564 |
Periodic: &PeriodicMetricReader{ |
| 565 |
Exporter: PushMetricExporter{ |
| 566 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 567 |
Endpoint: ptr("http://localhost:4318/path/123"), |
| 568 |
Compression: ptr("gzip"), |
| 569 |
Timeout: ptr(1000), |
| 570 |
Headers: []NameStringValuePair{ |
| 571 |
{Name: "test", Value: ptr("test1")}, |
| 572 |
}, |
| 573 |
}, |
| 574 |
}, |
| 575 |
}, |
| 576 |
}, |
| 577 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 578 |
}, |
| 579 |
{ |
| 580 |
name: "periodic/otlp-http-exporter-no-endpoint", |
| 581 |
reader: MetricReader{ |
| 582 |
Periodic: &PeriodicMetricReader{ |
| 583 |
Exporter: PushMetricExporter{ |
| 584 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 585 |
Compression: ptr("gzip"), |
| 586 |
Timeout: ptr(1000), |
| 587 |
Headers: []NameStringValuePair{ |
| 588 |
{Name: "test", Value: ptr("test1")}, |
| 589 |
}, |
| 590 |
}, |
| 591 |
}, |
| 592 |
}, |
| 593 |
}, |
| 594 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 595 |
}, |
| 596 |
{ |
| 597 |
name: "periodic/otlp-http-exporter-no-scheme", |
| 598 |
reader: MetricReader{ |
| 599 |
Periodic: &PeriodicMetricReader{ |
| 600 |
Exporter: PushMetricExporter{ |
| 601 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 602 |
Endpoint: ptr("localhost:4318"), |
| 603 |
Compression: ptr("gzip"), |
| 604 |
Timeout: ptr(1000), |
| 605 |
Headers: []NameStringValuePair{ |
| 606 |
{Name: "test", Value: ptr("test1")}, |
| 607 |
}, |
| 608 |
}, |
| 609 |
}, |
| 610 |
}, |
| 611 |
}, |
| 612 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 613 |
}, |
| 614 |
{ |
| 615 |
name: "periodic/otlp-http-invalid-endpoint", |
| 616 |
reader: MetricReader{ |
| 617 |
Periodic: &PeriodicMetricReader{ |
| 618 |
Exporter: PushMetricExporter{ |
| 619 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 620 |
Endpoint: ptr(" "), |
| 621 |
Compression: ptr("gzip"), |
| 622 |
Timeout: ptr(1000), |
| 623 |
Headers: []NameStringValuePair{ |
| 624 |
{Name: "test", Value: ptr("test1")}, |
| 625 |
}, |
| 626 |
}, |
| 627 |
}, |
| 628 |
}, |
| 629 |
}, |
| 630 |
wantErrT: newErrInvalid("endpoint parsing failed"), |
| 631 |
}, |
| 632 |
{ |
| 633 |
name: "periodic/otlp-http-none-compression", |
| 634 |
reader: MetricReader{ |
| 635 |
Periodic: &PeriodicMetricReader{ |
| 636 |
Exporter: PushMetricExporter{ |
| 637 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 638 |
Endpoint: ptr("localhost:4318"), |
| 639 |
Compression: ptr("none"), |
| 640 |
Timeout: ptr(1000), |
| 641 |
Headers: []NameStringValuePair{ |
| 642 |
{Name: "test", Value: ptr("test1")}, |
| 643 |
}, |
| 644 |
}, |
| 645 |
}, |
| 646 |
}, |
| 647 |
}, |
| 648 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 649 |
}, |
| 650 |
{ |
| 651 |
name: "periodic/otlp-http-cumulative-temporality", |
| 652 |
reader: MetricReader{ |
| 653 |
Periodic: &PeriodicMetricReader{ |
| 654 |
Exporter: PushMetricExporter{ |
| 655 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 656 |
Endpoint: ptr("localhost:4318"), |
| 657 |
Compression: ptr("none"), |
| 658 |
Timeout: ptr(1000), |
| 659 |
Headers: []NameStringValuePair{ |
| 660 |
{Name: "test", Value: ptr("test1")}, |
| 661 |
}, |
| 662 |
TemporalityPreference: ptr(ExporterTemporalityPreferenceCumulative), |
| 663 |
}, |
| 664 |
}, |
| 665 |
}, |
| 666 |
}, |
| 667 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 668 |
}, |
| 669 |
{ |
| 670 |
name: "periodic/otlp-http-lowmemory-temporality", |
| 671 |
reader: MetricReader{ |
| 672 |
Periodic: &PeriodicMetricReader{ |
| 673 |
Exporter: PushMetricExporter{ |
| 674 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 675 |
Endpoint: ptr("localhost:4318"), |
| 676 |
Compression: ptr("none"), |
| 677 |
Timeout: ptr(1000), |
| 678 |
Headers: []NameStringValuePair{ |
| 679 |
{Name: "test", Value: ptr("test1")}, |
| 680 |
}, |
| 681 |
TemporalityPreference: ptr(ExporterTemporalityPreferenceLowMemory), |
| 682 |
}, |
| 683 |
}, |
| 684 |
}, |
| 685 |
}, |
| 686 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 687 |
}, |
| 688 |
{ |
| 689 |
name: "periodic/otlp-http-delta-temporality", |
| 690 |
reader: MetricReader{ |
| 691 |
Periodic: &PeriodicMetricReader{ |
| 692 |
Exporter: PushMetricExporter{ |
| 693 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 694 |
Endpoint: ptr("localhost:4318"), |
| 695 |
Compression: ptr("none"), |
| 696 |
Timeout: ptr(1000), |
| 697 |
Headers: []NameStringValuePair{ |
| 698 |
{Name: "test", Value: ptr("test1")}, |
| 699 |
}, |
| 700 |
TemporalityPreference: ptr(ExporterTemporalityPreferenceDelta), |
| 701 |
}, |
| 702 |
}, |
| 703 |
}, |
| 704 |
}, |
| 705 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 706 |
}, |
| 707 |
{ |
| 708 |
name: "periodic/otlp-http-invalid-temporality", |
| 709 |
reader: MetricReader{ |
| 710 |
Periodic: &PeriodicMetricReader{ |
| 711 |
Exporter: PushMetricExporter{ |
| 712 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 713 |
Endpoint: ptr("localhost:4318"), |
| 714 |
Compression: ptr("none"), |
| 715 |
Timeout: ptr(1000), |
| 716 |
Headers: []NameStringValuePair{ |
| 717 |
{Name: "test", Value: ptr("test1")}, |
| 718 |
}, |
| 719 |
TemporalityPreference: (*ExporterTemporalityPreference)(ptr("invalid")), |
| 720 |
}, |
| 721 |
}, |
| 722 |
}, |
| 723 |
}, |
| 724 |
wantErrT: newErrInvalid("unsupported temporality preference \"invalid\""), |
| 725 |
}, |
| 726 |
{ |
| 727 |
name: "periodic/otlp-http-invalid-compression", |
| 728 |
reader: MetricReader{ |
| 729 |
Periodic: &PeriodicMetricReader{ |
| 730 |
Exporter: PushMetricExporter{ |
| 731 |
OTLPHttp: &OTLPHttpMetricExporter{ |
| 732 |
Endpoint: ptr("localhost:4318"), |
| 733 |
Compression: ptr("invalid"), |
| 734 |
Timeout: ptr(1000), |
| 735 |
Headers: []NameStringValuePair{ |
| 736 |
{Name: "test", Value: ptr("test1")}, |
| 737 |
}, |
| 738 |
}, |
| 739 |
}, |
| 740 |
}, |
| 741 |
}, |
| 742 |
wantErrT: newErrInvalid("unsupported compression \"invalid\""), |
| 743 |
}, |
| 744 |
{ |
| 745 |
name: "periodic/no-exporter", |
| 746 |
reader: MetricReader{ |
| 747 |
Periodic: &PeriodicMetricReader{ |
| 748 |
Exporter: PushMetricExporter{}, |
| 749 |
}, |
| 750 |
}, |
| 751 |
wantErrT: newErrInvalid("no valid metric exporter"), |
| 752 |
}, |
| 753 |
{ |
| 754 |
name: "periodic/console-exporter", |
| 755 |
reader: MetricReader{ |
| 756 |
Periodic: &PeriodicMetricReader{ |
| 757 |
Exporter: PushMetricExporter{ |
| 758 |
Console: &ConsoleMetricExporter{}, |
| 759 |
}, |
| 760 |
}, |
| 761 |
}, |
| 762 |
wantReader: sdkmetric.NewPeriodicReader(consoleExporter), |
| 763 |
}, |
| 764 |
{ |
| 765 |
name: "periodic/console-exporter-with-extra-options", |
| 766 |
reader: MetricReader{ |
| 767 |
Periodic: &PeriodicMetricReader{ |
| 768 |
Interval: ptr(30_000), |
| 769 |
Timeout: ptr(5_000), |
| 770 |
Exporter: PushMetricExporter{ |
| 771 |
Console: &ConsoleMetricExporter{}, |
| 772 |
}, |
| 773 |
}, |
| 774 |
}, |
| 775 |
wantReader: sdkmetric.NewPeriodicReader( |
| 776 |
consoleExporter, |
| 777 |
sdkmetric.WithInterval(30_000*time.Millisecond), |
| 778 |
sdkmetric.WithTimeout(5_000*time.Millisecond), |
| 779 |
), |
| 780 |
}, |
| 781 |
} |
| 782 |
for _, tt := range testCases { |
| 783 |
t.Run(tt.name, func(t *testing.T) { |
| 784 |
got, err := metricReader(t.Context(), tt.reader) |
| 785 |
require.ErrorIs(t, err, tt.wantErrT) |
| 786 |
if tt.wantReader == nil { |
| 787 |
require.Nil(t, got) |
| 788 |
} else { |
| 789 |
require.Equal(t, reflect.TypeOf(tt.wantReader), reflect.TypeOf(got)) |
| 790 |
var fieldName string |
| 791 |
switch reflect.TypeOf(tt.wantReader).String() { |
| 792 |
case "*metric.PeriodicReader": |
| 793 |
fieldName = "exporter" |
| 794 |
case "otelconf.readerWithServer": |
| 795 |
fieldName = "Reader" |
| 796 |
default: |
| 797 |
fieldName = "e" |
| 798 |
} |
| 799 |
wantExporterType := reflect.Indirect(reflect.ValueOf(tt.wantReader)).FieldByName(fieldName).Elem().Type() |
| 800 |
gotExporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName(fieldName).Elem().Type() |
| 801 |
require.Equal(t, wantExporterType.String(), gotExporterType.String()) |
| 802 |
require.NoError(t, got.Shutdown(t.Context())) |
| 803 |
} |
| 804 |
}) |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
func TestView(t *testing.T) { |
| 809 |
testCases := []struct { |
| 810 |
name string |
| 811 |
view View |
| 812 |
args any |
| 813 |
wantErr string |
| 814 |
matchInstrument *sdkmetric.Instrument |
| 815 |
wantStream sdkmetric.Stream |
| 816 |
wantResult bool |
| 817 |
}{ |
| 818 |
{ |
| 819 |
name: "selector/invalid_type", |
| 820 |
view: View{ |
| 821 |
Selector: ViewSelector{ |
| 822 |
InstrumentType: (*InstrumentType)(ptr("invalid_type")), |
| 823 |
}, |
| 824 |
}, |
| 825 |
wantErr: "view_selector: instrument_type: invalid value", |
| 826 |
}, |
| 827 |
{ |
| 828 |
name: "selector/invalid_type", |
| 829 |
view: View{ |
| 830 |
Selector: ViewSelector{}, |
| 831 |
}, |
| 832 |
wantErr: "view_selector: empty selector not supporter", |
| 833 |
}, |
| 834 |
{ |
| 835 |
name: "all selectors match", |
| 836 |
view: View{ |
| 837 |
Selector: ViewSelector{ |
| 838 |
InstrumentName: ptr("test_name"), |
| 839 |
InstrumentType: ptr(InstrumentTypeCounter), |
| 840 |
Unit: ptr("test_unit"), |
| 841 |
MeterName: ptr("test_meter_name"), |
| 842 |
MeterVersion: ptr("test_meter_version"), |
| 843 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 844 |
}, |
| 845 |
}, |
| 846 |
matchInstrument: &sdkmetric.Instrument{ |
| 847 |
Name: "test_name", |
| 848 |
Unit: "test_unit", |
| 849 |
Kind: sdkmetric.InstrumentKindCounter, |
| 850 |
Scope: instrumentation.Scope{ |
| 851 |
Name: "test_meter_name", |
| 852 |
Version: "test_meter_version", |
| 853 |
SchemaURL: "test_schema_url", |
| 854 |
}, |
| 855 |
}, |
| 856 |
wantStream: sdkmetric.Stream{Name: "test_name", Unit: "test_unit"}, |
| 857 |
wantResult: true, |
| 858 |
}, |
| 859 |
{ |
| 860 |
name: "all selectors no match name", |
| 861 |
view: View{ |
| 862 |
Selector: ViewSelector{ |
| 863 |
InstrumentName: ptr("test_name"), |
| 864 |
InstrumentType: ptr(InstrumentTypeCounter), |
| 865 |
Unit: ptr("test_unit"), |
| 866 |
MeterName: ptr("test_meter_name"), |
| 867 |
MeterVersion: ptr("test_meter_version"), |
| 868 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 869 |
}, |
| 870 |
}, |
| 871 |
matchInstrument: &sdkmetric.Instrument{ |
| 872 |
Name: "not_match", |
| 873 |
Unit: "test_unit", |
| 874 |
Kind: sdkmetric.InstrumentKindCounter, |
| 875 |
Scope: instrumentation.Scope{ |
| 876 |
Name: "test_meter_name", |
| 877 |
Version: "test_meter_version", |
| 878 |
SchemaURL: "test_schema_url", |
| 879 |
}, |
| 880 |
}, |
| 881 |
wantStream: sdkmetric.Stream{}, |
| 882 |
wantResult: false, |
| 883 |
}, |
| 884 |
{ |
| 885 |
name: "all selectors no match unit", |
| 886 |
view: View{ |
| 887 |
Selector: ViewSelector{ |
| 888 |
InstrumentName: ptr("test_name"), |
| 889 |
InstrumentType: ptr(InstrumentTypeCounter), |
| 890 |
Unit: ptr("test_unit"), |
| 891 |
MeterName: ptr("test_meter_name"), |
| 892 |
MeterVersion: ptr("test_meter_version"), |
| 893 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 894 |
}, |
| 895 |
}, |
| 896 |
matchInstrument: &sdkmetric.Instrument{ |
| 897 |
Name: "test_name", |
| 898 |
Unit: "not_match", |
| 899 |
Kind: sdkmetric.InstrumentKindCounter, |
| 900 |
Scope: instrumentation.Scope{ |
| 901 |
Name: "test_meter_name", |
| 902 |
Version: "test_meter_version", |
| 903 |
SchemaURL: "test_schema_url", |
| 904 |
}, |
| 905 |
}, |
| 906 |
wantStream: sdkmetric.Stream{}, |
| 907 |
wantResult: false, |
| 908 |
}, |
| 909 |
{ |
| 910 |
name: "all selectors no match kind", |
| 911 |
view: View{ |
| 912 |
Selector: ViewSelector{ |
| 913 |
InstrumentName: ptr("test_name"), |
| 914 |
InstrumentType: (*InstrumentType)(ptr("histogram")), |
| 915 |
Unit: ptr("test_unit"), |
| 916 |
MeterName: ptr("test_meter_name"), |
| 917 |
MeterVersion: ptr("test_meter_version"), |
| 918 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 919 |
}, |
| 920 |
}, |
| 921 |
matchInstrument: &sdkmetric.Instrument{ |
| 922 |
Name: "test_name", |
| 923 |
Unit: "test_unit", |
| 924 |
Kind: sdkmetric.InstrumentKindCounter, |
| 925 |
Scope: instrumentation.Scope{ |
| 926 |
Name: "test_meter_name", |
| 927 |
Version: "test_meter_version", |
| 928 |
SchemaURL: "test_schema_url", |
| 929 |
}, |
| 930 |
}, |
| 931 |
wantStream: sdkmetric.Stream{}, |
| 932 |
wantResult: false, |
| 933 |
}, |
| 934 |
{ |
| 935 |
name: "all selectors no match meter name", |
| 936 |
view: View{ |
| 937 |
Selector: ViewSelector{ |
| 938 |
InstrumentName: ptr("test_name"), |
| 939 |
InstrumentType: ptr(InstrumentTypeCounter), |
| 940 |
Unit: ptr("test_unit"), |
| 941 |
MeterName: ptr("test_meter_name"), |
| 942 |
MeterVersion: ptr("test_meter_version"), |
| 943 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 944 |
}, |
| 945 |
}, |
| 946 |
matchInstrument: &sdkmetric.Instrument{ |
| 947 |
Name: "test_name", |
| 948 |
Unit: "test_unit", |
| 949 |
Kind: sdkmetric.InstrumentKindCounter, |
| 950 |
Scope: instrumentation.Scope{ |
| 951 |
Name: "not_match", |
| 952 |
Version: "test_meter_version", |
| 953 |
SchemaURL: "test_schema_url", |
| 954 |
}, |
| 955 |
}, |
| 956 |
wantStream: sdkmetric.Stream{}, |
| 957 |
wantResult: false, |
| 958 |
}, |
| 959 |
{ |
| 960 |
name: "all selectors no match meter version", |
| 961 |
view: View{ |
| 962 |
Selector: ViewSelector{ |
| 963 |
InstrumentName: ptr("test_name"), |
| 964 |
InstrumentType: ptr(InstrumentTypeCounter), |
| 965 |
Unit: ptr("test_unit"), |
| 966 |
MeterName: ptr("test_meter_name"), |
| 967 |
MeterVersion: ptr("test_meter_version"), |
| 968 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 969 |
}, |
| 970 |
}, |
| 971 |
matchInstrument: &sdkmetric.Instrument{ |
| 972 |
Name: "test_name", |
| 973 |
Unit: "test_unit", |
| 974 |
Kind: sdkmetric.InstrumentKindCounter, |
| 975 |
Scope: instrumentation.Scope{ |
| 976 |
Name: "test_meter_name", |
| 977 |
Version: "not_match", |
| 978 |
SchemaURL: "test_schema_url", |
| 979 |
}, |
| 980 |
}, |
| 981 |
wantStream: sdkmetric.Stream{}, |
| 982 |
wantResult: false, |
| 983 |
}, |
| 984 |
{ |
| 985 |
name: "all selectors no match meter schema url", |
| 986 |
view: View{ |
| 987 |
Selector: ViewSelector{ |
| 988 |
InstrumentName: ptr("test_name"), |
| 989 |
InstrumentType: ptr(InstrumentTypeCounter), |
| 990 |
Unit: ptr("test_unit"), |
| 991 |
MeterName: ptr("test_meter_name"), |
| 992 |
MeterVersion: ptr("test_meter_version"), |
| 993 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 994 |
}, |
| 995 |
}, |
| 996 |
matchInstrument: &sdkmetric.Instrument{ |
| 997 |
Name: "test_name", |
| 998 |
Unit: "test_unit", |
| 999 |
Kind: sdkmetric.InstrumentKindCounter, |
| 1000 |
Scope: instrumentation.Scope{ |
| 1001 |
Name: "test_meter_name", |
| 1002 |
Version: "test_meter_version", |
| 1003 |
SchemaURL: "not_match", |
| 1004 |
}, |
| 1005 |
}, |
| 1006 |
wantStream: sdkmetric.Stream{}, |
| 1007 |
wantResult: false, |
| 1008 |
}, |
| 1009 |
{ |
| 1010 |
name: "with stream", |
| 1011 |
view: View{ |
| 1012 |
Selector: ViewSelector{ |
| 1013 |
InstrumentName: ptr("test_name"), |
| 1014 |
Unit: ptr("test_unit"), |
| 1015 |
}, |
| 1016 |
Stream: ViewStream{ |
| 1017 |
Name: ptr("new_name"), |
| 1018 |
Description: ptr("new_description"), |
| 1019 |
AttributeKeys: ptr(IncludeExclude{Included: []string{"foo", "bar"}}), |
| 1020 |
Aggregation: &Aggregation{Sum: make(SumAggregation)}, |
| 1021 |
}, |
| 1022 |
}, |
| 1023 |
matchInstrument: &sdkmetric.Instrument{ |
| 1024 |
Name: "test_name", |
| 1025 |
Description: "test_description", |
| 1026 |
Unit: "test_unit", |
| 1027 |
}, |
| 1028 |
wantStream: sdkmetric.Stream{ |
| 1029 |
Name: "new_name", |
| 1030 |
Description: "new_description", |
| 1031 |
Unit: "test_unit", |
| 1032 |
Aggregation: sdkmetric.AggregationSum{}, |
| 1033 |
}, |
| 1034 |
wantResult: true, |
| 1035 |
}, |
| 1036 |
} |
| 1037 |
for _, tt := range testCases { |
| 1038 |
t.Run(tt.name, func(t *testing.T) { |
| 1039 |
got, err := view(tt.view) |
| 1040 |
if tt.wantErr != "" { |
| 1041 |
require.EqualError(t, err, tt.wantErr) |
| 1042 |
require.Nil(t, got) |
| 1043 |
} else { |
| 1044 |
require.NoError(t, err) |
| 1045 |
gotStream, gotResult := got(*tt.matchInstrument) |
| 1046 |
// Remove filter, since it cannot be compared |
| 1047 |
gotStream.AttributeFilter = nil |
| 1048 |
require.Equal(t, tt.wantStream, gotStream) |
| 1049 |
require.Equal(t, tt.wantResult, gotResult) |
| 1050 |
} |
| 1051 |
}) |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
func TestInstrumentType(t *testing.T) { |
| 1056 |
testCases := []struct { |
| 1057 |
name string |
| 1058 |
instType *InstrumentType |
| 1059 |
wantErr error |
| 1060 |
wantKind sdkmetric.InstrumentKind |
| 1061 |
}{ |
| 1062 |
{ |
| 1063 |
name: "nil", |
| 1064 |
wantKind: sdkmetric.InstrumentKind(0), |
| 1065 |
}, |
| 1066 |
{ |
| 1067 |
name: "counter", |
| 1068 |
instType: ptr(InstrumentTypeCounter), |
| 1069 |
wantKind: sdkmetric.InstrumentKindCounter, |
| 1070 |
}, |
| 1071 |
{ |
| 1072 |
name: "up_down_counter", |
| 1073 |
instType: ptr(InstrumentTypeUpDownCounter), |
| 1074 |
wantKind: sdkmetric.InstrumentKindUpDownCounter, |
| 1075 |
}, |
| 1076 |
{ |
| 1077 |
name: "histogram", |
| 1078 |
instType: ptr(InstrumentTypeHistogram), |
| 1079 |
wantKind: sdkmetric.InstrumentKindHistogram, |
| 1080 |
}, |
| 1081 |
{ |
| 1082 |
name: "observable_counter", |
| 1083 |
instType: ptr(InstrumentTypeObservableCounter), |
| 1084 |
wantKind: sdkmetric.InstrumentKindObservableCounter, |
| 1085 |
}, |
| 1086 |
{ |
| 1087 |
name: "observable_up_down_counter", |
| 1088 |
instType: ptr(InstrumentTypeObservableUpDownCounter), |
| 1089 |
wantKind: sdkmetric.InstrumentKindObservableUpDownCounter, |
| 1090 |
}, |
| 1091 |
{ |
| 1092 |
name: "observable_gauge", |
| 1093 |
instType: ptr(InstrumentTypeObservableGauge), |
| 1094 |
wantKind: sdkmetric.InstrumentKindObservableGauge, |
| 1095 |
}, |
| 1096 |
{ |
| 1097 |
name: "invalid", |
| 1098 |
instType: (*InstrumentType)(ptr("invalid")), |
| 1099 |
wantErr: errors.New("instrument_type: invalid value"), |
| 1100 |
}, |
| 1101 |
} |
| 1102 |
for _, tt := range testCases { |
| 1103 |
t.Run(tt.name, func(t *testing.T) { |
| 1104 |
got, err := instrumentKind(tt.instType) |
| 1105 |
if tt.wantErr != nil { |
| 1106 |
require.Equal(t, tt.wantErr, err) |
| 1107 |
require.Zero(t, got) |
| 1108 |
} else { |
| 1109 |
require.NoError(t, err) |
| 1110 |
require.Equal(t, tt.wantKind, got) |
| 1111 |
} |
| 1112 |
}) |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
func TestAggregation(t *testing.T) { |
| 1117 |
testCases := []struct { |
| 1118 |
name string |
| 1119 |
aggregation *Aggregation |
| 1120 |
wantAggregation sdkmetric.Aggregation |
| 1121 |
}{ |
| 1122 |
{ |
| 1123 |
name: "nil", |
| 1124 |
wantAggregation: nil, |
| 1125 |
}, |
| 1126 |
{ |
| 1127 |
name: "empty", |
| 1128 |
aggregation: &Aggregation{}, |
| 1129 |
wantAggregation: nil, |
| 1130 |
}, |
| 1131 |
{ |
| 1132 |
name: "Base2ExponentialBucketHistogram empty", |
| 1133 |
aggregation: &Aggregation{ |
| 1134 |
Base2ExponentialBucketHistogram: &Base2ExponentialBucketHistogramAggregation{}, |
| 1135 |
}, |
| 1136 |
wantAggregation: sdkmetric.AggregationBase2ExponentialHistogram{ |
| 1137 |
MaxSize: 0, |
| 1138 |
MaxScale: 0, |
| 1139 |
NoMinMax: true, |
| 1140 |
}, |
| 1141 |
}, |
| 1142 |
{ |
| 1143 |
name: "Base2ExponentialBucketHistogram", |
| 1144 |
aggregation: &Aggregation{ |
| 1145 |
Base2ExponentialBucketHistogram: &Base2ExponentialBucketHistogramAggregation{ |
| 1146 |
MaxSize: ptr(2), |
| 1147 |
MaxScale: ptr(3), |
| 1148 |
RecordMinMax: ptr(true), |
| 1149 |
}, |
| 1150 |
}, |
| 1151 |
wantAggregation: sdkmetric.AggregationBase2ExponentialHistogram{ |
| 1152 |
MaxSize: 2, |
| 1153 |
MaxScale: 3, |
| 1154 |
NoMinMax: false, |
| 1155 |
}, |
| 1156 |
}, |
| 1157 |
{ |
| 1158 |
name: "Default", |
| 1159 |
aggregation: &Aggregation{ |
| 1160 |
Default: make(DefaultAggregation), |
| 1161 |
}, |
| 1162 |
wantAggregation: nil, |
| 1163 |
}, |
| 1164 |
{ |
| 1165 |
name: "Drop", |
| 1166 |
aggregation: &Aggregation{ |
| 1167 |
Drop: make(DropAggregation), |
| 1168 |
}, |
| 1169 |
wantAggregation: sdkmetric.AggregationDrop{}, |
| 1170 |
}, |
| 1171 |
{ |
| 1172 |
name: "ExplicitBucketHistogram empty", |
| 1173 |
aggregation: &Aggregation{ |
| 1174 |
ExplicitBucketHistogram: &ExplicitBucketHistogramAggregation{}, |
| 1175 |
}, |
| 1176 |
wantAggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 1177 |
Boundaries: nil, |
| 1178 |
NoMinMax: true, |
| 1179 |
}, |
| 1180 |
}, |
| 1181 |
{ |
| 1182 |
name: "ExplicitBucketHistogram", |
| 1183 |
aggregation: &Aggregation{ |
| 1184 |
ExplicitBucketHistogram: &ExplicitBucketHistogramAggregation{ |
| 1185 |
Boundaries: []float64{1, 2, 3}, |
| 1186 |
RecordMinMax: ptr(true), |
| 1187 |
}, |
| 1188 |
}, |
| 1189 |
wantAggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 1190 |
Boundaries: []float64{1, 2, 3}, |
| 1191 |
NoMinMax: false, |
| 1192 |
}, |
| 1193 |
}, |
| 1194 |
{ |
| 1195 |
name: "LastValue", |
| 1196 |
aggregation: &Aggregation{ |
| 1197 |
LastValue: make(LastValueAggregation), |
| 1198 |
}, |
| 1199 |
wantAggregation: sdkmetric.AggregationLastValue{}, |
| 1200 |
}, |
| 1201 |
{ |
| 1202 |
name: "Sum", |
| 1203 |
aggregation: &Aggregation{ |
| 1204 |
Sum: make(SumAggregation), |
| 1205 |
}, |
| 1206 |
wantAggregation: sdkmetric.AggregationSum{}, |
| 1207 |
}, |
| 1208 |
} |
| 1209 |
for _, tt := range testCases { |
| 1210 |
t.Run(tt.name, func(t *testing.T) { |
| 1211 |
got := aggregation(tt.aggregation) |
| 1212 |
require.Equal(t, tt.wantAggregation, got) |
| 1213 |
}) |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
func TestNewIncludeExcludeFilter(t *testing.T) { |
| 1218 |
testCases := []struct { |
| 1219 |
name string |
| 1220 |
attributeKeys *IncludeExclude |
| 1221 |
wantPass []string |
| 1222 |
wantFail []string |
| 1223 |
}{ |
| 1224 |
{ |
| 1225 |
name: "empty", |
| 1226 |
attributeKeys: nil, |
| 1227 |
wantPass: []string{"foo", "bar"}, |
| 1228 |
wantFail: nil, |
| 1229 |
}, |
| 1230 |
{ |
| 1231 |
name: "filter-with-include", |
| 1232 |
attributeKeys: ptr(IncludeExclude{ |
| 1233 |
Included: []string{"foo"}, |
| 1234 |
}), |
| 1235 |
wantPass: []string{"foo"}, |
| 1236 |
wantFail: []string{"bar"}, |
| 1237 |
}, |
| 1238 |
{ |
| 1239 |
name: "filter-with-exclude", |
| 1240 |
attributeKeys: ptr(IncludeExclude{ |
| 1241 |
Excluded: []string{"foo"}, |
| 1242 |
}), |
| 1243 |
wantPass: []string{"bar"}, |
| 1244 |
wantFail: []string{"foo"}, |
| 1245 |
}, |
| 1246 |
{ |
| 1247 |
name: "filter-with-include-and-exclude", |
| 1248 |
attributeKeys: ptr(IncludeExclude{ |
| 1249 |
Included: []string{"bar"}, |
| 1250 |
Excluded: []string{"foo"}, |
| 1251 |
}), |
| 1252 |
wantPass: []string{"bar"}, |
| 1253 |
wantFail: []string{"foo"}, |
| 1254 |
}, |
| 1255 |
} |
| 1256 |
for _, tt := range testCases { |
| 1257 |
t.Run(tt.name, func(t *testing.T) { |
| 1258 |
got, err := newIncludeExcludeFilter(tt.attributeKeys) |
| 1259 |
require.NoError(t, err) |
| 1260 |
for _, pass := range tt.wantPass { |
| 1261 |
require.True(t, got(attribute.KeyValue{Key: attribute.Key(pass), Value: attribute.StringValue("")})) |
| 1262 |
} |
| 1263 |
for _, fail := range tt.wantFail { |
| 1264 |
require.False(t, got(attribute.KeyValue{Key: attribute.Key(fail), Value: attribute.StringValue("")})) |
| 1265 |
} |
| 1266 |
}) |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
func TestNewIncludeExcludeFilterError(t *testing.T) { |
| 1271 |
_, err := newIncludeExcludeFilter(ptr(IncludeExclude{ |
| 1272 |
Included: []string{"foo"}, |
| 1273 |
Excluded: []string{"foo"}, |
| 1274 |
})) |
| 1275 |
require.Equal(t, fmt.Errorf("attribute cannot be in both include and exclude list: foo"), err) |
| 1276 |
} |
| 1277 |
|
| 1278 |
func Test_otlpGRPCMetricExporter(t *testing.T) { |
| 1279 |
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { |
| 1280 |
// TODO (#8115): Fix the flakiness on Windows and MacOS. |
| 1281 |
t.Skip("Test is flaky on Windows and MacOS.") |
| 1282 |
} |
| 1283 |
type args struct { |
| 1284 |
ctx context.Context |
| 1285 |
otlpConfig *OTLPGrpcMetricExporter |
| 1286 |
} |
| 1287 |
tests := []struct { |
| 1288 |
name string |
| 1289 |
args args |
| 1290 |
grpcServerOpts func() ([]grpc.ServerOption, error) |
| 1291 |
}{ |
| 1292 |
{ |
| 1293 |
name: "no TLS config", |
| 1294 |
args: args{ |
| 1295 |
ctx: t.Context(), |
| 1296 |
otlpConfig: &OTLPGrpcMetricExporter{ |
| 1297 |
Compression: ptr("gzip"), |
| 1298 |
Timeout: ptr(5000), |
| 1299 |
Tls: &GrpcTls{ |
| 1300 |
Insecure: ptr(true), |
| 1301 |
}, |
| 1302 |
Headers: []NameStringValuePair{ |
| 1303 |
{Name: "test", Value: ptr("test1")}, |
| 1304 |
}, |
| 1305 |
}, |
| 1306 |
}, |
| 1307 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 1308 |
return []grpc.ServerOption{}, nil |
| 1309 |
}, |
| 1310 |
}, |
| 1311 |
{ |
| 1312 |
name: "with TLS config", |
| 1313 |
args: args{ |
| 1314 |
ctx: t.Context(), |
| 1315 |
otlpConfig: &OTLPGrpcMetricExporter{ |
| 1316 |
Compression: ptr("gzip"), |
| 1317 |
Timeout: ptr(5000), |
| 1318 |
Tls: &GrpcTls{ |
| 1319 |
CaFile: ptr("testdata/server-certs/server.crt"), |
| 1320 |
}, |
| 1321 |
Headers: []NameStringValuePair{ |
| 1322 |
{Name: "test", Value: ptr("test1")}, |
| 1323 |
}, |
| 1324 |
}, |
| 1325 |
}, |
| 1326 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 1327 |
opts := []grpc.ServerOption{} |
| 1328 |
tlsCreds, err := credentials.NewServerTLSFromFile("testdata/server-certs/server.crt", "testdata/server-certs/server.key") |
| 1329 |
if err != nil { |
| 1330 |
return nil, err |
| 1331 |
} |
| 1332 |
opts = append(opts, grpc.Creds(tlsCreds)) |
| 1333 |
return opts, nil |
| 1334 |
}, |
| 1335 |
}, |
| 1336 |
{ |
| 1337 |
name: "with TLS config and client key", |
| 1338 |
args: args{ |
| 1339 |
ctx: t.Context(), |
| 1340 |
otlpConfig: &OTLPGrpcMetricExporter{ |
| 1341 |
Compression: ptr("gzip"), |
| 1342 |
Timeout: ptr(5000), |
| 1343 |
Tls: &GrpcTls{ |
| 1344 |
CaFile: ptr("testdata/server-certs/server.crt"), |
| 1345 |
KeyFile: ptr("testdata/client-certs/client.key"), |
| 1346 |
CertFile: ptr("testdata/client-certs/client.crt"), |
| 1347 |
}, |
| 1348 |
Headers: []NameStringValuePair{ |
| 1349 |
{Name: "test", Value: ptr("test1")}, |
| 1350 |
}, |
| 1351 |
}, |
| 1352 |
}, |
| 1353 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 1354 |
opts := []grpc.ServerOption{} |
| 1355 |
cert, err := tls.LoadX509KeyPair("testdata/server-certs/server.crt", "testdata/server-certs/server.key") |
| 1356 |
if err != nil { |
| 1357 |
return nil, err |
| 1358 |
} |
| 1359 |
caCert, err := os.ReadFile("testdata/ca.crt") |
| 1360 |
if err != nil { |
| 1361 |
return nil, err |
| 1362 |
} |
| 1363 |
caCertPool := x509.NewCertPool() |
| 1364 |
caCertPool.AppendCertsFromPEM(caCert) |
| 1365 |
tlsCreds := credentials.NewTLS(&tls.Config{ |
| 1366 |
Certificates: []tls.Certificate{cert}, |
| 1367 |
ClientCAs: caCertPool, |
| 1368 |
ClientAuth: tls.RequireAndVerifyClientCert, |
| 1369 |
}) |
| 1370 |
opts = append(opts, grpc.Creds(tlsCreds)) |
| 1371 |
return opts, nil |
| 1372 |
}, |
| 1373 |
}, |
| 1374 |
} |
| 1375 |
for _, tt := range tests { |
| 1376 |
t.Run(tt.name, func(t *testing.T) { |
| 1377 |
n, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp4", "localhost:0") |
| 1378 |
require.NoError(t, err) |
| 1379 |
|
| 1380 |
// We need to manually construct the endpoint using the port on which the server is listening. |
| 1381 |
// |
| 1382 |
// n.Addr() always returns 127.0.0.1 instead of localhost. |
| 1383 |
// But our certificate is created with CN as 'localhost', not '127.0.0.1'. |
| 1384 |
// So we have to manually form the endpoint as "localhost:<port>". |
| 1385 |
_, port, err := net.SplitHostPort(n.Addr().String()) |
| 1386 |
require.NoError(t, err) |
| 1387 |
tt.args.otlpConfig.Endpoint = ptr("localhost:" + port) |
| 1388 |
|
| 1389 |
serverOpts, err := tt.grpcServerOpts() |
| 1390 |
require.NoError(t, err) |
| 1391 |
|
| 1392 |
startGRPCMetricCollector(t, n, serverOpts) |
| 1393 |
|
| 1394 |
exporter, err := otlpGRPCMetricExporter(tt.args.ctx, tt.args.otlpConfig) |
| 1395 |
require.NoError(t, err) |
| 1396 |
|
| 1397 |
res, err := resource.New(t.Context()) |
| 1398 |
require.NoError(t, err) |
| 1399 |
|
| 1400 |
assert.EventuallyWithT(t, func(collect *assert.CollectT) { |
| 1401 |
assert.NoError(collect, exporter.Export(context.Background(), &metricdata.ResourceMetrics{ //nolint:usetesting // required to avoid getting a canceled context. |
| 1402 |
Resource: res, |
| 1403 |
ScopeMetrics: []metricdata.ScopeMetrics{ |
| 1404 |
{ |
| 1405 |
Metrics: []metricdata.Metrics{ |
| 1406 |
{ |
| 1407 |
Name: "test-metric", |
| 1408 |
Data: metricdata.Gauge[int64]{ |
| 1409 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 1410 |
{ |
| 1411 |
Value: 1, |
| 1412 |
}, |
| 1413 |
}, |
| 1414 |
}, |
| 1415 |
}, |
| 1416 |
}, |
| 1417 |
}, |
| 1418 |
}, |
| 1419 |
})) |
| 1420 |
}, 10*time.Second, 1*time.Second) |
| 1421 |
}) |
| 1422 |
} |
| 1423 |
} |
| 1424 |
|
| 1425 |
// grpcMetricCollector is an OTLP gRPC server that collects all requests it receives. |
| 1426 |
type grpcMetricCollector struct { |
| 1427 |
v1.UnimplementedMetricsServiceServer |
| 1428 |
} |
| 1429 |
|
| 1430 |
var _ v1.MetricsServiceServer = (*grpcMetricCollector)(nil) |
| 1431 |
|
| 1432 |
// startGRPCMetricCollector returns a *grpcMetricCollector that is listening at the provided |
| 1433 |
// endpoint. |
| 1434 |
// |
| 1435 |
// If endpoint is an empty string, the returned collector will be listening on |
| 1436 |
// the localhost interface at an OS chosen port. |
| 1437 |
func startGRPCMetricCollector(t *testing.T, listener net.Listener, serverOptions []grpc.ServerOption) { |
| 1438 |
srv := grpc.NewServer(serverOptions...) |
| 1439 |
c := &grpcMetricCollector{} |
| 1440 |
|
| 1441 |
v1.RegisterMetricsServiceServer(srv, c) |
| 1442 |
|
| 1443 |
errCh := make(chan error, 1) |
| 1444 |
go func() { errCh <- srv.Serve(listener) }() |
| 1445 |
|
| 1446 |
t.Cleanup(func() { |
| 1447 |
srv.GracefulStop() |
| 1448 |
if err := <-errCh; err != nil && !errors.Is(err, grpc.ErrServerStopped) { |
| 1449 |
assert.NoError(t, err) |
| 1450 |
} |
| 1451 |
}) |
| 1452 |
} |
| 1453 |
|
| 1454 |
// Export handles the export req. |
| 1455 |
func (*grpcMetricCollector) Export( |
| 1456 |
_ context.Context, |
| 1457 |
_ *v1.ExportMetricsServiceRequest, |
| 1458 |
) (*v1.ExportMetricsServiceResponse, error) { |
| 1459 |
return &v1.ExportMetricsServiceResponse{}, nil |
| 1460 |
} |