(t *testing.T)
| 445 | } |
| 446 | |
| 447 | func TestTracingHook_ProcessPipelineHook(t *testing.T) { |
| 448 | imsb := tracetest.NewInMemoryExporter() |
| 449 | provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb)) |
| 450 | hook := newTracingHook( |
| 451 | "redis://localhost:6379", |
| 452 | WithTracerProvider(provider), |
| 453 | ) |
| 454 | |
| 455 | tests := []struct { |
| 456 | name string |
| 457 | errTest error |
| 458 | }{ |
| 459 | {"nil error", nil}, |
| 460 | {"test error", fmt.Errorf("test error")}, |
| 461 | } |
| 462 | |
| 463 | for _, tt := range tests { |
| 464 | t.Run(tt.name, func(t *testing.T) { |
| 465 | defer imsb.Reset() |
| 466 | |
| 467 | cmds := []redis.Cmder{ |
| 468 | redis.NewCmd(context.Background(), "ping"), |
| 469 | redis.NewCmd(context.Background(), "ping"), |
| 470 | } |
| 471 | processHook := hook.ProcessPipelineHook(func(ctx context.Context, cmds []redis.Cmder) error { |
| 472 | return tt.errTest |
| 473 | }) |
| 474 | assertEqual(t, tt.errTest, processHook(context.Background(), cmds)) |
| 475 | assertEqual(t, 1, len(imsb.GetSpans())) |
| 476 | |
| 477 | spanData := imsb.GetSpans()[0] |
| 478 | assertEqual(t, instrumName, spanData.InstrumentationLibrary.Name) |
| 479 | assertEqual(t, "redis.pipeline ping", spanData.Name) |
| 480 | assertEqual(t, trace.SpanKindClient, spanData.SpanKind) |
| 481 | assertAttributeContains(t, spanData.Attributes, semconv.DBSystemRedis) |
| 482 | assertAttributeContains(t, spanData.Attributes, semconv.DBConnectionStringKey.String("redis://localhost:6379")) |
| 483 | assertAttributeContains(t, spanData.Attributes, semconv.DBStatementKey.String("ping\nping")) |
| 484 | |
| 485 | if tt.errTest == nil { |
| 486 | assertEqual(t, 0, len(spanData.Events)) |
| 487 | assertEqual(t, codes.Unset, spanData.Status.Code) |
| 488 | assertEqual(t, "", spanData.Status.Description) |
| 489 | return |
| 490 | } |
| 491 | |
| 492 | assertEqual(t, 1, len(spanData.Events)) |
| 493 | assertAttributeContains(t, spanData.Events[0].Attributes, semconv.ExceptionTypeKey.String("*errors.errorString")) |
| 494 | assertAttributeContains(t, spanData.Events[0].Attributes, semconv.ExceptionMessageKey.String(tt.errTest.Error())) |
| 495 | assertEqual(t, codes.Error, spanData.Status.Code) |
| 496 | assertEqual(t, tt.errTest.Error(), spanData.Status.Description) |
| 497 | }) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | func TestTracingHook_ProcessHook_LongCommand(t *testing.T) { |
| 502 | imsb := tracetest.NewInMemoryExporter() |
nothing calls this directly
no test coverage detected