Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for OTLP receiver in jaeger-v1 #6541

Merged
merged 30 commits into from
Jan 30, 2025

Conversation

chahatsagarmain
Copy link
Contributor

Which problem is this PR solving?

Description of the changes

  • Added unit test

How was this change tested?

Checklist

Signed-off-by: chahatsagarmain <[email protected]>
@chahatsagarmain chahatsagarmain requested a review from a team as a code owner January 13, 2025 19:51
@chahatsagarmain chahatsagarmain marked this pull request as draft January 13, 2025 19:51
Copy link

codecov bot commented Jan 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.94%. Comparing base (1074c5e) to head (1342050).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6541      +/-   ##
==========================================
+ Coverage   95.93%   95.94%   +0.01%     
==========================================
  Files         365      365              
  Lines       20616    20607       -9     
==========================================
- Hits        19778    19772       -6     
+ Misses        638      636       -2     
+ Partials      200      199       -1     
Flag Coverage Δ
badger_v1 9.91% <0.00%> (-0.01%) ⬇️
badger_v2 1.84% <0.00%> (-0.01%) ⬇️
cassandra-4.x-v1-manual 14.92% <0.00%> (-0.01%) ⬇️
cassandra-4.x-v2-auto 1.83% <0.00%> (-0.01%) ⬇️
cassandra-4.x-v2-manual 1.83% <0.00%> (-0.01%) ⬇️
cassandra-5.x-v1-manual 14.92% <0.00%> (-0.01%) ⬇️
cassandra-5.x-v2-auto 1.83% <0.00%> (-0.01%) ⬇️
cassandra-5.x-v2-manual 1.83% <0.00%> (-0.01%) ⬇️
elasticsearch-6.x-v1 19.30% <0.00%> (-0.01%) ⬇️
elasticsearch-7.x-v1 19.38% <0.00%> (-0.01%) ⬇️
elasticsearch-8.x-v1 19.55% <0.00%> (-0.01%) ⬇️
elasticsearch-8.x-v2 1.84% <0.00%> (-0.01%) ⬇️
grpc_v1 10.89% <0.00%> (-0.01%) ⬇️
grpc_v2 7.88% <0.00%> (-0.01%) ⬇️
kafka-3.x-v1 10.21% <0.00%> (-0.01%) ⬇️
kafka-3.x-v2 1.84% <0.00%> (-0.01%) ⬇️
memory_v2 1.84% <0.00%> (-0.01%) ⬇️
opensearch-1.x-v1 19.43% <0.00%> (-0.01%) ⬇️
opensearch-2.x-v1 19.43% <0.00%> (-0.01%) ⬇️
opensearch-2.x-v2 1.84% <0.00%> (-0.01%) ⬇️
tailsampling-processor 0.48% <0.00%> (-0.01%) ⬇️
unittests 94.83% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: chahatsagarmain <[email protected]>
@yurishkuro
Copy link
Member

dependent PR was merged

chahatsagarmain and others added 3 commits January 15, 2025 22:29
Signed-off-by: chahatsagarmain <[email protected]>
Signed-off-by: chahatsagarmain <[email protected]>
cmd/collector/app/span_processor_test.go Outdated Show resolved Hide resolved
cmd/collector/app/span_processor_test.go Outdated Show resolved Hide resolved
cmd/collector/app/span_processor_test.go Outdated Show resolved Hide resolved
Signed-off-by: chahatsagarmain <[email protected]>
@yurishkuro yurishkuro added the changelog:test Change that's adding missing tests or correcting existing tests label Jan 18, 2025
Signed-off-by: chahatsagarmain <[email protected]>
@chahatsagarmain chahatsagarmain marked this pull request as ready for review January 18, 2025 21:44
Signed-off-by: chahatsagarmain <[email protected]>
Signed-off-by: chahatsagarmain <[email protected]>
@yurishkuro yurishkuro changed the title [test][WIP] Unit test for OTLP receiver in jaeger-v1 Add unit test for OTLP receiver in jaeger-v1 Jan 21, 2025
@yurishkuro yurishkuro added changelog:test Change that's adding missing tests or correcting existing tests and removed changelog:test Change that's adding missing tests or correcting existing tests labels Jan 21, 2025
Signed-off-by: chahatsagarmain <[email protected]>
chahatsagarmain and others added 6 commits January 24, 2025 03:24
Signed-off-by: chahatsagarmain <[email protected]>
Signed-off-by: chahatsagarmain <[email protected]>
Signed-off-by: chahatsagarmain <[email protected]>
Comment on lines 853 to 854
var receivedTraces atomic.Pointer[ptrace.Traces]
var receivedCtx atomic.Pointer[context.Context]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cannot share state between tests like this, it creates side effects. HTTP and gRPC should be tested in complete isolation.

