| 1 |
|
| 2 |
|
| 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 |
"strings" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/stretchr/testify/assert" |
| 25 |
"github.com/stretchr/testify/require" |
| 26 |
"go.opentelemetry.io/otel/attribute" |
| 27 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" |
| 28 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 29 |
otelprom "go.opentelemetry.io/otel/exporters/prometheus" |
| 30 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
| 31 |
"go.opentelemetry.io/otel/metric" |
| 32 |
"go.opentelemetry.io/otel/metric/noop" |
| 33 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 34 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 35 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 36 |
"go.opentelemetry.io/otel/sdk/resource" |
| 37 |
v1 "go.opentelemetry.io/proto/otlp/collector/metrics/v1" |
| 38 |
"google.golang.org/grpc" |
| 39 |
"google.golang.org/grpc/credentials" |
| 40 |
) |
| 41 |
|
| 42 |
func TestMeterProvider(t *testing.T) { |
| 43 |
tests := []struct { |
| 44 |
name string |
| 45 |
cfg configOptions |
| 46 |
wantProvider metric.MeterProvider |
| 47 |
wantErr error |
| 48 |
}{ |
| 49 |
{ |
| 50 |
name: "no-meter-provider-configured", |
| 51 |
wantProvider: noop.NewMeterProvider(), |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "error-in-config", |
| 55 |
cfg: configOptions{ |
| 56 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 57 |
MeterProvider: &MeterProvider{ |
| 58 |
Readers: []MetricReader{ |
| 59 |
{ |
| 60 |
Periodic: &PeriodicMetricReader{}, |
| 61 |
Pull: &PullMetricReader{}, |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
}, |
| 67 |
wantProvider: noop.NewMeterProvider(), |
| 68 |
wantErr: errors.Join(errors.New("must not specify multiple metric reader type")), |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "multiple-errors-in-config", |
| 72 |
cfg: configOptions{ |
| 73 |
opentelemetryConfig: OpenTelemetryConfiguration{ |
| 74 |
MeterProvider: &MeterProvider{ |
| 75 |
Readers: []MetricReader{ |
| 76 |
{ |
| 77 |
Periodic: &PeriodicMetricReader{}, |
| 78 |
Pull: &PullMetricReader{}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
Periodic: &PeriodicMetricReader{ |
| 82 |
Exporter: PushMetricExporter{ |
| 83 |
Console: Console{}, |
| 84 |
OTLP: &OTLPMetric{}, |
| 85 |
}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
}, |
| 91 |
}, |
| 92 |
wantProvider: noop.NewMeterProvider(), |
| 93 |
wantErr: errors.Join(errors.New("must not specify multiple metric reader type"), errors.New("must not specify multiple exporters")), |
| 94 |
}, |
| 95 |
} |
| 96 |
for _, tt := range tests { |
| 97 |
mp, shutdown, err := meterProvider(tt.cfg, resource.Default()) |
| 98 |
require.Equal(t, tt.wantProvider, mp) |
| 99 |
assert.Equal(t, tt.wantErr, err) |
| 100 |
require.NoError(t, shutdown(t.Context())) |
| 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 |
OTLP: &OTLPMetric{ |
| 117 |
Protocol: ptr("http/protobuf"), |
| 118 |
Endpoint: ptr(srv.URL), |
| 119 |
Insecure: ptr(true), |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
}}, |
| 124 |
}, |
| 125 |
} |
| 126 |
|
| 127 |
var buf bytes.Buffer |
| 128 |
stdoutmetricExporter, err := stdoutmetric.New(stdoutmetric.WithWriter(&buf)) |
| 129 |
require.NoError(t, err) |
| 130 |
|
| 131 |
res := resource.NewSchemaless(attribute.String("foo", "bar")) |
| 132 |
sdk, err := NewSDK( |
| 133 |
WithOpenTelemetryConfiguration(cfg), |
| 134 |
WithMeterProviderOptions(sdkmetric.WithReader(sdkmetric.NewPeriodicReader(stdoutmetricExporter))), |
| 135 |
WithMeterProviderOptions(sdkmetric.WithResource(res)), |
| 136 |
) |
| 137 |
require.NoError(t, err) |
| 138 |
defer func() { |
| 139 |
assert.NoError(t, sdk.Shutdown(t.Context())) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
assert.NotZero(t, buf) |
| 144 |
assert.Equal(t, 1, calls) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
assert.NotContains(t, buf.String(), "foo") |
| 150 |
}() |
| 151 |
|
| 152 |
counter, _ := sdk.MeterProvider().Meter("test").Int64Counter("counter") |
| 153 |
counter.Add(t.Context(), 1) |
| 154 |
} |
| 155 |
|
| 156 |
func TestReader(t *testing.T) { |
| 157 |
consoleExporter, err := stdoutmetric.New( |
| 158 |
stdoutmetric.WithPrettyPrint(), |
| 159 |
) |
| 160 |
require.NoError(t, err) |
| 161 |
ctx := t.Context() |
| 162 |
otlpGRPCExporter, err := otlpmetricgrpc.New(ctx) |
| 163 |
require.NoError(t, err) |
| 164 |
otlpHTTPExporter, err := otlpmetrichttp.New(ctx) |
| 165 |
require.NoError(t, err) |
| 166 |
promExporter, err := otelprom.New() |
| 167 |
require.NoError(t, err) |
| 168 |
testCases := []struct { |
| 169 |
name string |
| 170 |
reader MetricReader |
| 171 |
args any |
| 172 |
wantErr string |
| 173 |
wantReader sdkmetric.Reader |
| 174 |
}{ |
| 175 |
{ |
| 176 |
name: "no reader", |
| 177 |
wantErr: "no valid metric reader", |
| 178 |
}, |
| 179 |
{ |
| 180 |
name: "pull/no-exporter", |
| 181 |
reader: MetricReader{ |
| 182 |
Pull: &PullMetricReader{}, |
| 183 |
}, |
| 184 |
wantErr: "no valid metric exporter", |
| 185 |
}, |
| 186 |
{ |
| 187 |
name: "pull/prometheus-no-host", |
| 188 |
reader: MetricReader{ |
| 189 |
Pull: &PullMetricReader{ |
| 190 |
Exporter: PullMetricExporter{ |
| 191 |
Prometheus: &Prometheus{}, |
| 192 |
}, |
| 193 |
}, |
| 194 |
}, |
| 195 |
wantErr: "host must be specified", |
| 196 |
}, |
| 197 |
{ |
| 198 |
name: "pull/prometheus-no-port", |
| 199 |
reader: MetricReader{ |
| 200 |
Pull: &PullMetricReader{ |
| 201 |
Exporter: PullMetricExporter{ |
| 202 |
Prometheus: &Prometheus{ |
| 203 |
Host: ptr("localhost"), |
| 204 |
}, |
| 205 |
}, |
| 206 |
}, |
| 207 |
}, |
| 208 |
wantErr: "port must be specified", |
| 209 |
}, |
| 210 |
{ |
| 211 |
name: "pull/prometheus", |
| 212 |
reader: MetricReader{ |
| 213 |
Pull: &PullMetricReader{ |
| 214 |
Exporter: PullMetricExporter{ |
| 215 |
Prometheus: &Prometheus{ |
| 216 |
Host: ptr("localhost"), |
| 217 |
Port: ptr(0), |
| 218 |
WithoutScopeInfo: ptr(true), |
| 219 |
WithoutUnits: ptr(true), |
| 220 |
WithoutTypeSuffix: ptr(true), |
| 221 |
WithResourceConstantLabels: &IncludeExclude{ |
| 222 |
Included: []string{"include"}, |
| 223 |
Excluded: []string{"exclude"}, |
| 224 |
}, |
| 225 |
}, |
| 226 |
}, |
| 227 |
}, |
| 228 |
}, |
| 229 |
wantReader: readerWithServer{promExporter, nil}, |
| 230 |
}, |
| 231 |
{ |
| 232 |
name: "periodic/otlp-exporter-invalid-protocol", |
| 233 |
reader: MetricReader{ |
| 234 |
Periodic: &PeriodicMetricReader{ |
| 235 |
Exporter: PushMetricExporter{ |
| 236 |
OTLP: &OTLPMetric{ |
| 237 |
Protocol: ptr("http/invalid"), |
| 238 |
}, |
| 239 |
}, |
| 240 |
}, |
| 241 |
}, |
| 242 |
wantErr: "unsupported protocol \"http/invalid\"", |
| 243 |
}, |
| 244 |
{ |
| 245 |
name: "periodic/otlp-grpc-exporter", |
| 246 |
reader: MetricReader{ |
| 247 |
Periodic: &PeriodicMetricReader{ |
| 248 |
Exporter: PushMetricExporter{ |
| 249 |
OTLP: &OTLPMetric{ |
| 250 |
Protocol: ptr("grpc"), |
| 251 |
Endpoint: ptr("http://localhost:4318"), |
| 252 |
Compression: ptr("gzip"), |
| 253 |
Timeout: ptr(1000), |
| 254 |
Headers: []NameStringValuePair{ |
| 255 |
{Name: "test", Value: ptr("test1")}, |
| 256 |
}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
}, |
| 260 |
}, |
| 261 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 262 |
}, |
| 263 |
{ |
| 264 |
name: "periodic/otlp-grpc-exporter-with-path", |
| 265 |
reader: MetricReader{ |
| 266 |
Periodic: &PeriodicMetricReader{ |
| 267 |
Exporter: PushMetricExporter{ |
| 268 |
OTLP: &OTLPMetric{ |
| 269 |
Protocol: ptr("grpc"), |
| 270 |
Endpoint: ptr("http://localhost:4318/path/123"), |
| 271 |
Compression: ptr("gzip"), |
| 272 |
Timeout: ptr(1000), |
| 273 |
Headers: []NameStringValuePair{ |
| 274 |
{Name: "test", Value: ptr("test1")}, |
| 275 |
}, |
| 276 |
}, |
| 277 |
}, |
| 278 |
}, |
| 279 |
}, |
| 280 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 281 |
}, |
| 282 |
{ |
| 283 |
name: "periodic/otlp-grpc-good-ca-certificate", |
| 284 |
reader: MetricReader{ |
| 285 |
Periodic: &PeriodicMetricReader{ |
| 286 |
Exporter: PushMetricExporter{ |
| 287 |
OTLP: &OTLPMetric{ |
| 288 |
Protocol: ptr("grpc"), |
| 289 |
Endpoint: ptr("https://localhost:4317"), |
| 290 |
Compression: ptr("gzip"), |
| 291 |
Timeout: ptr(1000), |
| 292 |
Certificate: ptr(filepath.Join("..", "testdata", "ca.crt")), |
| 293 |
}, |
| 294 |
}, |
| 295 |
}, |
| 296 |
}, |
| 297 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 298 |
}, |
| 299 |
{ |
| 300 |
name: "periodic/otlp-grpc-bad-ca-certificate", |
| 301 |
reader: MetricReader{ |
| 302 |
Periodic: &PeriodicMetricReader{ |
| 303 |
Exporter: PushMetricExporter{ |
| 304 |
OTLP: &OTLPMetric{ |
| 305 |
Protocol: ptr("grpc"), |
| 306 |
Endpoint: ptr("https://localhost:4317"), |
| 307 |
Compression: ptr("gzip"), |
| 308 |
Timeout: ptr(1000), |
| 309 |
Certificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 310 |
}, |
| 311 |
}, |
| 312 |
}, |
| 313 |
}, |
| 314 |
wantErr: "could not create certificate authority chain from certificate", |
| 315 |
}, |
| 316 |
{ |
| 317 |
name: "periodic/otlp-grpc-bad-client-certificate", |
| 318 |
reader: MetricReader{ |
| 319 |
Periodic: &PeriodicMetricReader{ |
| 320 |
Exporter: PushMetricExporter{ |
| 321 |
OTLP: &OTLPMetric{ |
| 322 |
Protocol: ptr("grpc"), |
| 323 |
Endpoint: ptr("localhost:4317"), |
| 324 |
Compression: ptr("gzip"), |
| 325 |
Timeout: ptr(1000), |
| 326 |
ClientCertificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 327 |
ClientKey: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 328 |
}, |
| 329 |
}, |
| 330 |
}, |
| 331 |
}, |
| 332 |
wantErr: "could not use client certificate: tls: failed to find any PEM data in certificate input", |
| 333 |
}, |
| 334 |
{ |
| 335 |
name: "periodic/otlp-grpc-bad-headerslist", |
| 336 |
reader: MetricReader{ |
| 337 |
Periodic: &PeriodicMetricReader{ |
| 338 |
Exporter: PushMetricExporter{ |
| 339 |
OTLP: &OTLPMetric{ |
| 340 |
Protocol: ptr("grpc"), |
| 341 |
Endpoint: ptr("localhost:4317"), |
| 342 |
Compression: ptr("gzip"), |
| 343 |
Timeout: ptr(1000), |
| 344 |
HeadersList: ptr("==="), |
| 345 |
}, |
| 346 |
}, |
| 347 |
}, |
| 348 |
}, |
| 349 |
wantErr: "invalid headers list: invalid key: \"\"", |
| 350 |
}, |
| 351 |
{ |
| 352 |
name: "periodic/otlp-grpc-exporter-no-endpoint", |
| 353 |
reader: MetricReader{ |
| 354 |
Periodic: &PeriodicMetricReader{ |
| 355 |
Exporter: PushMetricExporter{ |
| 356 |
OTLP: &OTLPMetric{ |
| 357 |
Protocol: ptr("grpc"), |
| 358 |
Compression: ptr("gzip"), |
| 359 |
Timeout: ptr(1000), |
| 360 |
Headers: []NameStringValuePair{ |
| 361 |
{Name: "test", Value: ptr("test1")}, |
| 362 |
}, |
| 363 |
}, |
| 364 |
}, |
| 365 |
}, |
| 366 |
}, |
| 367 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 368 |
}, |
| 369 |
{ |
| 370 |
name: "periodic/otlp-grpc-exporter-socket-endpoint", |
| 371 |
reader: MetricReader{ |
| 372 |
Periodic: &PeriodicMetricReader{ |
| 373 |
Exporter: PushMetricExporter{ |
| 374 |
OTLP: &OTLPMetric{ |
| 375 |
Protocol: ptr("grpc"), |
| 376 |
Endpoint: ptr("unix:collector.sock"), |
| 377 |
Compression: ptr("gzip"), |
| 378 |
Timeout: ptr(1000), |
| 379 |
Headers: []NameStringValuePair{ |
| 380 |
{Name: "test", Value: ptr("test1")}, |
| 381 |
}, |
| 382 |
}, |
| 383 |
}, |
| 384 |
}, |
| 385 |
}, |
| 386 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 387 |
}, |
| 388 |
{ |
| 389 |
name: "periodic/otlp-grpc-exporter-no-scheme", |
| 390 |
reader: MetricReader{ |
| 391 |
Periodic: &PeriodicMetricReader{ |
| 392 |
Exporter: PushMetricExporter{ |
| 393 |
OTLP: &OTLPMetric{ |
| 394 |
Protocol: ptr("grpc"), |
| 395 |
Endpoint: ptr("localhost:4318"), |
| 396 |
Compression: ptr("gzip"), |
| 397 |
Timeout: ptr(1000), |
| 398 |
Headers: []NameStringValuePair{ |
| 399 |
{Name: "test", Value: ptr("test1")}, |
| 400 |
}, |
| 401 |
}, |
| 402 |
}, |
| 403 |
}, |
| 404 |
}, |
| 405 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 406 |
}, |
| 407 |
{ |
| 408 |
name: "periodic/otlp-grpc-invalid-endpoint", |
| 409 |
reader: MetricReader{ |
| 410 |
Periodic: &PeriodicMetricReader{ |
| 411 |
Exporter: PushMetricExporter{ |
| 412 |
OTLP: &OTLPMetric{ |
| 413 |
Protocol: ptr("grpc"), |
| 414 |
Endpoint: ptr(" "), |
| 415 |
Compression: ptr("gzip"), |
| 416 |
Timeout: ptr(1000), |
| 417 |
Headers: []NameStringValuePair{ |
| 418 |
{Name: "test", Value: ptr("test1")}, |
| 419 |
}, |
| 420 |
}, |
| 421 |
}, |
| 422 |
}, |
| 423 |
}, |
| 424 |
wantErr: "parse \" \": invalid URI for request", |
| 425 |
}, |
| 426 |
{ |
| 427 |
name: "periodic/otlp-grpc-none-compression", |
| 428 |
reader: MetricReader{ |
| 429 |
Periodic: &PeriodicMetricReader{ |
| 430 |
Exporter: PushMetricExporter{ |
| 431 |
OTLP: &OTLPMetric{ |
| 432 |
Protocol: ptr("grpc"), |
| 433 |
Endpoint: ptr("localhost:4318"), |
| 434 |
Compression: ptr("none"), |
| 435 |
Timeout: ptr(1000), |
| 436 |
Headers: []NameStringValuePair{ |
| 437 |
{Name: "test", Value: ptr("test1")}, |
| 438 |
}, |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
}, |
| 443 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 444 |
}, |
| 445 |
{ |
| 446 |
name: "periodic/otlp-grpc-delta-temporality", |
| 447 |
reader: MetricReader{ |
| 448 |
Periodic: &PeriodicMetricReader{ |
| 449 |
Exporter: PushMetricExporter{ |
| 450 |
OTLP: &OTLPMetric{ |
| 451 |
Protocol: ptr("grpc"), |
| 452 |
Endpoint: ptr("localhost:4318"), |
| 453 |
Compression: ptr("none"), |
| 454 |
Timeout: ptr(1000), |
| 455 |
Headers: []NameStringValuePair{ |
| 456 |
{Name: "test", Value: ptr("test1")}, |
| 457 |
}, |
| 458 |
TemporalityPreference: ptr("delta"), |
| 459 |
}, |
| 460 |
}, |
| 461 |
}, |
| 462 |
}, |
| 463 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 464 |
}, |
| 465 |
{ |
| 466 |
name: "periodic/otlp-grpc-cumulative-temporality", |
| 467 |
reader: MetricReader{ |
| 468 |
Periodic: &PeriodicMetricReader{ |
| 469 |
Exporter: PushMetricExporter{ |
| 470 |
OTLP: &OTLPMetric{ |
| 471 |
Protocol: ptr("grpc"), |
| 472 |
Endpoint: ptr("localhost:4318"), |
| 473 |
Compression: ptr("none"), |
| 474 |
Timeout: ptr(1000), |
| 475 |
Headers: []NameStringValuePair{ |
| 476 |
{Name: "test", Value: ptr("test1")}, |
| 477 |
}, |
| 478 |
TemporalityPreference: ptr("cumulative"), |
| 479 |
}, |
| 480 |
}, |
| 481 |
}, |
| 482 |
}, |
| 483 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 484 |
}, |
| 485 |
{ |
| 486 |
name: "periodic/otlp-grpc-lowmemory-temporality", |
| 487 |
reader: MetricReader{ |
| 488 |
Periodic: &PeriodicMetricReader{ |
| 489 |
Exporter: PushMetricExporter{ |
| 490 |
OTLP: &OTLPMetric{ |
| 491 |
Protocol: ptr("grpc"), |
| 492 |
Endpoint: ptr("localhost:4318"), |
| 493 |
Compression: ptr("none"), |
| 494 |
Timeout: ptr(1000), |
| 495 |
Headers: []NameStringValuePair{ |
| 496 |
{Name: "test", Value: ptr("test1")}, |
| 497 |
}, |
| 498 |
TemporalityPreference: ptr("lowmemory"), |
| 499 |
}, |
| 500 |
}, |
| 501 |
}, |
| 502 |
}, |
| 503 |
wantReader: sdkmetric.NewPeriodicReader(otlpGRPCExporter), |
| 504 |
}, |
| 505 |
{ |
| 506 |
name: "periodic/otlp-grpc-invalid-temporality", |
| 507 |
reader: MetricReader{ |
| 508 |
Periodic: &PeriodicMetricReader{ |
| 509 |
Exporter: PushMetricExporter{ |
| 510 |
OTLP: &OTLPMetric{ |
| 511 |
Protocol: ptr("grpc"), |
| 512 |
Endpoint: ptr("localhost:4318"), |
| 513 |
Compression: ptr("none"), |
| 514 |
Timeout: ptr(1000), |
| 515 |
Headers: []NameStringValuePair{ |
| 516 |
{Name: "test", Value: ptr("test1")}, |
| 517 |
}, |
| 518 |
TemporalityPreference: ptr("invalid"), |
| 519 |
}, |
| 520 |
}, |
| 521 |
}, |
| 522 |
}, |
| 523 |
wantErr: "unsupported temporality preference \"invalid\"", |
| 524 |
}, |
| 525 |
{ |
| 526 |
name: "periodic/otlp-grpc-invalid-compression", |
| 527 |
reader: MetricReader{ |
| 528 |
Periodic: &PeriodicMetricReader{ |
| 529 |
Exporter: PushMetricExporter{ |
| 530 |
OTLP: &OTLPMetric{ |
| 531 |
Protocol: ptr("grpc"), |
| 532 |
Endpoint: ptr("localhost:4318"), |
| 533 |
Compression: ptr("invalid"), |
| 534 |
Timeout: ptr(1000), |
| 535 |
Headers: []NameStringValuePair{ |
| 536 |
{Name: "test", Value: ptr("test1")}, |
| 537 |
}, |
| 538 |
}, |
| 539 |
}, |
| 540 |
}, |
| 541 |
}, |
| 542 |
wantErr: "unsupported compression \"invalid\"", |
| 543 |
}, |
| 544 |
{ |
| 545 |
name: "periodic/otlp-http-exporter", |
| 546 |
reader: MetricReader{ |
| 547 |
Periodic: &PeriodicMetricReader{ |
| 548 |
Exporter: PushMetricExporter{ |
| 549 |
OTLP: &OTLPMetric{ |
| 550 |
Protocol: ptr("http/protobuf"), |
| 551 |
Endpoint: ptr("http://localhost:4318"), |
| 552 |
Compression: ptr("gzip"), |
| 553 |
Timeout: ptr(1000), |
| 554 |
Headers: []NameStringValuePair{ |
| 555 |
{Name: "test", Value: ptr("test1")}, |
| 556 |
}, |
| 557 |
}, |
| 558 |
}, |
| 559 |
}, |
| 560 |
}, |
| 561 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 562 |
}, |
| 563 |
{ |
| 564 |
name: "periodic/otlp-http-good-ca-certificate", |
| 565 |
reader: MetricReader{ |
| 566 |
Periodic: &PeriodicMetricReader{ |
| 567 |
Exporter: PushMetricExporter{ |
| 568 |
OTLP: &OTLPMetric{ |
| 569 |
Protocol: ptr("http/protobuf"), |
| 570 |
Endpoint: ptr("https://localhost:4317"), |
| 571 |
Compression: ptr("gzip"), |
| 572 |
Timeout: ptr(1000), |
| 573 |
Certificate: ptr(filepath.Join("..", "testdata", "ca.crt")), |
| 574 |
}, |
| 575 |
}, |
| 576 |
}, |
| 577 |
}, |
| 578 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 579 |
}, |
| 580 |
{ |
| 581 |
name: "periodic/otlp-http-bad-ca-certificate", |
| 582 |
reader: MetricReader{ |
| 583 |
Periodic: &PeriodicMetricReader{ |
| 584 |
Exporter: PushMetricExporter{ |
| 585 |
OTLP: &OTLPMetric{ |
| 586 |
Protocol: ptr("http/protobuf"), |
| 587 |
Endpoint: ptr("https://localhost:4317"), |
| 588 |
Compression: ptr("gzip"), |
| 589 |
Timeout: ptr(1000), |
| 590 |
Certificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 591 |
}, |
| 592 |
}, |
| 593 |
}, |
| 594 |
}, |
| 595 |
wantErr: "could not create certificate authority chain from certificate", |
| 596 |
}, |
| 597 |
{ |
| 598 |
name: "periodic/otlp-http-bad-client-certificate", |
| 599 |
reader: MetricReader{ |
| 600 |
Periodic: &PeriodicMetricReader{ |
| 601 |
Exporter: PushMetricExporter{ |
| 602 |
OTLP: &OTLPMetric{ |
| 603 |
Protocol: ptr("http/protobuf"), |
| 604 |
Endpoint: ptr("localhost:4317"), |
| 605 |
Compression: ptr("gzip"), |
| 606 |
Timeout: ptr(1000), |
| 607 |
ClientCertificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 608 |
ClientKey: ptr(filepath.Join("..", "testdata", "bad_cert.crt")), |
| 609 |
}, |
| 610 |
}, |
| 611 |
}, |
| 612 |
}, |
| 613 |
wantErr: "could not use client certificate: tls: failed to find any PEM data in certificate input", |
| 614 |
}, |
| 615 |
{ |
| 616 |
name: "periodic/otlp-http-bad-headerslist", |
| 617 |
reader: MetricReader{ |
| 618 |
Periodic: &PeriodicMetricReader{ |
| 619 |
Exporter: PushMetricExporter{ |
| 620 |
OTLP: &OTLPMetric{ |
| 621 |
Protocol: ptr("http/protobuf"), |
| 622 |
Endpoint: ptr("localhost:4317"), |
| 623 |
Compression: ptr("gzip"), |
| 624 |
Timeout: ptr(1000), |
| 625 |
HeadersList: ptr("==="), |
| 626 |
}, |
| 627 |
}, |
| 628 |
}, |
| 629 |
}, |
| 630 |
wantErr: "invalid headers list: invalid key: \"\"", |
| 631 |
}, |
| 632 |
{ |
| 633 |
name: "periodic/otlp-http-exporter-with-path", |
| 634 |
reader: MetricReader{ |
| 635 |
Periodic: &PeriodicMetricReader{ |
| 636 |
Exporter: PushMetricExporter{ |
| 637 |
OTLP: &OTLPMetric{ |
| 638 |
Protocol: ptr("http/protobuf"), |
| 639 |
Endpoint: ptr("http://localhost:4318/path/123"), |
| 640 |
Compression: ptr("gzip"), |
| 641 |
Timeout: ptr(1000), |
| 642 |
Headers: []NameStringValuePair{ |
| 643 |
{Name: "test", Value: ptr("test1")}, |
| 644 |
}, |
| 645 |
}, |
| 646 |
}, |
| 647 |
}, |
| 648 |
}, |
| 649 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 650 |
}, |
| 651 |
{ |
| 652 |
name: "periodic/otlp-http-exporter-no-endpoint", |
| 653 |
reader: MetricReader{ |
| 654 |
Periodic: &PeriodicMetricReader{ |
| 655 |
Exporter: PushMetricExporter{ |
| 656 |
OTLP: &OTLPMetric{ |
| 657 |
Protocol: ptr("http/protobuf"), |
| 658 |
Compression: ptr("gzip"), |
| 659 |
Timeout: ptr(1000), |
| 660 |
Headers: []NameStringValuePair{ |
| 661 |
{Name: "test", Value: ptr("test1")}, |
| 662 |
}, |
| 663 |
}, |
| 664 |
}, |
| 665 |
}, |
| 666 |
}, |
| 667 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 668 |
}, |
| 669 |
{ |
| 670 |
name: "periodic/otlp-http-exporter-no-scheme", |
| 671 |
reader: MetricReader{ |
| 672 |
Periodic: &PeriodicMetricReader{ |
| 673 |
Exporter: PushMetricExporter{ |
| 674 |
OTLP: &OTLPMetric{ |
| 675 |
Protocol: ptr("http/protobuf"), |
| 676 |
Endpoint: ptr("localhost:4318"), |
| 677 |
Compression: ptr("gzip"), |
| 678 |
Timeout: ptr(1000), |
| 679 |
Headers: []NameStringValuePair{ |
| 680 |
{Name: "test", Value: ptr("test1")}, |
| 681 |
}, |
| 682 |
}, |
| 683 |
}, |
| 684 |
}, |
| 685 |
}, |
| 686 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 687 |
}, |
| 688 |
{ |
| 689 |
name: "periodic/otlp-http-invalid-endpoint", |
| 690 |
reader: MetricReader{ |
| 691 |
Periodic: &PeriodicMetricReader{ |
| 692 |
Exporter: PushMetricExporter{ |
| 693 |
OTLP: &OTLPMetric{ |
| 694 |
Protocol: ptr("http/protobuf"), |
| 695 |
Endpoint: ptr(" "), |
| 696 |
Compression: ptr("gzip"), |
| 697 |
Timeout: ptr(1000), |
| 698 |
Headers: []NameStringValuePair{ |
| 699 |
{Name: "test", Value: ptr("test1")}, |
| 700 |
}, |
| 701 |
}, |
| 702 |
}, |
| 703 |
}, |
| 704 |
}, |
| 705 |
wantErr: "parse \" \": invalid URI for request", |
| 706 |
}, |
| 707 |
{ |
| 708 |
name: "periodic/otlp-http-none-compression", |
| 709 |
reader: MetricReader{ |
| 710 |
Periodic: &PeriodicMetricReader{ |
| 711 |
Exporter: PushMetricExporter{ |
| 712 |
OTLP: &OTLPMetric{ |
| 713 |
Protocol: ptr("http/protobuf"), |
| 714 |
Endpoint: ptr("localhost:4318"), |
| 715 |
Compression: ptr("none"), |
| 716 |
Timeout: ptr(1000), |
| 717 |
Headers: []NameStringValuePair{ |
| 718 |
{Name: "test", Value: ptr("test1")}, |
| 719 |
}, |
| 720 |
}, |
| 721 |
}, |
| 722 |
}, |
| 723 |
}, |
| 724 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 725 |
}, |
| 726 |
{ |
| 727 |
name: "periodic/otlp-http-cumulative-temporality", |
| 728 |
reader: MetricReader{ |
| 729 |
Periodic: &PeriodicMetricReader{ |
| 730 |
Exporter: PushMetricExporter{ |
| 731 |
OTLP: &OTLPMetric{ |
| 732 |
Protocol: ptr("http/protobuf"), |
| 733 |
Endpoint: ptr("localhost:4318"), |
| 734 |
Compression: ptr("none"), |
| 735 |
Timeout: ptr(1000), |
| 736 |
Headers: []NameStringValuePair{ |
| 737 |
{Name: "test", Value: ptr("test1")}, |
| 738 |
}, |
| 739 |
TemporalityPreference: ptr("cumulative"), |
| 740 |
}, |
| 741 |
}, |
| 742 |
}, |
| 743 |
}, |
| 744 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 745 |
}, |
| 746 |
{ |
| 747 |
name: "periodic/otlp-http-lowmemory-temporality", |
| 748 |
reader: MetricReader{ |
| 749 |
Periodic: &PeriodicMetricReader{ |
| 750 |
Exporter: PushMetricExporter{ |
| 751 |
OTLP: &OTLPMetric{ |
| 752 |
Protocol: ptr("http/protobuf"), |
| 753 |
Endpoint: ptr("localhost:4318"), |
| 754 |
Compression: ptr("none"), |
| 755 |
Timeout: ptr(1000), |
| 756 |
Headers: []NameStringValuePair{ |
| 757 |
{Name: "test", Value: ptr("test1")}, |
| 758 |
}, |
| 759 |
TemporalityPreference: ptr("lowmemory"), |
| 760 |
}, |
| 761 |
}, |
| 762 |
}, |
| 763 |
}, |
| 764 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 765 |
}, |
| 766 |
{ |
| 767 |
name: "periodic/otlp-http-delta-temporality", |
| 768 |
reader: MetricReader{ |
| 769 |
Periodic: &PeriodicMetricReader{ |
| 770 |
Exporter: PushMetricExporter{ |
| 771 |
OTLP: &OTLPMetric{ |
| 772 |
Protocol: ptr("http/protobuf"), |
| 773 |
Endpoint: ptr("localhost:4318"), |
| 774 |
Compression: ptr("none"), |
| 775 |
Timeout: ptr(1000), |
| 776 |
Headers: []NameStringValuePair{ |
| 777 |
{Name: "test", Value: ptr("test1")}, |
| 778 |
}, |
| 779 |
TemporalityPreference: ptr("delta"), |
| 780 |
}, |
| 781 |
}, |
| 782 |
}, |
| 783 |
}, |
| 784 |
wantReader: sdkmetric.NewPeriodicReader(otlpHTTPExporter), |
| 785 |
}, |
| 786 |
{ |
| 787 |
name: "periodic/otlp-http-invalid-temporality", |
| 788 |
reader: MetricReader{ |
| 789 |
Periodic: &PeriodicMetricReader{ |
| 790 |
Exporter: PushMetricExporter{ |
| 791 |
OTLP: &OTLPMetric{ |
| 792 |
Protocol: ptr("http/protobuf"), |
| 793 |
Endpoint: ptr("localhost:4318"), |
| 794 |
Compression: ptr("none"), |
| 795 |
Timeout: ptr(1000), |
| 796 |
Headers: []NameStringValuePair{ |
| 797 |
{Name: "test", Value: ptr("test1")}, |
| 798 |
}, |
| 799 |
TemporalityPreference: ptr("invalid"), |
| 800 |
}, |
| 801 |
}, |
| 802 |
}, |
| 803 |
}, |
| 804 |
wantErr: "unsupported temporality preference \"invalid\"", |
| 805 |
}, |
| 806 |
{ |
| 807 |
name: "periodic/otlp-http-invalid-compression", |
| 808 |
reader: MetricReader{ |
| 809 |
Periodic: &PeriodicMetricReader{ |
| 810 |
Exporter: PushMetricExporter{ |
| 811 |
OTLP: &OTLPMetric{ |
| 812 |
Protocol: ptr("http/protobuf"), |
| 813 |
Endpoint: ptr("localhost:4318"), |
| 814 |
Compression: ptr("invalid"), |
| 815 |
Timeout: ptr(1000), |
| 816 |
Headers: []NameStringValuePair{ |
| 817 |
{Name: "test", Value: ptr("test1")}, |
| 818 |
}, |
| 819 |
}, |
| 820 |
}, |
| 821 |
}, |
| 822 |
}, |
| 823 |
wantErr: "unsupported compression \"invalid\"", |
| 824 |
}, |
| 825 |
{ |
| 826 |
name: "periodic/no-exporter", |
| 827 |
reader: MetricReader{ |
| 828 |
Periodic: &PeriodicMetricReader{ |
| 829 |
Exporter: PushMetricExporter{}, |
| 830 |
}, |
| 831 |
}, |
| 832 |
wantErr: "no valid metric exporter", |
| 833 |
}, |
| 834 |
{ |
| 835 |
name: "periodic/console-exporter", |
| 836 |
reader: MetricReader{ |
| 837 |
Periodic: &PeriodicMetricReader{ |
| 838 |
Exporter: PushMetricExporter{ |
| 839 |
Console: Console{}, |
| 840 |
}, |
| 841 |
}, |
| 842 |
}, |
| 843 |
wantReader: sdkmetric.NewPeriodicReader(consoleExporter), |
| 844 |
}, |
| 845 |
{ |
| 846 |
name: "periodic/console-exporter-with-extra-options", |
| 847 |
reader: MetricReader{ |
| 848 |
Periodic: &PeriodicMetricReader{ |
| 849 |
Interval: ptr(30_000), |
| 850 |
Timeout: ptr(5_000), |
| 851 |
Exporter: PushMetricExporter{ |
| 852 |
Console: Console{}, |
| 853 |
}, |
| 854 |
}, |
| 855 |
}, |
| 856 |
wantReader: sdkmetric.NewPeriodicReader( |
| 857 |
consoleExporter, |
| 858 |
sdkmetric.WithInterval(30_000*time.Millisecond), |
| 859 |
sdkmetric.WithTimeout(5_000*time.Millisecond), |
| 860 |
), |
| 861 |
}, |
| 862 |
} |
| 863 |
for _, tt := range testCases { |
| 864 |
t.Run(tt.name, func(t *testing.T) { |
| 865 |
got, err := metricReader(t.Context(), tt.reader) |
| 866 |
if tt.wantErr != "" { |
| 867 |
require.Error(t, err) |
| 868 |
require.Equal(t, tt.wantErr, err.Error()) |
| 869 |
} else { |
| 870 |
require.NoError(t, err) |
| 871 |
} |
| 872 |
if tt.wantReader == nil { |
| 873 |
require.Nil(t, got) |
| 874 |
} else { |
| 875 |
require.Equal(t, reflect.TypeOf(tt.wantReader), reflect.TypeOf(got)) |
| 876 |
var fieldName string |
| 877 |
switch reflect.TypeOf(tt.wantReader).String() { |
| 878 |
case "*metric.PeriodicReader": |
| 879 |
fieldName = "exporter" |
| 880 |
case "otelconf.readerWithServer": |
| 881 |
fieldName = "Reader" |
| 882 |
default: |
| 883 |
fieldName = "e" |
| 884 |
} |
| 885 |
wantExporterType := reflect.Indirect(reflect.ValueOf(tt.wantReader)).FieldByName(fieldName).Elem().Type() |
| 886 |
gotExporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName(fieldName).Elem().Type() |
| 887 |
require.Equal(t, wantExporterType.String(), gotExporterType.String()) |
| 888 |
require.NoError(t, got.Shutdown(t.Context())) |
| 889 |
} |
| 890 |
}) |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
func TestView(t *testing.T) { |
| 895 |
testCases := []struct { |
| 896 |
name string |
| 897 |
view View |
| 898 |
args any |
| 899 |
wantErr string |
| 900 |
matchInstrument *sdkmetric.Instrument |
| 901 |
wantStream sdkmetric.Stream |
| 902 |
wantResult bool |
| 903 |
}{ |
| 904 |
{ |
| 905 |
name: "no selector", |
| 906 |
wantErr: "view: no selector provided", |
| 907 |
}, |
| 908 |
{ |
| 909 |
name: "selector/invalid_type", |
| 910 |
view: View{ |
| 911 |
Selector: &ViewSelector{ |
| 912 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("invalid_type")), |
| 913 |
}, |
| 914 |
}, |
| 915 |
wantErr: "view_selector: instrument_type: invalid value", |
| 916 |
}, |
| 917 |
{ |
| 918 |
name: "selector/invalid_type", |
| 919 |
view: View{ |
| 920 |
Selector: &ViewSelector{}, |
| 921 |
}, |
| 922 |
wantErr: "view_selector: empty selector not supporter", |
| 923 |
}, |
| 924 |
{ |
| 925 |
name: "all selectors match", |
| 926 |
view: View{ |
| 927 |
Selector: &ViewSelector{ |
| 928 |
InstrumentName: ptr("test_name"), |
| 929 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 930 |
Unit: ptr("test_unit"), |
| 931 |
MeterName: ptr("test_meter_name"), |
| 932 |
MeterVersion: ptr("test_meter_version"), |
| 933 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 934 |
}, |
| 935 |
}, |
| 936 |
matchInstrument: &sdkmetric.Instrument{ |
| 937 |
Name: "test_name", |
| 938 |
Unit: "test_unit", |
| 939 |
Kind: sdkmetric.InstrumentKindCounter, |
| 940 |
Scope: instrumentation.Scope{ |
| 941 |
Name: "test_meter_name", |
| 942 |
Version: "test_meter_version", |
| 943 |
SchemaURL: "test_schema_url", |
| 944 |
}, |
| 945 |
}, |
| 946 |
wantStream: sdkmetric.Stream{Name: "test_name", Unit: "test_unit"}, |
| 947 |
wantResult: true, |
| 948 |
}, |
| 949 |
{ |
| 950 |
name: "all selectors no match name", |
| 951 |
view: View{ |
| 952 |
Selector: &ViewSelector{ |
| 953 |
InstrumentName: ptr("test_name"), |
| 954 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 955 |
Unit: ptr("test_unit"), |
| 956 |
MeterName: ptr("test_meter_name"), |
| 957 |
MeterVersion: ptr("test_meter_version"), |
| 958 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 959 |
}, |
| 960 |
}, |
| 961 |
matchInstrument: &sdkmetric.Instrument{ |
| 962 |
Name: "not_match", |
| 963 |
Unit: "test_unit", |
| 964 |
Kind: sdkmetric.InstrumentKindCounter, |
| 965 |
Scope: instrumentation.Scope{ |
| 966 |
Name: "test_meter_name", |
| 967 |
Version: "test_meter_version", |
| 968 |
SchemaURL: "test_schema_url", |
| 969 |
}, |
| 970 |
}, |
| 971 |
wantStream: sdkmetric.Stream{}, |
| 972 |
wantResult: false, |
| 973 |
}, |
| 974 |
{ |
| 975 |
name: "all selectors no match unit", |
| 976 |
view: View{ |
| 977 |
Selector: &ViewSelector{ |
| 978 |
InstrumentName: ptr("test_name"), |
| 979 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 980 |
Unit: ptr("test_unit"), |
| 981 |
MeterName: ptr("test_meter_name"), |
| 982 |
MeterVersion: ptr("test_meter_version"), |
| 983 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 984 |
}, |
| 985 |
}, |
| 986 |
matchInstrument: &sdkmetric.Instrument{ |
| 987 |
Name: "test_name", |
| 988 |
Unit: "not_match", |
| 989 |
Kind: sdkmetric.InstrumentKindCounter, |
| 990 |
Scope: instrumentation.Scope{ |
| 991 |
Name: "test_meter_name", |
| 992 |
Version: "test_meter_version", |
| 993 |
SchemaURL: "test_schema_url", |
| 994 |
}, |
| 995 |
}, |
| 996 |
wantStream: sdkmetric.Stream{}, |
| 997 |
wantResult: false, |
| 998 |
}, |
| 999 |
{ |
| 1000 |
name: "all selectors no match kind", |
| 1001 |
view: View{ |
| 1002 |
Selector: &ViewSelector{ |
| 1003 |
InstrumentName: ptr("test_name"), |
| 1004 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("histogram")), |
| 1005 |
Unit: ptr("test_unit"), |
| 1006 |
MeterName: ptr("test_meter_name"), |
| 1007 |
MeterVersion: ptr("test_meter_version"), |
| 1008 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 1009 |
}, |
| 1010 |
}, |
| 1011 |
matchInstrument: &sdkmetric.Instrument{ |
| 1012 |
Name: "test_name", |
| 1013 |
Unit: "test_unit", |
| 1014 |
Kind: sdkmetric.InstrumentKindCounter, |
| 1015 |
Scope: instrumentation.Scope{ |
| 1016 |
Name: "test_meter_name", |
| 1017 |
Version: "test_meter_version", |
| 1018 |
SchemaURL: "test_schema_url", |
| 1019 |
}, |
| 1020 |
}, |
| 1021 |
wantStream: sdkmetric.Stream{}, |
| 1022 |
wantResult: false, |
| 1023 |
}, |
| 1024 |
{ |
| 1025 |
name: "all selectors no match meter name", |
| 1026 |
view: View{ |
| 1027 |
Selector: &ViewSelector{ |
| 1028 |
InstrumentName: ptr("test_name"), |
| 1029 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 1030 |
Unit: ptr("test_unit"), |
| 1031 |
MeterName: ptr("test_meter_name"), |
| 1032 |
MeterVersion: ptr("test_meter_version"), |
| 1033 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 1034 |
}, |
| 1035 |
}, |
| 1036 |
matchInstrument: &sdkmetric.Instrument{ |
| 1037 |
Name: "test_name", |
| 1038 |
Unit: "test_unit", |
| 1039 |
Kind: sdkmetric.InstrumentKindCounter, |
| 1040 |
Scope: instrumentation.Scope{ |
| 1041 |
Name: "not_match", |
| 1042 |
Version: "test_meter_version", |
| 1043 |
SchemaURL: "test_schema_url", |
| 1044 |
}, |
| 1045 |
}, |
| 1046 |
wantStream: sdkmetric.Stream{}, |
| 1047 |
wantResult: false, |
| 1048 |
}, |
| 1049 |
{ |
| 1050 |
name: "all selectors no match meter version", |
| 1051 |
view: View{ |
| 1052 |
Selector: &ViewSelector{ |
| 1053 |
InstrumentName: ptr("test_name"), |
| 1054 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 1055 |
Unit: ptr("test_unit"), |
| 1056 |
MeterName: ptr("test_meter_name"), |
| 1057 |
MeterVersion: ptr("test_meter_version"), |
| 1058 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 1059 |
}, |
| 1060 |
}, |
| 1061 |
matchInstrument: &sdkmetric.Instrument{ |
| 1062 |
Name: "test_name", |
| 1063 |
Unit: "test_unit", |
| 1064 |
Kind: sdkmetric.InstrumentKindCounter, |
| 1065 |
Scope: instrumentation.Scope{ |
| 1066 |
Name: "test_meter_name", |
| 1067 |
Version: "not_match", |
| 1068 |
SchemaURL: "test_schema_url", |
| 1069 |
}, |
| 1070 |
}, |
| 1071 |
wantStream: sdkmetric.Stream{}, |
| 1072 |
wantResult: false, |
| 1073 |
}, |
| 1074 |
{ |
| 1075 |
name: "all selectors no match meter schema url", |
| 1076 |
view: View{ |
| 1077 |
Selector: &ViewSelector{ |
| 1078 |
InstrumentName: ptr("test_name"), |
| 1079 |
InstrumentType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 1080 |
Unit: ptr("test_unit"), |
| 1081 |
MeterName: ptr("test_meter_name"), |
| 1082 |
MeterVersion: ptr("test_meter_version"), |
| 1083 |
MeterSchemaUrl: ptr("test_schema_url"), |
| 1084 |
}, |
| 1085 |
}, |
| 1086 |
matchInstrument: &sdkmetric.Instrument{ |
| 1087 |
Name: "test_name", |
| 1088 |
Unit: "test_unit", |
| 1089 |
Kind: sdkmetric.InstrumentKindCounter, |
| 1090 |
Scope: instrumentation.Scope{ |
| 1091 |
Name: "test_meter_name", |
| 1092 |
Version: "test_meter_version", |
| 1093 |
SchemaURL: "not_match", |
| 1094 |
}, |
| 1095 |
}, |
| 1096 |
wantStream: sdkmetric.Stream{}, |
| 1097 |
wantResult: false, |
| 1098 |
}, |
| 1099 |
{ |
| 1100 |
name: "with stream", |
| 1101 |
view: View{ |
| 1102 |
Selector: &ViewSelector{ |
| 1103 |
InstrumentName: ptr("test_name"), |
| 1104 |
Unit: ptr("test_unit"), |
| 1105 |
}, |
| 1106 |
Stream: &ViewStream{ |
| 1107 |
Name: ptr("new_name"), |
| 1108 |
Description: ptr("new_description"), |
| 1109 |
AttributeKeys: ptr(IncludeExclude{Included: []string{"foo", "bar"}}), |
| 1110 |
Aggregation: &ViewStreamAggregation{Sum: make(ViewStreamAggregationSum)}, |
| 1111 |
}, |
| 1112 |
}, |
| 1113 |
matchInstrument: &sdkmetric.Instrument{ |
| 1114 |
Name: "test_name", |
| 1115 |
Description: "test_description", |
| 1116 |
Unit: "test_unit", |
| 1117 |
}, |
| 1118 |
wantStream: sdkmetric.Stream{ |
| 1119 |
Name: "new_name", |
| 1120 |
Description: "new_description", |
| 1121 |
Unit: "test_unit", |
| 1122 |
Aggregation: sdkmetric.AggregationSum{}, |
| 1123 |
}, |
| 1124 |
wantResult: true, |
| 1125 |
}, |
| 1126 |
} |
| 1127 |
for _, tt := range testCases { |
| 1128 |
t.Run(tt.name, func(t *testing.T) { |
| 1129 |
got, err := view(tt.view) |
| 1130 |
if tt.wantErr != "" { |
| 1131 |
require.EqualError(t, err, tt.wantErr) |
| 1132 |
require.Nil(t, got) |
| 1133 |
} else { |
| 1134 |
require.NoError(t, err) |
| 1135 |
gotStream, gotResult := got(*tt.matchInstrument) |
| 1136 |
|
| 1137 |
gotStream.AttributeFilter = nil |
| 1138 |
require.Equal(t, tt.wantStream, gotStream) |
| 1139 |
require.Equal(t, tt.wantResult, gotResult) |
| 1140 |
} |
| 1141 |
}) |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
func TestInstrumentType(t *testing.T) { |
| 1146 |
testCases := []struct { |
| 1147 |
name string |
| 1148 |
instType *ViewSelectorInstrumentType |
| 1149 |
wantErr error |
| 1150 |
wantKind sdkmetric.InstrumentKind |
| 1151 |
}{ |
| 1152 |
{ |
| 1153 |
name: "nil", |
| 1154 |
wantKind: sdkmetric.InstrumentKind(0), |
| 1155 |
}, |
| 1156 |
{ |
| 1157 |
name: "counter", |
| 1158 |
instType: (*ViewSelectorInstrumentType)(ptr("counter")), |
| 1159 |
wantKind: sdkmetric.InstrumentKindCounter, |
| 1160 |
}, |
| 1161 |
{ |
| 1162 |
name: "up_down_counter", |
| 1163 |
instType: (*ViewSelectorInstrumentType)(ptr("up_down_counter")), |
| 1164 |
wantKind: sdkmetric.InstrumentKindUpDownCounter, |
| 1165 |
}, |
| 1166 |
{ |
| 1167 |
name: "histogram", |
| 1168 |
instType: (*ViewSelectorInstrumentType)(ptr("histogram")), |
| 1169 |
wantKind: sdkmetric.InstrumentKindHistogram, |
| 1170 |
}, |
| 1171 |
{ |
| 1172 |
name: "observable_counter", |
| 1173 |
instType: (*ViewSelectorInstrumentType)(ptr("observable_counter")), |
| 1174 |
wantKind: sdkmetric.InstrumentKindObservableCounter, |
| 1175 |
}, |
| 1176 |
{ |
| 1177 |
name: "observable_up_down_counter", |
| 1178 |
instType: (*ViewSelectorInstrumentType)(ptr("observable_up_down_counter")), |
| 1179 |
wantKind: sdkmetric.InstrumentKindObservableUpDownCounter, |
| 1180 |
}, |
| 1181 |
{ |
| 1182 |
name: "observable_gauge", |
| 1183 |
instType: (*ViewSelectorInstrumentType)(ptr("observable_gauge")), |
| 1184 |
wantKind: sdkmetric.InstrumentKindObservableGauge, |
| 1185 |
}, |
| 1186 |
{ |
| 1187 |
name: "invalid", |
| 1188 |
instType: (*ViewSelectorInstrumentType)(ptr("invalid")), |
| 1189 |
wantErr: errors.New("instrument_type: invalid value"), |
| 1190 |
}, |
| 1191 |
} |
| 1192 |
for _, tt := range testCases { |
| 1193 |
t.Run(tt.name, func(t *testing.T) { |
| 1194 |
got, err := instrumentKind(tt.instType) |
| 1195 |
if tt.wantErr != nil { |
| 1196 |
require.Equal(t, tt.wantErr, err) |
| 1197 |
require.Zero(t, got) |
| 1198 |
} else { |
| 1199 |
require.NoError(t, err) |
| 1200 |
require.Equal(t, tt.wantKind, got) |
| 1201 |
} |
| 1202 |
}) |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
func TestAggregation(t *testing.T) { |
| 1207 |
testCases := []struct { |
| 1208 |
name string |
| 1209 |
aggregation *ViewStreamAggregation |
| 1210 |
wantAggregation sdkmetric.Aggregation |
| 1211 |
}{ |
| 1212 |
{ |
| 1213 |
name: "nil", |
| 1214 |
wantAggregation: nil, |
| 1215 |
}, |
| 1216 |
{ |
| 1217 |
name: "empty", |
| 1218 |
aggregation: &ViewStreamAggregation{}, |
| 1219 |
wantAggregation: nil, |
| 1220 |
}, |
| 1221 |
{ |
| 1222 |
name: "Base2ExponentialBucketHistogram empty", |
| 1223 |
aggregation: &ViewStreamAggregation{ |
| 1224 |
Base2ExponentialBucketHistogram: &ViewStreamAggregationBase2ExponentialBucketHistogram{}, |
| 1225 |
}, |
| 1226 |
wantAggregation: sdkmetric.AggregationBase2ExponentialHistogram{ |
| 1227 |
MaxSize: 0, |
| 1228 |
MaxScale: 0, |
| 1229 |
NoMinMax: true, |
| 1230 |
}, |
| 1231 |
}, |
| 1232 |
{ |
| 1233 |
name: "Base2ExponentialBucketHistogram", |
| 1234 |
aggregation: &ViewStreamAggregation{ |
| 1235 |
Base2ExponentialBucketHistogram: &ViewStreamAggregationBase2ExponentialBucketHistogram{ |
| 1236 |
MaxSize: ptr(2), |
| 1237 |
MaxScale: ptr(3), |
| 1238 |
RecordMinMax: ptr(true), |
| 1239 |
}, |
| 1240 |
}, |
| 1241 |
wantAggregation: sdkmetric.AggregationBase2ExponentialHistogram{ |
| 1242 |
MaxSize: 2, |
| 1243 |
MaxScale: 3, |
| 1244 |
NoMinMax: false, |
| 1245 |
}, |
| 1246 |
}, |
| 1247 |
{ |
| 1248 |
name: "Default", |
| 1249 |
aggregation: &ViewStreamAggregation{ |
| 1250 |
Default: make(ViewStreamAggregationDefault), |
| 1251 |
}, |
| 1252 |
wantAggregation: nil, |
| 1253 |
}, |
| 1254 |
{ |
| 1255 |
name: "Drop", |
| 1256 |
aggregation: &ViewStreamAggregation{ |
| 1257 |
Drop: make(ViewStreamAggregationDrop), |
| 1258 |
}, |
| 1259 |
wantAggregation: sdkmetric.AggregationDrop{}, |
| 1260 |
}, |
| 1261 |
{ |
| 1262 |
name: "ExplicitBucketHistogram empty", |
| 1263 |
aggregation: &ViewStreamAggregation{ |
| 1264 |
ExplicitBucketHistogram: &ViewStreamAggregationExplicitBucketHistogram{}, |
| 1265 |
}, |
| 1266 |
wantAggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 1267 |
Boundaries: nil, |
| 1268 |
NoMinMax: true, |
| 1269 |
}, |
| 1270 |
}, |
| 1271 |
{ |
| 1272 |
name: "ExplicitBucketHistogram", |
| 1273 |
aggregation: &ViewStreamAggregation{ |
| 1274 |
ExplicitBucketHistogram: &ViewStreamAggregationExplicitBucketHistogram{ |
| 1275 |
Boundaries: []float64{1, 2, 3}, |
| 1276 |
RecordMinMax: ptr(true), |
| 1277 |
}, |
| 1278 |
}, |
| 1279 |
wantAggregation: sdkmetric.AggregationExplicitBucketHistogram{ |
| 1280 |
Boundaries: []float64{1, 2, 3}, |
| 1281 |
NoMinMax: false, |
| 1282 |
}, |
| 1283 |
}, |
| 1284 |
{ |
| 1285 |
name: "LastValue", |
| 1286 |
aggregation: &ViewStreamAggregation{ |
| 1287 |
LastValue: make(ViewStreamAggregationLastValue), |
| 1288 |
}, |
| 1289 |
wantAggregation: sdkmetric.AggregationLastValue{}, |
| 1290 |
}, |
| 1291 |
{ |
| 1292 |
name: "Sum", |
| 1293 |
aggregation: &ViewStreamAggregation{ |
| 1294 |
Sum: make(ViewStreamAggregationSum), |
| 1295 |
}, |
| 1296 |
wantAggregation: sdkmetric.AggregationSum{}, |
| 1297 |
}, |
| 1298 |
} |
| 1299 |
for _, tt := range testCases { |
| 1300 |
t.Run(tt.name, func(t *testing.T) { |
| 1301 |
got := aggregation(tt.aggregation) |
| 1302 |
require.Equal(t, tt.wantAggregation, got) |
| 1303 |
}) |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
func TestNewIncludeExcludeFilter(t *testing.T) { |
| 1308 |
testCases := []struct { |
| 1309 |
name string |
| 1310 |
attributeKeys *IncludeExclude |
| 1311 |
wantPass []string |
| 1312 |
wantFail []string |
| 1313 |
}{ |
| 1314 |
{ |
| 1315 |
name: "empty", |
| 1316 |
attributeKeys: nil, |
| 1317 |
wantPass: []string{"foo", "bar"}, |
| 1318 |
wantFail: nil, |
| 1319 |
}, |
| 1320 |
{ |
| 1321 |
name: "filter-with-include", |
| 1322 |
attributeKeys: ptr(IncludeExclude{ |
| 1323 |
Included: []string{"foo"}, |
| 1324 |
}), |
| 1325 |
wantPass: []string{"foo"}, |
| 1326 |
wantFail: []string{"bar"}, |
| 1327 |
}, |
| 1328 |
{ |
| 1329 |
name: "filter-with-exclude", |
| 1330 |
attributeKeys: ptr(IncludeExclude{ |
| 1331 |
Excluded: []string{"foo"}, |
| 1332 |
}), |
| 1333 |
wantPass: []string{"bar"}, |
| 1334 |
wantFail: []string{"foo"}, |
| 1335 |
}, |
| 1336 |
{ |
| 1337 |
name: "filter-with-include-and-exclude", |
| 1338 |
attributeKeys: ptr(IncludeExclude{ |
| 1339 |
Included: []string{"bar"}, |
| 1340 |
Excluded: []string{"foo"}, |
| 1341 |
}), |
| 1342 |
wantPass: []string{"bar"}, |
| 1343 |
wantFail: []string{"foo"}, |
| 1344 |
}, |
| 1345 |
} |
| 1346 |
for _, tt := range testCases { |
| 1347 |
t.Run(tt.name, func(t *testing.T) { |
| 1348 |
got, err := newIncludeExcludeFilter(tt.attributeKeys) |
| 1349 |
require.NoError(t, err) |
| 1350 |
for _, pass := range tt.wantPass { |
| 1351 |
require.True(t, got(attribute.KeyValue{Key: attribute.Key(pass), Value: attribute.StringValue("")})) |
| 1352 |
} |
| 1353 |
for _, fail := range tt.wantFail { |
| 1354 |
require.False(t, got(attribute.KeyValue{Key: attribute.Key(fail), Value: attribute.StringValue("")})) |
| 1355 |
} |
| 1356 |
}) |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
func TestNewIncludeExcludeFilterError(t *testing.T) { |
| 1361 |
_, err := newIncludeExcludeFilter(ptr(IncludeExclude{ |
| 1362 |
Included: []string{"foo"}, |
| 1363 |
Excluded: []string{"foo"}, |
| 1364 |
})) |
| 1365 |
require.Equal(t, fmt.Errorf("attribute cannot be in both include and exclude list: foo"), err) |
| 1366 |
} |
| 1367 |
|
| 1368 |
func TestPrometheusReaderOpts(t *testing.T) { |
| 1369 |
testCases := []struct { |
| 1370 |
name string |
| 1371 |
cfg Prometheus |
| 1372 |
wantOptions int |
| 1373 |
}{ |
| 1374 |
{ |
| 1375 |
name: "no options", |
| 1376 |
cfg: Prometheus{}, |
| 1377 |
wantOptions: 1, |
| 1378 |
}, |
| 1379 |
{ |
| 1380 |
name: "all set", |
| 1381 |
cfg: Prometheus{ |
| 1382 |
WithoutScopeInfo: ptr(true), |
| 1383 |
WithoutTypeSuffix: ptr(true), |
| 1384 |
WithoutUnits: ptr(true), |
| 1385 |
WithResourceConstantLabels: &IncludeExclude{}, |
| 1386 |
}, |
| 1387 |
wantOptions: 3, |
| 1388 |
}, |
| 1389 |
{ |
| 1390 |
name: "all set false", |
| 1391 |
cfg: Prometheus{ |
| 1392 |
WithoutScopeInfo: ptr(false), |
| 1393 |
WithoutTypeSuffix: ptr(false), |
| 1394 |
WithoutUnits: ptr(false), |
| 1395 |
WithResourceConstantLabels: &IncludeExclude{}, |
| 1396 |
}, |
| 1397 |
wantOptions: 2, |
| 1398 |
}, |
| 1399 |
} |
| 1400 |
for _, tt := range testCases { |
| 1401 |
t.Run(tt.name, func(t *testing.T) { |
| 1402 |
opts, err := prometheusReaderOpts(&tt.cfg) |
| 1403 |
require.NoError(t, err) |
| 1404 |
require.Len(t, opts, tt.wantOptions) |
| 1405 |
}) |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
func TestPrometheusIPv6(t *testing.T) { |
| 1410 |
tests := []struct { |
| 1411 |
name string |
| 1412 |
host string |
| 1413 |
}{ |
| 1414 |
{ |
| 1415 |
name: "IPv6", |
| 1416 |
host: "::1", |
| 1417 |
}, |
| 1418 |
{ |
| 1419 |
name: "[IPv6]", |
| 1420 |
host: "[::1]", |
| 1421 |
}, |
| 1422 |
} |
| 1423 |
for _, tt := range tests { |
| 1424 |
t.Run(tt.name, func(t *testing.T) { |
| 1425 |
port := 0 |
| 1426 |
cfg := Prometheus{ |
| 1427 |
Host: &tt.host, |
| 1428 |
Port: &port, |
| 1429 |
WithoutScopeInfo: ptr(true), |
| 1430 |
WithoutTypeSuffix: ptr(true), |
| 1431 |
WithoutUnits: ptr(true), |
| 1432 |
WithResourceConstantLabels: &IncludeExclude{}, |
| 1433 |
} |
| 1434 |
|
| 1435 |
rs, err := prometheusReader(t.Context(), &cfg) |
| 1436 |
t.Cleanup(func() { |
| 1437 |
|
| 1438 |
require.NoError(t, rs.Shutdown(context.Background())) |
| 1439 |
}) |
| 1440 |
require.NoError(t, err) |
| 1441 |
|
| 1442 |
hServ := rs.(readerWithServer).server |
| 1443 |
assert.True(t, strings.HasPrefix(hServ.Addr, "[::1]:")) |
| 1444 |
|
| 1445 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+hServ.Addr+"/metrics", http.NoBody) |
| 1446 |
require.NoError(t, err) |
| 1447 |
|
| 1448 |
resp, err := http.DefaultClient.Do(req) |
| 1449 |
t.Cleanup(func() { |
| 1450 |
require.NoError(t, resp.Body.Close()) |
| 1451 |
}) |
| 1452 |
require.NoError(t, err) |
| 1453 |
assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 1454 |
}) |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
func TestPrometheusReaderErrorCases(t *testing.T) { |
| 1459 |
tests := []struct { |
| 1460 |
name string |
| 1461 |
config Prometheus |
| 1462 |
errMsg string |
| 1463 |
}{ |
| 1464 |
{ |
| 1465 |
name: "missing host", |
| 1466 |
config: Prometheus{Port: ptr(8080)}, |
| 1467 |
errMsg: "host must be specified", |
| 1468 |
}, |
| 1469 |
{ |
| 1470 |
name: "missing port", |
| 1471 |
config: Prometheus{Host: ptr("localhost")}, |
| 1472 |
errMsg: "port must be specified", |
| 1473 |
}, |
| 1474 |
{ |
| 1475 |
name: "invalid port", |
| 1476 |
config: Prometheus{ |
| 1477 |
Host: ptr("localhost"), |
| 1478 |
Port: ptr(99999), |
| 1479 |
WithoutScopeInfo: ptr(true), |
| 1480 |
WithoutTypeSuffix: ptr(true), |
| 1481 |
WithoutUnits: ptr(true), |
| 1482 |
WithResourceConstantLabels: &IncludeExclude{}, |
| 1483 |
}, |
| 1484 |
errMsg: "binding address", |
| 1485 |
}, |
| 1486 |
} |
| 1487 |
|
| 1488 |
for _, tt := range tests { |
| 1489 |
t.Run(tt.name, func(t *testing.T) { |
| 1490 |
reader, err := prometheusReader(t.Context(), &tt.config) |
| 1491 |
assert.ErrorContains(t, err, tt.errMsg) |
| 1492 |
assert.Nil(t, reader) |
| 1493 |
}) |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
func TestPrometheusReaderHostParsing(t *testing.T) { |
| 1498 |
tests := []struct { |
| 1499 |
name string |
| 1500 |
host string |
| 1501 |
wantAddr string |
| 1502 |
}{ |
| 1503 |
{ |
| 1504 |
name: "regular host", |
| 1505 |
host: "localhost", |
| 1506 |
wantAddr: "127.0.0.1", |
| 1507 |
}, |
| 1508 |
{ |
| 1509 |
name: "IPv4", |
| 1510 |
host: "127.0.0.1", |
| 1511 |
wantAddr: "127.0.0.1", |
| 1512 |
}, |
| 1513 |
{ |
| 1514 |
name: "IPv6 with brackets", |
| 1515 |
host: "[::1]", |
| 1516 |
wantAddr: "::1", |
| 1517 |
}, |
| 1518 |
{ |
| 1519 |
name: "IPv6 without brackets", |
| 1520 |
host: "::1", |
| 1521 |
wantAddr: "::1", |
| 1522 |
}, |
| 1523 |
} |
| 1524 |
|
| 1525 |
for _, tt := range tests { |
| 1526 |
t.Run(tt.name, func(t *testing.T) { |
| 1527 |
port := 0 |
| 1528 |
cfg := Prometheus{ |
| 1529 |
Host: &tt.host, |
| 1530 |
Port: &port, |
| 1531 |
WithoutScopeInfo: ptr(true), |
| 1532 |
WithoutTypeSuffix: ptr(true), |
| 1533 |
WithoutUnits: ptr(true), |
| 1534 |
WithResourceConstantLabels: &IncludeExclude{}, |
| 1535 |
} |
| 1536 |
|
| 1537 |
reader, err := prometheusReader(t.Context(), &cfg) |
| 1538 |
require.NoError(t, err) |
| 1539 |
require.NotNil(t, reader) |
| 1540 |
|
| 1541 |
t.Cleanup(func() { |
| 1542 |
|
| 1543 |
require.NoError(t, reader.Shutdown(context.Background())) |
| 1544 |
}) |
| 1545 |
|
| 1546 |
rws, ok := reader.(readerWithServer) |
| 1547 |
require.True(t, ok, "reader is not a readerWithServer") |
| 1548 |
server := rws.server |
| 1549 |
|
| 1550 |
assert.Contains(t, server.Addr, tt.wantAddr) |
| 1551 |
}) |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
func TestPrometheusReaderConfigurationOptions(t *testing.T) { |
| 1556 |
host := "localhost" |
| 1557 |
port := 0 |
| 1558 |
cfg := &Prometheus{ |
| 1559 |
Host: &host, |
| 1560 |
Port: &port, |
| 1561 |
WithoutScopeInfo: ptr(true), |
| 1562 |
WithoutTypeSuffix: ptr(true), |
| 1563 |
WithoutUnits: ptr(true), |
| 1564 |
WithResourceConstantLabels: &IncludeExclude{ |
| 1565 |
Included: []string{"service.name"}, |
| 1566 |
Excluded: []string{"host.name"}, |
| 1567 |
}, |
| 1568 |
} |
| 1569 |
|
| 1570 |
reader, err := prometheusReader(t.Context(), cfg) |
| 1571 |
require.NoError(t, err) |
| 1572 |
require.NotNil(t, reader) |
| 1573 |
|
| 1574 |
t.Cleanup(func() { |
| 1575 |
|
| 1576 |
require.NoError(t, reader.Shutdown(context.Background())) |
| 1577 |
}) |
| 1578 |
|
| 1579 |
rws, ok := reader.(readerWithServer) |
| 1580 |
require.True(t, ok, "reader is not a readerWithServer") |
| 1581 |
server := rws.server |
| 1582 |
|
| 1583 |
addr := server.Addr |
| 1584 |
|
| 1585 |
assert.Contains(t, addr, "127.0.0.1") |
| 1586 |
|
| 1587 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+addr+"/metrics", http.NoBody) |
| 1588 |
require.NoError(t, err) |
| 1589 |
|
| 1590 |
resp, err := http.DefaultClient.Do(req) |
| 1591 |
require.NoError(t, err) |
| 1592 |
defer resp.Body.Close() |
| 1593 |
|
| 1594 |
assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 1595 |
} |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
func TestPrometheusReaderDotStyleLabels(t *testing.T) { |
| 1601 |
host := "localhost" |
| 1602 |
port := 0 |
| 1603 |
reader, err := prometheusReader(t.Context(), &Prometheus{ |
| 1604 |
Host: &host, |
| 1605 |
Port: &port, |
| 1606 |
WithoutTypeSuffix: ptr(true), |
| 1607 |
WithoutUnits: ptr(true), |
| 1608 |
}) |
| 1609 |
require.NoError(t, err) |
| 1610 |
|
| 1611 |
res := resource.NewWithAttributes("", attribute.String("service.name", "test-svc")) |
| 1612 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader), sdkmetric.WithResource(res)) |
| 1613 |
t.Cleanup(func() { |
| 1614 |
|
| 1615 |
require.NoError(t, mp.Shutdown(context.Background())) |
| 1616 |
}) |
| 1617 |
c, err := mp.Meter("test").Int64Counter("test.counter") |
| 1618 |
require.NoError(t, err) |
| 1619 |
c.Add(t.Context(), 1) |
| 1620 |
|
| 1621 |
addr := reader.(readerWithServer).server.Addr |
| 1622 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://"+addr+"/metrics", http.NoBody) |
| 1623 |
require.NoError(t, err) |
| 1624 |
req.Header.Set("Accept", "application/openmetrics-text; version=1.0.0; escaping=allow-utf-8") |
| 1625 |
resp, err := http.DefaultClient.Do(req) |
| 1626 |
require.NoError(t, err) |
| 1627 |
defer resp.Body.Close() |
| 1628 |
|
| 1629 |
var buf bytes.Buffer |
| 1630 |
_, err = buf.ReadFrom(resp.Body) |
| 1631 |
require.NoError(t, err) |
| 1632 |
body := buf.String() |
| 1633 |
|
| 1634 |
assert.Contains(t, body, `target_info{"service.name"="test-svc"}`) |
| 1635 |
assert.NotContains(t, body, "service_name") |
| 1636 |
assert.NotContains(t, body, "target.info") |
| 1637 |
} |
| 1638 |
|
| 1639 |
func Test_otlpGRPCMetricExporter(t *testing.T) { |
| 1640 |
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { |
| 1641 |
|
| 1642 |
t.Skip("Test is flaky on Windows and MacOS.") |
| 1643 |
} |
| 1644 |
type args struct { |
| 1645 |
ctx context.Context |
| 1646 |
otlpConfig *OTLPMetric |
| 1647 |
} |
| 1648 |
tests := []struct { |
| 1649 |
name string |
| 1650 |
args args |
| 1651 |
grpcServerOpts func() ([]grpc.ServerOption, error) |
| 1652 |
}{ |
| 1653 |
{ |
| 1654 |
name: "no TLS config", |
| 1655 |
args: args{ |
| 1656 |
ctx: t.Context(), |
| 1657 |
otlpConfig: &OTLPMetric{ |
| 1658 |
Protocol: ptr("grpc"), |
| 1659 |
Compression: ptr("gzip"), |
| 1660 |
Timeout: ptr(5000), |
| 1661 |
Insecure: ptr(true), |
| 1662 |
Headers: []NameStringValuePair{ |
| 1663 |
{Name: "test", Value: ptr("test1")}, |
| 1664 |
}, |
| 1665 |
}, |
| 1666 |
}, |
| 1667 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 1668 |
return []grpc.ServerOption{}, nil |
| 1669 |
}, |
| 1670 |
}, |
| 1671 |
{ |
| 1672 |
name: "with TLS config", |
| 1673 |
args: args{ |
| 1674 |
ctx: t.Context(), |
| 1675 |
otlpConfig: &OTLPMetric{ |
| 1676 |
Protocol: ptr("grpc"), |
| 1677 |
Compression: ptr("gzip"), |
| 1678 |
Timeout: ptr(5000), |
| 1679 |
Certificate: ptr("testdata/server-certs/server.crt"), |
| 1680 |
Headers: []NameStringValuePair{ |
| 1681 |
{Name: "test", Value: ptr("test1")}, |
| 1682 |
}, |
| 1683 |
}, |
| 1684 |
}, |
| 1685 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 1686 |
opts := []grpc.ServerOption{} |
| 1687 |
tlsCreds, err := credentials.NewServerTLSFromFile("testdata/server-certs/server.crt", "testdata/server-certs/server.key") |
| 1688 |
if err != nil { |
| 1689 |
return nil, err |
| 1690 |
} |
| 1691 |
opts = append(opts, grpc.Creds(tlsCreds)) |
| 1692 |
return opts, nil |
| 1693 |
}, |
| 1694 |
}, |
| 1695 |
{ |
| 1696 |
name: "with TLS config and client key", |
| 1697 |
args: args{ |
| 1698 |
ctx: t.Context(), |
| 1699 |
otlpConfig: &OTLPMetric{ |
| 1700 |
Protocol: ptr("grpc"), |
| 1701 |
Compression: ptr("gzip"), |
| 1702 |
Timeout: ptr(5000), |
| 1703 |
Certificate: ptr("testdata/server-certs/server.crt"), |
| 1704 |
ClientKey: ptr("testdata/client-certs/client.key"), |
| 1705 |
ClientCertificate: ptr("testdata/client-certs/client.crt"), |
| 1706 |
Headers: []NameStringValuePair{ |
| 1707 |
{Name: "test", Value: ptr("test1")}, |
| 1708 |
}, |
| 1709 |
}, |
| 1710 |
}, |
| 1711 |
grpcServerOpts: func() ([]grpc.ServerOption, error) { |
| 1712 |
opts := []grpc.ServerOption{} |
| 1713 |
cert, err := tls.LoadX509KeyPair("testdata/server-certs/server.crt", "testdata/server-certs/server.key") |
| 1714 |
if err != nil { |
| 1715 |
return nil, err |
| 1716 |
} |
| 1717 |
caCert, err := os.ReadFile("testdata/ca.crt") |
| 1718 |
if err != nil { |
| 1719 |
return nil, err |
| 1720 |
} |
| 1721 |
caCertPool := x509.NewCertPool() |
| 1722 |
caCertPool.AppendCertsFromPEM(caCert) |
| 1723 |
tlsCreds := credentials.NewTLS(&tls.Config{ |
| 1724 |
Certificates: []tls.Certificate{cert}, |
| 1725 |
ClientCAs: caCertPool, |
| 1726 |
ClientAuth: tls.RequireAndVerifyClientCert, |
| 1727 |
}) |
| 1728 |
opts = append(opts, grpc.Creds(tlsCreds)) |
| 1729 |
return opts, nil |
| 1730 |
}, |
| 1731 |
}, |
| 1732 |
} |
| 1733 |
for _, tt := range tests { |
| 1734 |
t.Run(tt.name, func(t *testing.T) { |
| 1735 |
n, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp4", "localhost:0") |
| 1736 |
require.NoError(t, err) |
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
_, port, err := net.SplitHostPort(n.Addr().String()) |
| 1744 |
require.NoError(t, err) |
| 1745 |
tt.args.otlpConfig.Endpoint = ptr("localhost:" + port) |
| 1746 |
|
| 1747 |
serverOpts, err := tt.grpcServerOpts() |
| 1748 |
require.NoError(t, err) |
| 1749 |
|
| 1750 |
startGRPCMetricCollector(t, n, serverOpts) |
| 1751 |
|
| 1752 |
exporter, err := otlpGRPCMetricExporter(tt.args.ctx, tt.args.otlpConfig) |
| 1753 |
require.NoError(t, err) |
| 1754 |
|
| 1755 |
res, err := resource.New(t.Context()) |
| 1756 |
require.NoError(t, err) |
| 1757 |
|
| 1758 |
assert.EventuallyWithT(t, func(collect *assert.CollectT) { |
| 1759 |
assert.NoError(collect, exporter.Export(context.Background(), &metricdata.ResourceMetrics{ |
| 1760 |
Resource: res, |
| 1761 |
ScopeMetrics: []metricdata.ScopeMetrics{ |
| 1762 |
{ |
| 1763 |
Metrics: []metricdata.Metrics{ |
| 1764 |
{ |
| 1765 |
Name: "test-metric", |
| 1766 |
Data: metricdata.Gauge[int64]{ |
| 1767 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 1768 |
{ |
| 1769 |
Value: 1, |
| 1770 |
}, |
| 1771 |
}, |
| 1772 |
}, |
| 1773 |
}, |
| 1774 |
}, |
| 1775 |
}, |
| 1776 |
}, |
| 1777 |
})) |
| 1778 |
}, 10*time.Second, 1*time.Second) |
| 1779 |
}) |
| 1780 |
} |
| 1781 |
} |
| 1782 |
|
| 1783 |
|
| 1784 |
type grpcMetricCollector struct { |
| 1785 |
v1.UnimplementedMetricsServiceServer |
| 1786 |
} |
| 1787 |
|
| 1788 |
var _ v1.MetricsServiceServer = (*grpcMetricCollector)(nil) |
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
func startGRPCMetricCollector(t *testing.T, listener net.Listener, serverOptions []grpc.ServerOption) { |
| 1796 |
srv := grpc.NewServer(serverOptions...) |
| 1797 |
c := &grpcMetricCollector{} |
| 1798 |
|
| 1799 |
v1.RegisterMetricsServiceServer(srv, c) |
| 1800 |
|
| 1801 |
errCh := make(chan error, 1) |
| 1802 |
go func() { errCh <- srv.Serve(listener) }() |
| 1803 |
|
| 1804 |
t.Cleanup(func() { |
| 1805 |
srv.GracefulStop() |
| 1806 |
if err := <-errCh; err != nil && !errors.Is(err, grpc.ErrServerStopped) { |
| 1807 |
assert.NoError(t, err) |
| 1808 |
} |
| 1809 |
}) |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
func (*grpcMetricCollector) Export( |
| 1814 |
_ context.Context, |
| 1815 |
_ *v1.ExportMetricsServiceRequest, |
| 1816 |
) (*v1.ExportMetricsServiceResponse, error) { |
| 1817 |
return &v1.ExportMetricsServiceResponse{}, nil |
| 1818 |
} |