| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlploggrpc // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"net" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/google/go-cmp/cmp" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
collogpb "go.opentelemetry.io/proto/otlp/collector/logs/v1" |
| 18 |
cpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 19 |
lpb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 20 |
rpb "go.opentelemetry.io/proto/otlp/resource/v1" |
| 21 |
"google.golang.org/genproto/googleapis/rpc/errdetails" |
| 22 |
"google.golang.org/grpc" |
| 23 |
"google.golang.org/grpc/codes" |
| 24 |
"google.golang.org/grpc/credentials" |
| 25 |
"google.golang.org/grpc/credentials/insecure" |
| 26 |
"google.golang.org/grpc/metadata" |
| 27 |
"google.golang.org/grpc/status" |
| 28 |
"google.golang.org/protobuf/proto" |
| 29 |
"google.golang.org/protobuf/types/known/durationpb" |
| 30 |
|
| 31 |
"go.opentelemetry.io/otel" |
| 32 |
"go.opentelemetry.io/otel/attribute" |
| 33 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal" |
| 34 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal/observ" |
| 35 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 36 |
"go.opentelemetry.io/otel/sdk/log" |
| 37 |
"go.opentelemetry.io/otel/sdk/metric" |
| 38 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 39 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 40 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 41 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 42 |
) |
| 43 |
|
| 44 |
var ( |
| 45 |
// Sat Jan 01 2000 00:00:00 GMT+0000. |
| 46 |
ts = time.Date(2000, time.January, 0o1, 0, 0, 0, 0, time.FixedZone("GMT", 0)) |
| 47 |
obs = ts.Add(30 * time.Second) |
| 48 |
|
| 49 |
kvAlice = &cpb.KeyValue{Key: "user", Value: &cpb.AnyValue{ |
| 50 |
Value: &cpb.AnyValue_StringValue{StringValue: "alice"}, |
| 51 |
}} |
| 52 |
kvBob = &cpb.KeyValue{Key: "user", Value: &cpb.AnyValue{ |
| 53 |
Value: &cpb.AnyValue_StringValue{StringValue: "bob"}, |
| 54 |
}} |
| 55 |
kvSrvName = &cpb.KeyValue{Key: "service.name", Value: &cpb.AnyValue{ |
| 56 |
Value: &cpb.AnyValue_StringValue{StringValue: "test server"}, |
| 57 |
}} |
| 58 |
kvSrvVer = &cpb.KeyValue{Key: "service.version", Value: &cpb.AnyValue{ |
| 59 |
Value: &cpb.AnyValue_StringValue{StringValue: "v0.1.0"}, |
| 60 |
}} |
| 61 |
|
| 62 |
pbSevA = lpb.SeverityNumber_SEVERITY_NUMBER_INFO |
| 63 |
pbSevB = lpb.SeverityNumber_SEVERITY_NUMBER_ERROR |
| 64 |
|
| 65 |
pbBodyA = &cpb.AnyValue{ |
| 66 |
Value: &cpb.AnyValue_StringValue{ |
| 67 |
StringValue: "a", |
| 68 |
}, |
| 69 |
} |
| 70 |
pbBodyB = &cpb.AnyValue{ |
| 71 |
Value: &cpb.AnyValue_StringValue{ |
| 72 |
StringValue: "b", |
| 73 |
}, |
| 74 |
} |
| 75 |
|
| 76 |
spanIDA = []byte{0, 0, 0, 0, 0, 0, 0, 1} |
| 77 |
spanIDB = []byte{0, 0, 0, 0, 0, 0, 0, 2} |
| 78 |
traceIDA = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} |
| 79 |
traceIDB = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2} |
| 80 |
flagsA = byte(1) |
| 81 |
flagsB = byte(0) |
| 82 |
|
| 83 |
logRecords = []*lpb.LogRecord{ |
| 84 |
{ |
| 85 |
TimeUnixNano: uint64(ts.UnixNano()), |
| 86 |
ObservedTimeUnixNano: uint64(obs.UnixNano()), |
| 87 |
SeverityNumber: pbSevA, |
| 88 |
SeverityText: "A", |
| 89 |
Body: pbBodyA, |
| 90 |
Attributes: []*cpb.KeyValue{kvAlice}, |
| 91 |
Flags: uint32(flagsA), |
| 92 |
TraceId: traceIDA, |
| 93 |
SpanId: spanIDA, |
| 94 |
}, |
| 95 |
{ |
| 96 |
TimeUnixNano: uint64(ts.UnixNano()), |
| 97 |
ObservedTimeUnixNano: uint64(obs.UnixNano()), |
| 98 |
SeverityNumber: pbSevA, |
| 99 |
SeverityText: "A", |
| 100 |
Body: pbBodyA, |
| 101 |
Attributes: []*cpb.KeyValue{kvBob}, |
| 102 |
Flags: uint32(flagsA), |
| 103 |
TraceId: traceIDA, |
| 104 |
SpanId: spanIDA, |
| 105 |
}, |
| 106 |
{ |
| 107 |
TimeUnixNano: uint64(ts.UnixNano()), |
| 108 |
ObservedTimeUnixNano: uint64(obs.UnixNano()), |
| 109 |
SeverityNumber: pbSevB, |
| 110 |
SeverityText: "B", |
| 111 |
Body: pbBodyB, |
| 112 |
Attributes: []*cpb.KeyValue{kvAlice}, |
| 113 |
Flags: uint32(flagsB), |
| 114 |
TraceId: traceIDB, |
| 115 |
SpanId: spanIDB, |
| 116 |
}, |
| 117 |
{ |
| 118 |
TimeUnixNano: uint64(ts.UnixNano()), |
| 119 |
ObservedTimeUnixNano: uint64(obs.UnixNano()), |
| 120 |
SeverityNumber: pbSevB, |
| 121 |
SeverityText: "B", |
| 122 |
Body: pbBodyB, |
| 123 |
Attributes: []*cpb.KeyValue{kvBob}, |
| 124 |
Flags: uint32(flagsB), |
| 125 |
TraceId: traceIDB, |
| 126 |
SpanId: spanIDB, |
| 127 |
}, |
| 128 |
} |
| 129 |
|
| 130 |
scope = &cpb.InstrumentationScope{ |
| 131 |
Name: "test/code/path", |
| 132 |
Version: "v0.1.0", |
| 133 |
} |
| 134 |
scopeLogs = []*lpb.ScopeLogs{ |
| 135 |
{ |
| 136 |
Scope: scope, |
| 137 |
LogRecords: logRecords, |
| 138 |
SchemaUrl: semconv.SchemaURL, |
| 139 |
}, |
| 140 |
} |
| 141 |
|
| 142 |
res = &rpb.Resource{ |
| 143 |
Attributes: []*cpb.KeyValue{kvSrvName, kvSrvVer}, |
| 144 |
} |
| 145 |
resourceLogs = []*lpb.ResourceLogs{{ |
| 146 |
Resource: res, |
| 147 |
ScopeLogs: scopeLogs, |
| 148 |
SchemaUrl: semconv.SchemaURL, |
| 149 |
}} |
| 150 |
) |
| 151 |
|
| 152 |
func TestThrottleDelay(t *testing.T) { |
| 153 |
c := codes.ResourceExhausted |
| 154 |
testcases := []struct { |
| 155 |
status *status.Status |
| 156 |
wantOK bool |
| 157 |
wantDuration time.Duration |
| 158 |
}{ |
| 159 |
{ |
| 160 |
status: status.New(c, "NoRetryInfo"), |
| 161 |
wantOK: false, |
| 162 |
wantDuration: 0, |
| 163 |
}, |
| 164 |
{ |
| 165 |
status: func() *status.Status { |
| 166 |
s, err := status.New(c, "SingleRetryInfo").WithDetails( |
| 167 |
&errdetails.RetryInfo{ |
| 168 |
RetryDelay: durationpb.New(15 * time.Millisecond), |
| 169 |
}, |
| 170 |
) |
| 171 |
require.NoError(t, err) |
| 172 |
return s |
| 173 |
}(), |
| 174 |
wantOK: true, |
| 175 |
wantDuration: 15 * time.Millisecond, |
| 176 |
}, |
| 177 |
{ |
| 178 |
status: func() *status.Status { |
| 179 |
s, err := status.New(c, "ErrorInfo").WithDetails( |
| 180 |
&errdetails.ErrorInfo{Reason: "no throttle detail"}, |
| 181 |
) |
| 182 |
require.NoError(t, err) |
| 183 |
return s |
| 184 |
}(), |
| 185 |
wantOK: false, |
| 186 |
wantDuration: 0, |
| 187 |
}, |
| 188 |
{ |
| 189 |
status: func() *status.Status { |
| 190 |
s, err := status.New(c, "ErrorAndRetryInfo").WithDetails( |
| 191 |
&errdetails.ErrorInfo{Reason: "with throttle detail"}, |
| 192 |
&errdetails.RetryInfo{ |
| 193 |
RetryDelay: durationpb.New(13 * time.Minute), |
| 194 |
}, |
| 195 |
) |
| 196 |
require.NoError(t, err) |
| 197 |
return s |
| 198 |
}(), |
| 199 |
wantOK: true, |
| 200 |
wantDuration: 13 * time.Minute, |
| 201 |
}, |
| 202 |
{ |
| 203 |
status: func() *status.Status { |
| 204 |
s, err := status.New(c, "DoubleRetryInfo").WithDetails( |
| 205 |
&errdetails.RetryInfo{ |
| 206 |
RetryDelay: durationpb.New(13 * time.Minute), |
| 207 |
}, |
| 208 |
&errdetails.RetryInfo{ |
| 209 |
RetryDelay: durationpb.New(15 * time.Minute), |
| 210 |
}, |
| 211 |
) |
| 212 |
require.NoError(t, err) |
| 213 |
return s |
| 214 |
}(), |
| 215 |
wantOK: true, |
| 216 |
wantDuration: 13 * time.Minute, |
| 217 |
}, |
| 218 |
} |
| 219 |
|
| 220 |
for _, tc := range testcases { |
| 221 |
t.Run(tc.status.Message(), func(t *testing.T) { |
| 222 |
ok, d := throttleDelay(tc.status) |
| 223 |
assert.Equal(t, tc.wantOK, ok) |
| 224 |
assert.Equal(t, tc.wantDuration, d) |
| 225 |
}) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func TestRetryable(t *testing.T) { |
| 230 |
retryableCodes := map[codes.Code]bool{ |
| 231 |
codes.OK: false, |
| 232 |
codes.Canceled: true, |
| 233 |
codes.Unknown: false, |
| 234 |
codes.InvalidArgument: false, |
| 235 |
codes.DeadlineExceeded: true, |
| 236 |
codes.NotFound: false, |
| 237 |
codes.AlreadyExists: false, |
| 238 |
codes.PermissionDenied: false, |
| 239 |
codes.ResourceExhausted: false, |
| 240 |
codes.FailedPrecondition: false, |
| 241 |
codes.Aborted: true, |
| 242 |
codes.OutOfRange: true, |
| 243 |
codes.Unimplemented: false, |
| 244 |
codes.Internal: false, |
| 245 |
codes.Unavailable: true, |
| 246 |
codes.DataLoss: true, |
| 247 |
codes.Unauthenticated: false, |
| 248 |
} |
| 249 |
|
| 250 |
for c, want := range retryableCodes { |
| 251 |
got, _ := retryable(status.Error(c, "")) |
| 252 |
assert.Equalf(t, want, got, "evaluate(%s)", c) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func TestRetryableGRPCStatusResourceExhaustedWithRetryInfo(t *testing.T) { |
| 257 |
delay := 15 * time.Millisecond |
| 258 |
s, err := status.New(codes.ResourceExhausted, "WithRetryInfo").WithDetails( |
| 259 |
&errdetails.RetryInfo{ |
| 260 |
RetryDelay: durationpb.New(delay), |
| 261 |
}, |
| 262 |
) |
| 263 |
require.NoError(t, err) |
| 264 |
|
| 265 |
ok, d := retryableGRPCStatus(s) |
| 266 |
assert.True(t, ok) |
| 267 |
assert.Equal(t, delay, d) |
| 268 |
} |
| 269 |
|
| 270 |
func TestNewClient(t *testing.T) { |
| 271 |
newGRPCClientFnSwap := newGRPCClientFn |
| 272 |
t.Cleanup(func() { |
| 273 |
newGRPCClientFn = newGRPCClientFnSwap |
| 274 |
}) |
| 275 |
|
| 276 |
// The gRPC connection created by newClient. |
| 277 |
conn, err := grpc.NewClient("test", grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 278 |
require.NoError(t, err) |
| 279 |
newGRPCClientFn = func(string, ...grpc.DialOption) (*grpc.ClientConn, error) { |
| 280 |
return conn, nil |
| 281 |
} |
| 282 |
|
| 283 |
// The gRPC connection created by users. |
| 284 |
userConn, err := grpc.NewClient("test 2", grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 285 |
require.NoError(t, err) |
| 286 |
|
| 287 |
testCases := []struct { |
| 288 |
name string |
| 289 |
cfg config |
| 290 |
cli *client |
| 291 |
}{ |
| 292 |
{ |
| 293 |
name: "empty config", |
| 294 |
cli: &client{ |
| 295 |
ourConn: true, |
| 296 |
conn: conn, |
| 297 |
lsc: collogpb.NewLogsServiceClient(conn), |
| 298 |
}, |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "with headers", |
| 302 |
cfg: config{ |
| 303 |
headers: newSetting(map[string]string{ |
| 304 |
"key": "value", |
| 305 |
}), |
| 306 |
}, |
| 307 |
cli: &client{ |
| 308 |
ourConn: true, |
| 309 |
conn: conn, |
| 310 |
lsc: collogpb.NewLogsServiceClient(conn), |
| 311 |
metadata: map[string][]string{"key": {"value"}}, |
| 312 |
}, |
| 313 |
}, |
| 314 |
{ |
| 315 |
name: "with gRPC connection", |
| 316 |
cfg: config{ |
| 317 |
gRPCConn: newSetting(userConn), |
| 318 |
}, |
| 319 |
cli: &client{ |
| 320 |
ourConn: false, |
| 321 |
conn: userConn, |
| 322 |
lsc: collogpb.NewLogsServiceClient(userConn), |
| 323 |
}, |
| 324 |
}, |
| 325 |
{ |
| 326 |
// It is not possible to compare grpc dial options directly, so we just check that the client is created |
| 327 |
// and no panic occurs. |
| 328 |
name: "with dial options", |
| 329 |
cfg: config{ |
| 330 |
serviceConfig: newSetting("service config"), |
| 331 |
gRPCCredentials: newSetting(credentials.NewTLS(nil)), |
| 332 |
compression: newSetting(GzipCompression), |
| 333 |
reconnectionPeriod: newSetting(10 * time.Second), |
| 334 |
}, |
| 335 |
cli: &client{ |
| 336 |
ourConn: true, |
| 337 |
conn: conn, |
| 338 |
lsc: collogpb.NewLogsServiceClient(conn), |
| 339 |
}, |
| 340 |
}, |
| 341 |
} |
| 342 |
|
| 343 |
for _, tc := range testCases { |
| 344 |
t.Run(tc.name, func(t *testing.T) { |
| 345 |
cli, err := newClient(tc.cfg) |
| 346 |
require.NoError(t, err) |
| 347 |
|
| 348 |
assert.Equal(t, tc.cli.metadata, cli.metadata) |
| 349 |
assert.Equal(t, tc.cli.exportTimeout, cli.exportTimeout) |
| 350 |
assert.Equal(t, tc.cli.ourConn, cli.ourConn) |
| 351 |
assert.Equal(t, tc.cli.conn, cli.conn) |
| 352 |
assert.Equal(t, tc.cli.lsc, cli.lsc) |
| 353 |
}) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
type exportResult struct { |
| 358 |
Response *collogpb.ExportLogsServiceResponse |
| 359 |
Err error |
| 360 |
} |
| 361 |
|
| 362 |
// storage stores uploaded OTLP log data in their proto form. |
| 363 |
type storage struct { |
| 364 |
dataMu sync.Mutex |
| 365 |
data []*lpb.ResourceLogs |
| 366 |
} |
| 367 |
|
| 368 |
// newStorage returns a configure storage ready to store received requests. |
| 369 |
func newStorage() *storage { |
| 370 |
return &storage{} |
| 371 |
} |
| 372 |
|
| 373 |
// Add adds the request to the Storage. |
| 374 |
func (s *storage) Add(request *collogpb.ExportLogsServiceRequest) { |
| 375 |
s.dataMu.Lock() |
| 376 |
defer s.dataMu.Unlock() |
| 377 |
s.data = append(s.data, request.ResourceLogs...) |
| 378 |
} |
| 379 |
|
| 380 |
// Dump returns all added ResourceLogs and clears the storage. |
| 381 |
func (s *storage) Dump() []*lpb.ResourceLogs { |
| 382 |
s.dataMu.Lock() |
| 383 |
defer s.dataMu.Unlock() |
| 384 |
|
| 385 |
var data []*lpb.ResourceLogs |
| 386 |
data, s.data = s.data, []*lpb.ResourceLogs{} |
| 387 |
return data |
| 388 |
} |
| 389 |
|
| 390 |
// grpcCollector is an OTLP gRPC server that collects all requests it receives. |
| 391 |
type grpcCollector struct { |
| 392 |
collogpb.UnimplementedLogsServiceServer |
| 393 |
|
| 394 |
headersMu sync.Mutex |
| 395 |
headers metadata.MD |
| 396 |
storage *storage |
| 397 |
|
| 398 |
resultCh <-chan exportResult |
| 399 |
listener net.Listener |
| 400 |
srv *grpc.Server |
| 401 |
} |
| 402 |
|
| 403 |
var _ collogpb.LogsServiceServer = (*grpcCollector)(nil) |
| 404 |
|
| 405 |
// newGRPCCollector returns a *grpcCollector that is listening at the provided |
| 406 |
// endpoint. |
| 407 |
// |
| 408 |
// If endpoint is an empty string, the returned collector will be listening on |
| 409 |
// the localhost interface at an OS chosen port. |
| 410 |
// |
| 411 |
// If errCh is not nil, the collector will respond to Export calls with errors |
| 412 |
// sent on that channel. This means that if errCh is not nil Export calls will |
| 413 |
// block until an error is received. |
| 414 |
func newGRPCCollector(ctx context.Context, endpoint string, resultCh <-chan exportResult) (*grpcCollector, error) { |
| 415 |
if endpoint == "" { |
| 416 |
endpoint = "localhost:0" |
| 417 |
} |
| 418 |
|
| 419 |
c := &grpcCollector{ |
| 420 |
storage: newStorage(), |
| 421 |
resultCh: resultCh, |
| 422 |
} |
| 423 |
|
| 424 |
var err error |
| 425 |
c.listener, err = (&net.ListenConfig{}).Listen(ctx, "tcp", endpoint) |
| 426 |
if err != nil { |
| 427 |
return nil, err |
| 428 |
} |
| 429 |
|
| 430 |
c.srv = grpc.NewServer() |
| 431 |
collogpb.RegisterLogsServiceServer(c.srv, c) |
| 432 |
go func() { _ = c.srv.Serve(c.listener) }() |
| 433 |
|
| 434 |
return c, nil |
| 435 |
} |
| 436 |
|
| 437 |
// Export handles the export req. |
| 438 |
func (c *grpcCollector) Export( |
| 439 |
ctx context.Context, |
| 440 |
req *collogpb.ExportLogsServiceRequest, |
| 441 |
) (*collogpb.ExportLogsServiceResponse, error) { |
| 442 |
c.storage.Add(req) |
| 443 |
|
| 444 |
if h, ok := metadata.FromIncomingContext(ctx); ok { |
| 445 |
c.headersMu.Lock() |
| 446 |
c.headers = metadata.Join(c.headers, h) |
| 447 |
c.headersMu.Unlock() |
| 448 |
} |
| 449 |
|
| 450 |
if c.resultCh != nil { |
| 451 |
r := <-c.resultCh |
| 452 |
if r.Response == nil { |
| 453 |
return &collogpb.ExportLogsServiceResponse{}, r.Err |
| 454 |
} |
| 455 |
return r.Response, r.Err |
| 456 |
} |
| 457 |
return &collogpb.ExportLogsServiceResponse{}, nil |
| 458 |
} |
| 459 |
|
| 460 |
// Collect returns the Storage holding all collected requests. |
| 461 |
func (c *grpcCollector) Collect() *storage { |
| 462 |
return c.storage |
| 463 |
} |
| 464 |
|
| 465 |
func clientFactory(t *testing.T, rCh <-chan exportResult) (*client, *grpcCollector) { |
| 466 |
t.Helper() |
| 467 |
coll, err := newGRPCCollector(t.Context(), "", rCh) |
| 468 |
require.NoError(t, err) |
| 469 |
|
| 470 |
addr := coll.listener.Addr().String() |
| 471 |
opts := []Option{WithEndpoint(addr), WithInsecure()} |
| 472 |
cfg := newConfig(opts) |
| 473 |
client, err := newClient(cfg) |
| 474 |
require.NoError(t, err) |
| 475 |
return client, coll |
| 476 |
} |
| 477 |
|
| 478 |
func testCtxErrs(factory func() func(context.Context) error) func(t *testing.T) { |
| 479 |
return func(t *testing.T) { |
| 480 |
t.Helper() |
| 481 |
ctx, cancel := context.WithCancel(t.Context()) |
| 482 |
t.Cleanup(cancel) |
| 483 |
|
| 484 |
t.Run("DeadlineExceeded", func(t *testing.T) { |
| 485 |
innerCtx, innerCancel := context.WithTimeout(ctx, time.Nanosecond) |
| 486 |
t.Cleanup(innerCancel) |
| 487 |
<-innerCtx.Done() |
| 488 |
|
| 489 |
f := factory() |
| 490 |
assert.ErrorIs(t, f(innerCtx), context.DeadlineExceeded) |
| 491 |
}) |
| 492 |
|
| 493 |
t.Run("Canceled", func(t *testing.T) { |
| 494 |
innerCtx, innerCancel := context.WithCancel(ctx) |
| 495 |
innerCancel() |
| 496 |
|
| 497 |
f := factory() |
| 498 |
assert.ErrorIs(t, f(innerCtx), context.Canceled) |
| 499 |
}) |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
func TestClient(t *testing.T) { |
| 504 |
t.Run("ClientHonorsContextErrors", func(t *testing.T) { |
| 505 |
t.Run("Shutdown", testCtxErrs(func() func(context.Context) error { |
| 506 |
c, _ := clientFactory(t, nil) |
| 507 |
return c.Shutdown |
| 508 |
})) |
| 509 |
|
| 510 |
t.Run("UploadLog", testCtxErrs(func() func(context.Context) error { |
| 511 |
c, _ := clientFactory(t, nil) |
| 512 |
return func(ctx context.Context) error { |
| 513 |
return c.UploadLogs(ctx, nil) |
| 514 |
} |
| 515 |
})) |
| 516 |
}) |
| 517 |
|
| 518 |
t.Run("UploadLogs", func(t *testing.T) { |
| 519 |
ctx := t.Context() |
| 520 |
client, coll := clientFactory(t, nil) |
| 521 |
|
| 522 |
require.NoError(t, client.UploadLogs(ctx, resourceLogs)) |
| 523 |
require.NoError(t, client.Shutdown(ctx)) |
| 524 |
got := coll.Collect().Dump() |
| 525 |
require.Len(t, got, 1, "upload of one ResourceLogs") |
| 526 |
diff := cmp.Diff(got[0], resourceLogs[0], cmp.Comparer(proto.Equal)) |
| 527 |
if diff != "" { |
| 528 |
t.Fatalf("unexpected ResourceLogs:\n%s", diff) |
| 529 |
} |
| 530 |
}) |
| 531 |
|
| 532 |
t.Run("PartialSuccess", func(t *testing.T) { |
| 533 |
const n, msg = 2, "bad data" |
| 534 |
rCh := make(chan exportResult, 3) |
| 535 |
rCh <- exportResult{ |
| 536 |
Response: &collogpb.ExportLogsServiceResponse{ |
| 537 |
PartialSuccess: &collogpb.ExportLogsPartialSuccess{ |
| 538 |
RejectedLogRecords: n, |
| 539 |
ErrorMessage: msg, |
| 540 |
}, |
| 541 |
}, |
| 542 |
} |
| 543 |
rCh <- exportResult{ |
| 544 |
Response: &collogpb.ExportLogsServiceResponse{ |
| 545 |
PartialSuccess: &collogpb.ExportLogsPartialSuccess{ |
| 546 |
// Should not be logged. |
| 547 |
RejectedLogRecords: 0, |
| 548 |
ErrorMessage: "", |
| 549 |
}, |
| 550 |
}, |
| 551 |
} |
| 552 |
rCh <- exportResult{ |
| 553 |
Response: &collogpb.ExportLogsServiceResponse{}, |
| 554 |
} |
| 555 |
|
| 556 |
ctx := t.Context() |
| 557 |
client, _ := clientFactory(t, rCh) |
| 558 |
|
| 559 |
assert.ErrorIs(t, client.UploadLogs(ctx, resourceLogs), internal.PartialSuccess{}) |
| 560 |
assert.NoError(t, client.UploadLogs(ctx, resourceLogs)) |
| 561 |
assert.NoError(t, client.UploadLogs(ctx, resourceLogs)) |
| 562 |
}) |
| 563 |
} |
| 564 |
|
| 565 |
func TestConfig(t *testing.T) { |
| 566 |
factoryFunc := func(rCh <-chan exportResult, o ...Option) (log.Exporter, *grpcCollector) { |
| 567 |
coll, err := newGRPCCollector(t.Context(), "", rCh) |
| 568 |
require.NoError(t, err) |
| 569 |
|
| 570 |
ctx := t.Context() |
| 571 |
opts := append([]Option{ |
| 572 |
WithEndpoint(coll.listener.Addr().String()), |
| 573 |
WithInsecure(), |
| 574 |
}, o...) |
| 575 |
exp, err := New(ctx, opts...) |
| 576 |
require.NoError(t, err) |
| 577 |
return exp, coll |
| 578 |
} |
| 579 |
|
| 580 |
t.Run("WithHeaders", func(t *testing.T) { |
| 581 |
key := "my-custom-header" |
| 582 |
headers := map[string]string{key: "custom-value"} |
| 583 |
exp, coll := factoryFunc(nil, WithHeaders(headers)) |
| 584 |
t.Cleanup(coll.srv.Stop) |
| 585 |
|
| 586 |
ctx := t.Context() |
| 587 |
additionalKey := "additional-custom-header" |
| 588 |
ctx = metadata.AppendToOutgoingContext(ctx, additionalKey, "additional-value") |
| 589 |
require.NoError(t, exp.Export(ctx, make([]log.Record, 1))) |
| 590 |
// Ensure everything is flushed. |
| 591 |
require.NoError(t, exp.Shutdown(ctx)) |
| 592 |
|
| 593 |
got := metadata.Join(coll.headers) |
| 594 |
require.Regexp(t, "OTel Go OTLP over gRPC logs exporter/[01]\\..*", got) |
| 595 |
require.Contains(t, got, key) |
| 596 |
require.Contains(t, got, additionalKey) |
| 597 |
assert.Equal(t, []string{headers[key]}, got[key]) |
| 598 |
}) |
| 599 |
} |
| 600 |
|
| 601 |
// SetExporterID sets the exporter ID counter to v and returns the previous |
| 602 |
// value. |
| 603 |
// |
| 604 |
// This function is useful for testing purposes, allowing you to reset the |
| 605 |
// counter. It should not be used in production code. |
| 606 |
func SetExporterID(v int64) int64 { |
| 607 |
return exporterN.Swap(v) |
| 608 |
} |
| 609 |
|
| 610 |
func TestClientObservability(t *testing.T) { |
| 611 |
testCases := []struct { |
| 612 |
name string |
| 613 |
enabled bool |
| 614 |
test func(t *testing.T, scopeMetrics func() metricdata.ScopeMetrics) |
| 615 |
}{ |
| 616 |
{ |
| 617 |
name: "disable", |
| 618 |
enabled: false, |
| 619 |
test: func(t *testing.T, _ func() metricdata.ScopeMetrics) { |
| 620 |
client, _ := clientFactory(t, nil) |
| 621 |
assert.Empty(t, client.instrumentation) |
| 622 |
}, |
| 623 |
}, |
| 624 |
{ |
| 625 |
name: "upload success", |
| 626 |
enabled: true, |
| 627 |
test: func(t *testing.T, scopeMetrics func() metricdata.ScopeMetrics) { |
| 628 |
ctx := t.Context() |
| 629 |
client, coll := clientFactory(t, nil) |
| 630 |
|
| 631 |
componentName := observ.GetComponentName(0) |
| 632 |
serverAddrAttrs := observ.ServerAddrAttrs(client.conn.CanonicalTarget()) |
| 633 |
wantMetrics := metricdata.ScopeMetrics{ |
| 634 |
Scope: instrumentation.Scope{ |
| 635 |
Name: observ.ScopeName, |
| 636 |
Version: observ.Version, |
| 637 |
SchemaURL: semconv.SchemaURL, |
| 638 |
}, |
| 639 |
Metrics: []metricdata.Metrics{ |
| 640 |
{ |
| 641 |
Name: otelconv.SDKExporterLogInflight{}.Name(), |
| 642 |
Description: otelconv.SDKExporterLogInflight{}.Description(), |
| 643 |
Unit: otelconv.SDKExporterLogInflight{}.Unit(), |
| 644 |
Data: metricdata.Sum[int64]{ |
| 645 |
Temporality: metricdata.CumulativeTemporality, |
| 646 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 647 |
{ |
| 648 |
Attributes: attribute.NewSet( |
| 649 |
otelconv.SDKExporterLogInflight{}.AttrComponentName(componentName), |
| 650 |
otelconv.SDKExporterLogInflight{}.AttrComponentType( |
| 651 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 652 |
), |
| 653 |
serverAddrAttrs[0], |
| 654 |
serverAddrAttrs[1], |
| 655 |
), |
| 656 |
Value: 0, |
| 657 |
}, |
| 658 |
}, |
| 659 |
}, |
| 660 |
}, |
| 661 |
{ |
| 662 |
Name: otelconv.SDKExporterLogExported{}.Name(), |
| 663 |
Description: otelconv.SDKExporterLogExported{}.Description(), |
| 664 |
Unit: otelconv.SDKExporterLogExported{}.Unit(), |
| 665 |
Data: metricdata.Sum[int64]{ |
| 666 |
Temporality: metricdata.CumulativeTemporality, |
| 667 |
IsMonotonic: true, |
| 668 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 669 |
{ |
| 670 |
Attributes: attribute.NewSet( |
| 671 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 672 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 673 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 674 |
), |
| 675 |
serverAddrAttrs[0], |
| 676 |
serverAddrAttrs[1], |
| 677 |
), |
| 678 |
Value: int64(len(resourceLogs)), |
| 679 |
}, |
| 680 |
}, |
| 681 |
}, |
| 682 |
}, |
| 683 |
{ |
| 684 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 685 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 686 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 687 |
Data: metricdata.Histogram[float64]{ |
| 688 |
Temporality: metricdata.CumulativeTemporality, |
| 689 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 690 |
{ |
| 691 |
Attributes: attribute.NewSet( |
| 692 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 693 |
otelconv.SDKExporterOperationDuration{}.AttrComponentType( |
| 694 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 695 |
), |
| 696 |
otelconv.SDKExporterOperationDuration{}.AttrRPCResponseStatusCode( |
| 697 |
codes.OK.String(), |
| 698 |
), |
| 699 |
serverAddrAttrs[0], |
| 700 |
serverAddrAttrs[1], |
| 701 |
), |
| 702 |
Count: 1, |
| 703 |
}, |
| 704 |
}, |
| 705 |
}, |
| 706 |
}, |
| 707 |
}, |
| 708 |
} |
| 709 |
|
| 710 |
require.NoError(t, client.UploadLogs(ctx, resourceLogs)) |
| 711 |
require.NoError(t, client.Shutdown(ctx)) |
| 712 |
got := coll.Collect().Dump() |
| 713 |
require.Len(t, got, 1, "upload of one ResourceLogs") |
| 714 |
diff := cmp.Diff(got[0], resourceLogs[0], cmp.Comparer(proto.Equal)) |
| 715 |
if diff != "" { |
| 716 |
t.Fatalf("unexpected ResourceLogs:\n%s", diff) |
| 717 |
} |
| 718 |
|
| 719 |
assert.Equal(t, instrumentation.Scope{ |
| 720 |
Name: observ.ScopeName, |
| 721 |
Version: observ.Version, |
| 722 |
SchemaURL: semconv.SchemaURL, |
| 723 |
}, wantMetrics.Scope) |
| 724 |
|
| 725 |
g := scopeMetrics() |
| 726 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[0], g.Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 727 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[1], g.Metrics[1], metricdatatest.IgnoreTimestamp()) |
| 728 |
metricdatatest.AssertEqual( |
| 729 |
t, |
| 730 |
wantMetrics.Metrics[2], |
| 731 |
g.Metrics[2], |
| 732 |
metricdatatest.IgnoreTimestamp(), |
| 733 |
metricdatatest.IgnoreValue(), |
| 734 |
) |
| 735 |
}, |
| 736 |
}, |
| 737 |
{ |
| 738 |
name: "partial success", |
| 739 |
enabled: true, |
| 740 |
test: func(t *testing.T, scopeMetrics func() metricdata.ScopeMetrics) { |
| 741 |
const n, msg = 2, "bad data" |
| 742 |
rCh := make(chan exportResult, 1) |
| 743 |
rCh <- exportResult{ |
| 744 |
Response: &collogpb.ExportLogsServiceResponse{ |
| 745 |
PartialSuccess: &collogpb.ExportLogsPartialSuccess{ |
| 746 |
RejectedLogRecords: n, |
| 747 |
ErrorMessage: msg, |
| 748 |
}, |
| 749 |
}, |
| 750 |
} |
| 751 |
ctx := t.Context() |
| 752 |
client, _ := clientFactory(t, rCh) |
| 753 |
|
| 754 |
componentName := observ.GetComponentName(0) |
| 755 |
serverAddrAttrs := observ.ServerAddrAttrs(client.conn.CanonicalTarget()) |
| 756 |
var wantErr error |
| 757 |
wantErr = errors.Join(wantErr, internal.LogPartialSuccessError(n, msg)) |
| 758 |
wantMetrics := metricdata.ScopeMetrics{ |
| 759 |
Scope: instrumentation.Scope{ |
| 760 |
Name: observ.ScopeName, |
| 761 |
Version: observ.Version, |
| 762 |
SchemaURL: semconv.SchemaURL, |
| 763 |
}, |
| 764 |
Metrics: []metricdata.Metrics{ |
| 765 |
{ |
| 766 |
Name: otelconv.SDKExporterLogInflight{}.Name(), |
| 767 |
Description: otelconv.SDKExporterLogInflight{}.Description(), |
| 768 |
Unit: otelconv.SDKExporterLogInflight{}.Unit(), |
| 769 |
Data: metricdata.Sum[int64]{ |
| 770 |
Temporality: metricdata.CumulativeTemporality, |
| 771 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 772 |
{ |
| 773 |
Attributes: attribute.NewSet( |
| 774 |
otelconv.SDKExporterLogInflight{}.AttrComponentName(componentName), |
| 775 |
otelconv.SDKExporterLogInflight{}.AttrComponentType( |
| 776 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 777 |
), |
| 778 |
serverAddrAttrs[0], |
| 779 |
serverAddrAttrs[1], |
| 780 |
), |
| 781 |
|
| 782 |
Value: 0, |
| 783 |
}, |
| 784 |
}, |
| 785 |
}, |
| 786 |
}, |
| 787 |
{ |
| 788 |
Name: otelconv.SDKExporterLogExported{}.Name(), |
| 789 |
Description: otelconv.SDKExporterLogExported{}.Description(), |
| 790 |
Unit: otelconv.SDKExporterLogExported{}.Unit(), |
| 791 |
Data: metricdata.Sum[int64]{ |
| 792 |
Temporality: metricdata.CumulativeTemporality, |
| 793 |
IsMonotonic: true, |
| 794 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 795 |
{ |
| 796 |
Attributes: attribute.NewSet( |
| 797 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 798 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 799 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 800 |
), |
| 801 |
serverAddrAttrs[0], |
| 802 |
serverAddrAttrs[1], |
| 803 |
), |
| 804 |
Value: 0, |
| 805 |
}, |
| 806 |
{ |
| 807 |
Attributes: attribute.NewSet( |
| 808 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 809 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 810 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 811 |
), |
| 812 |
serverAddrAttrs[0], |
| 813 |
serverAddrAttrs[1], |
| 814 |
semconv.ErrorType(wantErr), |
| 815 |
), |
| 816 |
Value: 1, |
| 817 |
}, |
| 818 |
}, |
| 819 |
}, |
| 820 |
}, |
| 821 |
{ |
| 822 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 823 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 824 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 825 |
Data: metricdata.Histogram[float64]{ |
| 826 |
Temporality: metricdata.CumulativeTemporality, |
| 827 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 828 |
{ |
| 829 |
Attributes: attribute.NewSet( |
| 830 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 831 |
otelconv.SDKExporterOperationDuration{}.AttrComponentType( |
| 832 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 833 |
), |
| 834 |
otelconv.SDKExporterOperationDuration{}.AttrRPCResponseStatusCode( |
| 835 |
status.Code(wantErr).String(), |
| 836 |
), |
| 837 |
serverAddrAttrs[0], |
| 838 |
serverAddrAttrs[1], |
| 839 |
semconv.ErrorType(wantErr), |
| 840 |
), |
| 841 |
Count: 1, |
| 842 |
}, |
| 843 |
}, |
| 844 |
}, |
| 845 |
}, |
| 846 |
}, |
| 847 |
} |
| 848 |
|
| 849 |
err := client.UploadLogs(ctx, resourceLogs) |
| 850 |
assert.ErrorContains(t, err, wantErr.Error()) |
| 851 |
|
| 852 |
assert.Equal(t, instrumentation.Scope{ |
| 853 |
Name: observ.ScopeName, |
| 854 |
Version: observ.Version, |
| 855 |
SchemaURL: semconv.SchemaURL, |
| 856 |
}, wantMetrics.Scope) |
| 857 |
|
| 858 |
g := scopeMetrics() |
| 859 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[0], g.Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 860 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[1], g.Metrics[1], metricdatatest.IgnoreTimestamp()) |
| 861 |
metricdatatest.AssertEqual( |
| 862 |
t, |
| 863 |
wantMetrics.Metrics[2], |
| 864 |
g.Metrics[2], |
| 865 |
metricdatatest.IgnoreTimestamp(), |
| 866 |
metricdatatest.IgnoreValue(), |
| 867 |
) |
| 868 |
}, |
| 869 |
}, |
| 870 |
{ |
| 871 |
name: "upload failure", |
| 872 |
enabled: true, |
| 873 |
test: func(t *testing.T, scopeMetrics func() metricdata.ScopeMetrics) { |
| 874 |
err := status.Error(codes.InvalidArgument, "request contains invalid arguments") |
| 875 |
var wantErr error |
| 876 |
wantErr = errors.Join(wantErr, err) |
| 877 |
|
| 878 |
wantErrTypeAttr := semconv.ErrorType(wantErr) |
| 879 |
wantGRPCStatus := codes.InvalidArgument |
| 880 |
rCh := make(chan exportResult, 1) |
| 881 |
rCh <- exportResult{ |
| 882 |
Err: err, |
| 883 |
} |
| 884 |
ctx := t.Context() |
| 885 |
client, _ := clientFactory(t, rCh) |
| 886 |
uploadErr := client.UploadLogs(ctx, resourceLogs) |
| 887 |
assert.ErrorContains(t, uploadErr, "request contains invalid arguments") |
| 888 |
|
| 889 |
componentName := observ.GetComponentName(0) |
| 890 |
|
| 891 |
serverAddrAttrs := observ.ServerAddrAttrs(client.conn.CanonicalTarget()) |
| 892 |
wantMetrics := metricdata.ScopeMetrics{ |
| 893 |
Scope: instrumentation.Scope{ |
| 894 |
Name: observ.ScopeName, |
| 895 |
Version: observ.Version, |
| 896 |
SchemaURL: semconv.SchemaURL, |
| 897 |
}, |
| 898 |
Metrics: []metricdata.Metrics{ |
| 899 |
{ |
| 900 |
Name: otelconv.SDKExporterLogInflight{}.Name(), |
| 901 |
Description: otelconv.SDKExporterLogInflight{}.Description(), |
| 902 |
Unit: otelconv.SDKExporterLogInflight{}.Unit(), |
| 903 |
Data: metricdata.Sum[int64]{ |
| 904 |
Temporality: metricdata.CumulativeTemporality, |
| 905 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 906 |
{ |
| 907 |
Attributes: attribute.NewSet( |
| 908 |
otelconv.SDKExporterLogInflight{}.AttrComponentName(componentName), |
| 909 |
otelconv.SDKExporterLogInflight{}.AttrComponentType( |
| 910 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 911 |
), |
| 912 |
serverAddrAttrs[0], |
| 913 |
serverAddrAttrs[1], |
| 914 |
), |
| 915 |
Value: 0, |
| 916 |
}, |
| 917 |
}, |
| 918 |
}, |
| 919 |
}, |
| 920 |
{ |
| 921 |
Name: otelconv.SDKExporterLogExported{}.Name(), |
| 922 |
Description: otelconv.SDKExporterLogExported{}.Description(), |
| 923 |
Unit: otelconv.SDKExporterLogExported{}.Unit(), |
| 924 |
Data: metricdata.Sum[int64]{ |
| 925 |
Temporality: metricdata.CumulativeTemporality, |
| 926 |
IsMonotonic: true, |
| 927 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 928 |
{ |
| 929 |
Attributes: attribute.NewSet( |
| 930 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 931 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 932 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 933 |
), |
| 934 |
serverAddrAttrs[0], |
| 935 |
serverAddrAttrs[1], |
| 936 |
), |
| 937 |
Value: 0, |
| 938 |
}, |
| 939 |
{ |
| 940 |
Attributes: attribute.NewSet( |
| 941 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 942 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 943 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 944 |
), |
| 945 |
serverAddrAttrs[0], |
| 946 |
serverAddrAttrs[1], |
| 947 |
wantErrTypeAttr, |
| 948 |
), |
| 949 |
Value: 1, |
| 950 |
}, |
| 951 |
}, |
| 952 |
}, |
| 953 |
}, |
| 954 |
{ |
| 955 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 956 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 957 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 958 |
Data: metricdata.Histogram[float64]{ |
| 959 |
Temporality: metricdata.CumulativeTemporality, |
| 960 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 961 |
{ |
| 962 |
Attributes: attribute.NewSet( |
| 963 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 964 |
otelconv.SDKExporterOperationDuration{}.AttrComponentType( |
| 965 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 966 |
), |
| 967 |
otelconv.SDKExporterOperationDuration{}.AttrRPCResponseStatusCode( |
| 968 |
wantGRPCStatus.String(), |
| 969 |
), |
| 970 |
serverAddrAttrs[0], |
| 971 |
serverAddrAttrs[1], |
| 972 |
wantErrTypeAttr, |
| 973 |
), |
| 974 |
Count: 1, |
| 975 |
}, |
| 976 |
}, |
| 977 |
}, |
| 978 |
}, |
| 979 |
}, |
| 980 |
} |
| 981 |
g := scopeMetrics() |
| 982 |
assert.Equal(t, instrumentation.Scope{ |
| 983 |
Name: observ.ScopeName, |
| 984 |
Version: observ.Version, |
| 985 |
SchemaURL: semconv.SchemaURL, |
| 986 |
}, wantMetrics.Scope) |
| 987 |
|
| 988 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[0], g.Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 989 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[1], g.Metrics[1], metricdatatest.IgnoreTimestamp()) |
| 990 |
metricdatatest.AssertEqual( |
| 991 |
t, |
| 992 |
wantMetrics.Metrics[2], |
| 993 |
g.Metrics[2], |
| 994 |
metricdatatest.IgnoreTimestamp(), |
| 995 |
metricdatatest.IgnoreValue(), |
| 996 |
) |
| 997 |
}, |
| 998 |
}, |
| 999 |
} |
| 1000 |
for _, tc := range testCases { |
| 1001 |
t.Run(tc.name, func(t *testing.T) { |
| 1002 |
if tc.enabled { |
| 1003 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 1004 |
|
| 1005 |
// Reset component name counter for each test. |
| 1006 |
_ = SetExporterID(0) |
| 1007 |
} |
| 1008 |
prev := otel.GetMeterProvider() |
| 1009 |
t.Cleanup(func() { |
| 1010 |
otel.SetMeterProvider(prev) |
| 1011 |
}) |
| 1012 |
r := metric.NewManualReader() |
| 1013 |
mp := metric.NewMeterProvider(metric.WithReader(r)) |
| 1014 |
otel.SetMeterProvider(mp) |
| 1015 |
|
| 1016 |
scopeMetrics := func() metricdata.ScopeMetrics { |
| 1017 |
var got metricdata.ResourceMetrics |
| 1018 |
err := r.Collect(t.Context(), &got) |
| 1019 |
require.NoError(t, err) |
| 1020 |
require.Len(t, got.ScopeMetrics, 1) |
| 1021 |
return got.ScopeMetrics[0] |
| 1022 |
} |
| 1023 |
tc.test(t, scopeMetrics) |
| 1024 |
}) |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
func TestClientObservabilityWithRetry(t *testing.T) { |
| 1029 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 1030 |
|
| 1031 |
_ = SetExporterID(0) |
| 1032 |
prev := otel.GetMeterProvider() |
| 1033 |
t.Cleanup(func() { |
| 1034 |
otel.SetMeterProvider(prev) |
| 1035 |
}) |
| 1036 |
|
| 1037 |
r := metric.NewManualReader() |
| 1038 |
mp := metric.NewMeterProvider(metric.WithReader(r)) |
| 1039 |
otel.SetMeterProvider(mp) |
| 1040 |
|
| 1041 |
scopeMetrics := func() metricdata.ScopeMetrics { |
| 1042 |
var got metricdata.ResourceMetrics |
| 1043 |
err := r.Collect(t.Context(), &got) |
| 1044 |
require.NoError(t, err) |
| 1045 |
require.Len(t, got.ScopeMetrics, 1) |
| 1046 |
return got.ScopeMetrics[0] |
| 1047 |
} |
| 1048 |
|
| 1049 |
rCh := make(chan exportResult, 2) |
| 1050 |
rCh <- exportResult{ |
| 1051 |
Err: status.Error(codes.Unavailable, "service temporarily unavailable"), |
| 1052 |
} |
| 1053 |
const n, msg = 1, "some logs rejected" |
| 1054 |
rCh <- exportResult{ |
| 1055 |
Response: &collogpb.ExportLogsServiceResponse{ |
| 1056 |
PartialSuccess: &collogpb.ExportLogsPartialSuccess{ |
| 1057 |
RejectedLogRecords: n, |
| 1058 |
ErrorMessage: msg, |
| 1059 |
}, |
| 1060 |
}, |
| 1061 |
} |
| 1062 |
|
| 1063 |
ctx := t.Context() |
| 1064 |
client, _ := clientFactory(t, rCh) |
| 1065 |
|
| 1066 |
componentName := observ.GetComponentName(0) |
| 1067 |
|
| 1068 |
serverAddrAttrs := observ.ServerAddrAttrs(client.conn.CanonicalTarget()) |
| 1069 |
var wantErr error |
| 1070 |
wantErr = errors.Join(wantErr, internal.LogPartialSuccessError(n, msg)) |
| 1071 |
|
| 1072 |
wantMetrics := metricdata.ScopeMetrics{ |
| 1073 |
Scope: instrumentation.Scope{ |
| 1074 |
Name: observ.ScopeName, |
| 1075 |
Version: observ.Version, |
| 1076 |
SchemaURL: semconv.SchemaURL, |
| 1077 |
}, |
| 1078 |
Metrics: []metricdata.Metrics{ |
| 1079 |
{ |
| 1080 |
Name: otelconv.SDKExporterLogInflight{}.Name(), |
| 1081 |
Description: otelconv.SDKExporterLogInflight{}.Description(), |
| 1082 |
Unit: otelconv.SDKExporterLogInflight{}.Unit(), |
| 1083 |
Data: metricdata.Sum[int64]{ |
| 1084 |
Temporality: metricdata.CumulativeTemporality, |
| 1085 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 1086 |
{ |
| 1087 |
Attributes: attribute.NewSet( |
| 1088 |
otelconv.SDKExporterLogInflight{}.AttrComponentName(componentName), |
| 1089 |
otelconv.SDKExporterLogInflight{}.AttrComponentType( |
| 1090 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 1091 |
), |
| 1092 |
serverAddrAttrs[0], |
| 1093 |
serverAddrAttrs[1], |
| 1094 |
), |
| 1095 |
Value: 0, |
| 1096 |
}, |
| 1097 |
}, |
| 1098 |
}, |
| 1099 |
}, |
| 1100 |
{ |
| 1101 |
Name: otelconv.SDKExporterLogExported{}.Name(), |
| 1102 |
Description: otelconv.SDKExporterLogExported{}.Description(), |
| 1103 |
Unit: otelconv.SDKExporterLogExported{}.Unit(), |
| 1104 |
Data: metricdata.Sum[int64]{ |
| 1105 |
Temporality: metricdata.CumulativeTemporality, |
| 1106 |
IsMonotonic: true, |
| 1107 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 1108 |
{ |
| 1109 |
Attributes: attribute.NewSet( |
| 1110 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 1111 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 1112 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 1113 |
), |
| 1114 |
serverAddrAttrs[0], |
| 1115 |
serverAddrAttrs[1], |
| 1116 |
), |
| 1117 |
Value: int64(len(resourceLogs)) - n, |
| 1118 |
}, |
| 1119 |
{ |
| 1120 |
Attributes: attribute.NewSet( |
| 1121 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 1122 |
otelconv.SDKExporterLogExported{}.AttrComponentType( |
| 1123 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 1124 |
), |
| 1125 |
serverAddrAttrs[0], |
| 1126 |
serverAddrAttrs[1], |
| 1127 |
semconv.ErrorType(wantErr), |
| 1128 |
), |
| 1129 |
Value: n, |
| 1130 |
}, |
| 1131 |
}, |
| 1132 |
}, |
| 1133 |
}, |
| 1134 |
{ |
| 1135 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 1136 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 1137 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 1138 |
Data: metricdata.Histogram[float64]{ |
| 1139 |
Temporality: metricdata.CumulativeTemporality, |
| 1140 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 1141 |
{ |
| 1142 |
Attributes: attribute.NewSet( |
| 1143 |
otelconv.SDKExporterLogExported{}.AttrComponentName(componentName), |
| 1144 |
otelconv.SDKExporterOperationDuration{}.AttrComponentType( |
| 1145 |
otelconv.ComponentTypeOtlpGRPCLogExporter, |
| 1146 |
), |
| 1147 |
otelconv.SDKExporterOperationDuration{}.AttrRPCResponseStatusCode( |
| 1148 |
status.Code(wantErr).String(), |
| 1149 |
), |
| 1150 |
serverAddrAttrs[0], |
| 1151 |
serverAddrAttrs[1], |
| 1152 |
semconv.ErrorType(wantErr), |
| 1153 |
), |
| 1154 |
Count: 1, |
| 1155 |
}, |
| 1156 |
}, |
| 1157 |
}, |
| 1158 |
}, |
| 1159 |
}, |
| 1160 |
} |
| 1161 |
|
| 1162 |
err := client.UploadLogs(ctx, resourceLogs) |
| 1163 |
assert.ErrorContains(t, err, wantErr.Error()) |
| 1164 |
|
| 1165 |
assert.Equal(t, instrumentation.Scope{ |
| 1166 |
Name: observ.ScopeName, |
| 1167 |
Version: observ.Version, |
| 1168 |
SchemaURL: semconv.SchemaURL, |
| 1169 |
}, wantMetrics.Scope) |
| 1170 |
|
| 1171 |
g := scopeMetrics() |
| 1172 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[0], g.Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 1173 |
metricdatatest.AssertEqual(t, wantMetrics.Metrics[1], g.Metrics[1], metricdatatest.IgnoreTimestamp()) |
| 1174 |
metricdatatest.AssertEqual( |
| 1175 |
t, |
| 1176 |
wantMetrics.Metrics[2], |
| 1177 |
g.Metrics[2], |
| 1178 |
metricdatatest.IgnoreTimestamp(), |
| 1179 |
metricdatatest.IgnoreValue(), |
| 1180 |
) |
| 1181 |
} |
| 1182 |
|
| 1183 |
func BenchmarkExporterExportLogs(b *testing.B) { |
| 1184 |
const logRecordsCount = 100 |
| 1185 |
|
| 1186 |
run := func(b *testing.B) { |
| 1187 |
coll, err := newGRPCCollector(b.Context(), "", nil) |
| 1188 |
require.NoError(b, err) |
| 1189 |
b.Cleanup(func() { |
| 1190 |
coll.srv.Stop() |
| 1191 |
}) |
| 1192 |
|
| 1193 |
ctx := b.Context() |
| 1194 |
opts := []Option{ |
| 1195 |
WithEndpoint(coll.listener.Addr().String()), |
| 1196 |
WithInsecure(), |
| 1197 |
WithTimeout(5 * time.Second), |
| 1198 |
} |
| 1199 |
exp, err := New(ctx, opts...) |
| 1200 |
require.NoError(b, err) |
| 1201 |
b.Cleanup(func() { |
| 1202 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 1203 |
assert.NoError(b, exp.Shutdown(context.Background())) |
| 1204 |
}) |
| 1205 |
|
| 1206 |
logs := make([]log.Record, logRecordsCount) |
| 1207 |
now := time.Now() |
| 1208 |
for i := range logs { |
| 1209 |
logs[i].SetTimestamp(now) |
| 1210 |
logs[i].SetObservedTimestamp(now) |
| 1211 |
} |
| 1212 |
|
| 1213 |
b.ReportAllocs() |
| 1214 |
b.ResetTimer() |
| 1215 |
|
| 1216 |
for b.Loop() { |
| 1217 |
err := exp.Export(b.Context(), logs) |
| 1218 |
require.NoError(b, err) |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
b.Run("Observability", func(b *testing.B) { |
| 1223 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 1224 |
run(b) |
| 1225 |
}) |
| 1226 |
|
| 1227 |
b.Run("NoObservability", func(b *testing.B) { |
| 1228 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "false") |
| 1229 |
run(b) |
| 1230 |
}) |
| 1231 |
} |
| 1232 |
|
| 1233 |
func TestNextExporterID(t *testing.T) { |
| 1234 |
SetExporterID(0) |
| 1235 |
|
| 1236 |
var expected int64 |
| 1237 |
for range 10 { |
| 1238 |
id := nextExporterID() |
| 1239 |
if id != expected { |
| 1240 |
t.Errorf("nextExporterID() = %d; want %d", id, expected) |
| 1241 |
} |
| 1242 |
expected++ |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
func TestSetExporterID(t *testing.T) { |
| 1247 |
SetExporterID(0) |
| 1248 |
|
| 1249 |
prev := SetExporterID(42) |
| 1250 |
if prev != 0 { |
| 1251 |
t.Errorf("SetExporterID(42) returned %d; want 0", prev) |
| 1252 |
} |
| 1253 |
|
| 1254 |
id := nextExporterID() |
| 1255 |
if id != 42 { |
| 1256 |
t.Errorf("nextExporterID() = %d; want 42", id) |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
func TestNextExporterIDConcurrentSafe(t *testing.T) { |
| 1261 |
SetExporterID(0) |
| 1262 |
|
| 1263 |
const goroutines = 100 |
| 1264 |
const increments = 10 |
| 1265 |
|
| 1266 |
var wg sync.WaitGroup |
| 1267 |
wg.Add(goroutines) |
| 1268 |
|
| 1269 |
for range goroutines { |
| 1270 |
go func() { |
| 1271 |
defer wg.Done() |
| 1272 |
for range increments { |
| 1273 |
nextExporterID() |
| 1274 |
} |
| 1275 |
}() |
| 1276 |
} |
| 1277 |
|
| 1278 |
wg.Wait() |
| 1279 |
|
| 1280 |
expected := int64(goroutines * increments) |
| 1281 |
if id := nextExporterID(); id != expected { |
| 1282 |
t.Errorf("nextExporterID() = %d; want %d", id, expected) |
| 1283 |
} |
| 1284 |
} |