Signed-off-by: chahatsagarmain <[email protected]>
)
require.NoError(t, err)
ctx := context.Background()
defer rec.Shutdown(ctx)
Copy link
Member

@yurishkuro yurishkuro Jan 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from L852 you are repeating a lot of code for each test. You can create a helper function that will initialize the receiver (it can be a lambda function).

receivedTraces.Store(&storeTrace)
receivedCtx.Store(&storeContext)
}).Return(nil)
// Can't send tenancy headers with http request to OTLP receiver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old comment

cmd/collector/app/handler/grpc_handler.go Outdated Show resolved Hide resolved

func TestOTLPReceiverWithV2Storage(t *testing.T) {
// Send trace via HTTP
t.Run("Send trace data using HTTP connection", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are multiple ways to organize the tests, yours makes the least sense to me.

Option 1: two top-level functions, with shared helper functions
Option 2: a single test function with table-driven tests, no setup helpers needed but each test has a lambda function parameter executeRequest implemented with two other functions, for http and grpc separately

Your test is only checking success path, does not check scenario when incoming tenant is invalid.

Signed-off-by: chahatsagarmain <[email protected]>
tenants := httpMd.Metadata.Get(c.tenancyMgr.Header)

if len(tenants) > 0 {
if !c.tenancyMgr.Valid(tenants[0]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to duplicate validation. You are missing other checks like the one that causes return "", status.Errorf(codes.PermissionDenied, "missing tenant header")

This function needs to be constructed of two parts, one is extracting the tenant info, another is validation.

And I would prefer to consolidate all this in pkg/tenancy/getValidTenant (which can be made public). It's a kind of a mess there too. We have four possible sources of tenant:

  • it may already be in the Context
  • it may be in the HTTP request headers
  • it may be in gRPC request headers, which are accessible via Context via metadata
  • finally, it may be in the Context as OTEL's client.Metadata

Signed-off-by: chahatsagarmain <[email protected]>
chahatsagarmain and others added 2 commits January 29, 2025 02:47
Signed-off-by: chahatsagarmain <[email protected]>
var tenant string
var err error
if tenant = GetTenant(ctx); tenant != "" {
if !tm.Valid(tenant) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please refactor such that you don't repeat logic. You can have a separate helper to extract the tenants (with early returns), and here validate it once.

if tenant != "" {
if !tc.Valid(tenant) {
return tenant, status.Errorf(codes.PermissionDenied, "unknown tenant")
func GetValidTenant(ctx context.Context, tm *Manager) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a unit test for full coverage. Currently

$ go test -cover ./pkg/tenancy/
ok  	github.com/jaegertracing/jaeger/pkg/tenancy	0.483s	coverage: 100.0% of statements

Signed-off-by: chahatsagarmain <[email protected]>
Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@yurishkuro yurishkuro merged commit 17cdd33 into jaegertracing:main Jan 30, 2025
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:test Change that's adding missing tests or correcting existing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add e2e unit test for OTLP receiver in jaeger-v1
2 participants