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
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5ce9b7e
unit test
chahatsagarmain Jan 13, 2025
1e1208e
changes
chahatsagarmain Jan 14, 2025
bd2ea16
Merge branch 'main' into e2e-otlp
chahatsagarmain Jan 15, 2025
b1116f5
logging tenant
chahatsagarmain Jan 15, 2025
24b9b7e
http test
chahatsagarmain Jan 16, 2025
bcc716a
resolved changes
chahatsagarmain Jan 18, 2025
e1d95b2
atomic operation on shared var
chahatsagarmain Jan 18, 2025
5ff0d50
Update cmd/collector/app/span_processor_test.go
yurishkuro Jan 18, 2025
105a171
use assert.eventually
chahatsagarmain Jan 18, 2025
25b68ec
Merge branch 'e2e-otlp' of https://github.com/chahatsagarmain/jaeger …
chahatsagarmain Jan 18, 2025
d750fbc
remove unecessasry code
chahatsagarmain Jan 18, 2025
3868088
Update cmd/collector/app/span_processor_test.go
yurishkuro Jan 19, 2025
90f8935
Merge branch 'e2e-otlp' of https://github.com/chahatsagarmain/jaeger …
chahatsagarmain Jan 21, 2025
28c1231
added grpc test
chahatsagarmain Jan 21, 2025
04c9aab
refactoring
chahatsagarmain Jan 21, 2025
a89135f
Merge branch 'main' into e2e-otlp
chahatsagarmain Jan 21, 2025
95712f2
changes
chahatsagarmain Jan 22, 2025
37b1105
Merge branch 'main' into e2e-otlp
chahatsagarmain Jan 23, 2025
a0b9aeb
changes
chahatsagarmain Jan 25, 2025
4330264
changes
chahatsagarmain Jan 25, 2025
1a9f494
Merge branch 'e2e-otlp' of https://github.com/chahatsagarmain/jaeger …
chahatsagarmain Jan 25, 2025
11d65ce
Merge branch 'main' into e2e-otlp
chahatsagarmain Jan 25, 2025
9544bad
lint
chahatsagarmain Jan 25, 2025
2befd5d
isoloate mock writer
chahatsagarmain Jan 26, 2025
b589977
valid tenant and refactored test
chahatsagarmain Jan 26, 2025
1d7f157
refactor
chahatsagarmain Jan 26, 2025
7b40951
Merge branch 'main' into e2e-otlp
chahatsagarmain Jan 28, 2025
aaedf3c
lint
chahatsagarmain Jan 28, 2025
1e21118
refactor and fix test
chahatsagarmain Jan 29, 2025
1342050
Merge branch 'main' into e2e-otlp
chahatsagarmain Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions cmd/collector/app/handler/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go.uber.org/zap"
"google.golang.org/grpc/codes"
_ "google.golang.org/grpc/encoding/gzip" // register zip encoding
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/jaegertracing/jaeger-idl/model/v1"
Expand Down Expand Up @@ -97,22 +96,5 @@ func (c *batchConsumer) validateTenant(ctx context.Context) (string, error) {
if !c.tenancyMgr.Enabled {
return "", nil
}

md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", status.Errorf(codes.PermissionDenied, "missing tenant header")
}

tenants := md.Get(c.tenancyMgr.Header)
if len(tenants) < 1 {
return "", status.Errorf(codes.PermissionDenied, "missing tenant header")
} else if len(tenants) > 1 {
return "", status.Errorf(codes.PermissionDenied, "extra tenant header")
}

if !c.tenancyMgr.Valid(tenants[0]) {
return "", status.Errorf(codes.PermissionDenied, "unknown tenant")
}

return tenants[0], nil
return tenancy.GetValidTenant(ctx, c.tenancyMgr)
}
225 changes: 225 additions & 0 deletions cmd/collector/app/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ import (
"errors"
"fmt"
"io"
"net/http"
"reflect"
"slices"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/gogo/protobuf/jsonpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/pdata/ptrace"
otlptrace "go.opentelemetry.io/proto/otlp/collector/trace/v1"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"

"github.com/jaegertracing/jaeger-idl/model/v1"
"github.com/jaegertracing/jaeger-idl/thrift-gen/jaeger"
zc "github.com/jaegertracing/jaeger-idl/thrift-gen/zipkincore"
cFlags "github.com/jaegertracing/jaeger/cmd/collector/app/flags"
"github.com/jaegertracing/jaeger/cmd/collector/app/handler"
"github.com/jaegertracing/jaeger/cmd/collector/app/processor"
zipkinsanitizer "github.com/jaegertracing/jaeger/cmd/collector/app/sanitizer/zipkin"
"github.com/jaegertracing/jaeger/internal/metricstest"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/pkg/tenancy"
"github.com/jaegertracing/jaeger/pkg/testutils"
"github.com/jaegertracing/jaeger/storage_v2/tracestore/mocks"
"github.com/jaegertracing/jaeger/storage_v2/v1adapter"
)

