test: add unit tests for telemetry#3033
Conversation
π WalkthroughWalkthroughThis PR adds unit test files for the telemetry package, covering exporter factories for log, meter, and tracer exporters (OTLP, zipkin, jaeger, signoz), as well as core telemetry helpers like resource naming, handler factory, log/tracer providers, and metric instruments. ChangesTelemetry exporter and core test coverage
Estimated code review effort: 2 (Simple) | ~12 minutes π₯ Pre-merge checks | β 5β Passed checks (5 passed)
β¨ Finishing Touchesπ Generate docstrings
π§ͺ Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
π§Ή Nitpick comments (2)
pkg/telemetry/meterexporters/factory_test.go (2)
57-57: π Maintainability & Code Quality | π΅ Trivial | β‘ Quick winVerify the exact error message for the unsupported protocol case.
TestExporterFactoryasserts the precise error string for error cases, butTestNewOTLPonly checks that an error occurred for the"unsupported protocol"case. Asserting the message ("unsupported protocol: udp") would make the test consistent and guard against accidental error-format changes.β»οΈ Proposed fix
{name: "unsupported protocol", protocol: "udp", wantErr: true},func TestNewOTLP(t *testing.T) { tests := []struct { name string insecure bool urlpath string headers map[string]string protocol string wantErr bool + errMessage string }{ {name: "http insecure with options", insecure: true, urlpath: "/v1/metrics", headers: map[string]string{"key": "value"}, protocol: "http"}, {name: "http secure", insecure: false, protocol: "http"}, {name: "grpc insecure", insecure: true, headers: map[string]string{"key": "value"}, protocol: "grpc"}, {name: "grpc secure", insecure: false, headers: map[string]string{"key": "value"}, protocol: "grpc"}, - {name: "unsupported protocol", protocol: "udp", wantErr: true}, + {name: "unsupported protocol", protocol: "udp", wantErr: true, errMessage: "unsupported protocol: udp"}, }exp, err := NewOTLP("localhost:4317", tt.insecure, tt.urlpath, tt.headers, tt.protocol) if tt.wantErr { if err == nil { t.Fatalf("expected error, got nil") } + if tt.errMessage != "" && err.Error() != tt.errMessage { + t.Fatalf("expected error %q, got %q", tt.errMessage, err.Error()) + } return }π€ Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/telemetry/meterexporters/factory_test.go` at line 57, The unsupported protocol case in TestNewOTLP only checks for an error, but it should assert the exact message to stay consistent with TestExporterFactory. Update the "unsupported protocol" table-driven case in factory_test.go to verify the error string from NewOTLP matches the precise unsupported protocol message for "udp", using the existing TestNewOTLP and unsupported protocol symbols to locate the assertion.
60-75: π©Ί Stability & Availability | π΅ Trivial | β‘ Quick winShut down exporters in success cases to avoid resource leaks.
otlpmetrichttp.Newandotlpmetricgrpc.Newcan start background goroutines/connections. The success cases create exporters but never callShutdown, which may leak resources or cause flakiness when the suite runs in CI.β»οΈ Proposed fix
if exp == nil { t.Fatalf("expected exporter, got nil") } + if err := exp.Shutdown(context.Background()); err != nil { + t.Fatalf("failed to shutdown exporter: %v", err) + } })You'll also need to add
"context"to the import block.π€ Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/telemetry/meterexporters/factory_test.go` around lines 60 - 75, The successful NewOTLP test cases create exporters but never clean them up, which can leave background goroutines or connections running. Update the table-driven test in factory_test.go so that when NewOTLP returns a non-nil exporter in the non-error path, it calls Shutdown with a context before the subtest exits. Add the context import needed for that cleanup, and keep the existing error assertions intact in the t.Run cases around NewOTLP.
π€ Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@pkg/telemetry/meterexporters/factory_test.go`:
- Line 57: The unsupported protocol case in TestNewOTLP only checks for an
error, but it should assert the exact message to stay consistent with
TestExporterFactory. Update the "unsupported protocol" table-driven case in
factory_test.go to verify the error string from NewOTLP matches the precise
unsupported protocol message for "udp", using the existing TestNewOTLP and
unsupported protocol symbols to locate the assertion.
- Around line 60-75: The successful NewOTLP test cases create exporters but
never clean them up, which can leave background goroutines or connections
running. Update the table-driven test in factory_test.go so that when NewOTLP
returns a non-nil exporter in the non-error path, it calls Shutdown with a
context before the subtest exits. Add the context import needed for that
cleanup, and keep the existing error assertions intact in the t.Run cases around
NewOTLP.
βΉοΈ Review info
βοΈ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e8706542-67e3-4b6e-b182-e72c06e96857
π Files selected for processing (4)
pkg/telemetry/logexporters/factory_test.gopkg/telemetry/meterexporters/factory_test.gopkg/telemetry/telemetry_test.gopkg/telemetry/tracerexporters/factory_test.go
Codecov Reportβ
All modified and coverable lines are covered by tests. β Your project check has failed because the head coverage (74.24%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## master #3033 +/- ##
==========================================
- Coverage 74.67% 74.24% -0.42%
==========================================
Files 83 99 +16
Lines 9212 9541 +329
==========================================
+ Hits 6878 7083 +205
- Misses 1798 1910 +112
- Partials 536 548 +12 β View full report in Codecov by Harness. π New features to boost your workflow:
|
Summary by CodeRabbit