Expand Down Expand Up @@ -807,3 +822,213 @@ func TestSpanProcessorWithOnDroppedSpanOption(t *testing.T) {
require.EqualError(t, err, processor.ErrBusy.Error())
assert.Equal(t, []string{"op3"}, droppedOperations)
}

func optionsWithPorts(portHttp string, portGrpc string) *cFlags.CollectorOptions {
opts := &cFlags.CollectorOptions{
OTLP: struct {
Enabled bool
GRPC configgrpc.ServerConfig
HTTP confighttp.ServerConfig
}{
Enabled: true,
HTTP: confighttp.ServerConfig{
Endpoint: portHttp,
IncludeMetadata: true,
},
GRPC: configgrpc.ServerConfig{
NetAddr: confignet.AddrConfig{
Endpoint: portGrpc,
Transport: confignet.TransportTypeTCP,
},
},
},
}
return opts
}

func TestOTLPReceiverWithV2Storage(t *testing.T) {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
name string
requestType string
tenant string
expectedTenant string
expectedError bool
executeRequest func(ctx context.Context, url string, tenant string) error
}{
{
name: "Valid tenant via HTTP",
requestType: "http",
tenant: "test-tenant",
expectedTenant: "test-tenant",
expectedError: false,
executeRequest: sendHTTPRequest,
},
{
name: "Invalid tenant via HTTP",
requestType: "http",
tenant: "invalid-tenant",
expectedTenant: "",
expectedError: true,
executeRequest: sendHTTPRequest,
},
{
name: "Valid tenant via gRPC",
requestType: "grpc",
tenant: "test-tenant",
expectedTenant: "test-tenant",
expectedError: false,
executeRequest: sendGRPCRequest,
},
{
name: "Invalid tenant via gRPC",
requestType: "grpc",
tenant: "invalid-tenant",
expectedTenant: "",
expectedError: true,
executeRequest: sendGRPCRequest,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockWriter := mocks.NewWriter(t)

spanProcessor, err := NewSpanProcessor(
mockWriter,
nil,
Options.NumWorkers(1),
Options.QueueSize(1),
Options.ReportBusy(true),
)
require.NoError(t, err)
defer spanProcessor.Close()
logger := zaptest.NewLogger(t)

portHttp := "4317"
portGrpc := "4318"

var receivedTraces atomic.Pointer[ptrace.Traces]
var receivedCtx atomic.Pointer[context.Context]
if !tt.expectedError {
mockWriter.On("WriteTraces", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
storeContext := args.Get(0).(context.Context)
storeTrace := args.Get(1).(ptrace.Traces)
receivedTraces.Store(&storeTrace)
receivedCtx.Store(&storeContext)
}).Return(nil)
}

tenancyMgr := tenancy.NewManager(&tenancy.Options{
Enabled: true,
Header: "x-tenant",
Tenants: []string{"test-tenant"},
})

rec, err := handler.StartOTLPReceiver(
optionsWithPorts(fmt.Sprintf("localhost:%v", portHttp), fmt.Sprintf("localhost:%v", portGrpc)),
logger,
spanProcessor,
tenancyMgr,
)
require.NoError(t, err)
ctx := context.Background()
defer rec.Shutdown(ctx)

var url string
if tt.requestType == "http" {
url = fmt.Sprintf("http://localhost:%v/v1/traces", portHttp)
} else {
url = fmt.Sprintf("localhost:%v", portGrpc)
}
err = tt.executeRequest(ctx, url, tt.tenant)
if tt.expectedError {
assert.Error(t, err)
return
}
require.NoError(t, err)

assert.Eventually(t, func() bool {
storedTraces := receivedTraces.Load()
storedCtx := receivedCtx.Load()
if storedTraces == nil || storedCtx == nil {
return false
}
receivedSpan := storedTraces.ResourceSpans().At(0).
ScopeSpans().At(0).
Spans().At(0)
receivedTenant := tenancy.GetTenant(*storedCtx)
return receivedSpan.Name() == "test-trace" && receivedTenant == tt.expectedTenant
}, 1*time.Second, 100*time.Millisecond)

mockWriter.AssertExpectations(t)
})
}
}

// Helper function to send HTTP request
func sendHTTPRequest(ctx context.Context, url string, tenant string) error {
traceJSON := `{
"resourceSpans": [{
"scopeSpans": [{
"spans": [{
"name": "test-trace"
}]
}]
}]
}`

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(traceJSON))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-tenant", tenant)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}

// Helper function to send gRPC request
func sendGRPCRequest(ctx context.Context, url string, tenant string) error {
conn, err := grpc.NewClient(
url,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return err
}
defer conn.Close()

md := metadata.New(map[string]string{
"x-tenant": tenant,
})
ctxWithMD := metadata.NewOutgoingContext(ctx, md)

client := otlptrace.NewTraceServiceClient(conn)
req := &otlptrace.ExportTraceServiceRequest{
ResourceSpans: []*tracepb.ResourceSpans{
{
ScopeSpans: []*tracepb.ScopeSpans{
{
Spans: []*tracepb.Span{
{
Name: "test-trace",
},
},
},
},
},
},
}

_, err = client.Export(ctxWithMD, req)
return err
}
Loading
Loading