Skip to content

Commit

Permalink
Rework Jaeger and Zipkin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej Gera committed Jul 21, 2020
1 parent 349d534 commit 623f125
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 147 deletions.
2 changes: 1 addition & 1 deletion example/zipkin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initTracer(url string) {
// probability.
err := zipkin.InstallNewPipeline(
url,
zipkin.WithServiceName("zipkin-test"),
"zipkin-test",
zipkin.WithLogger(logger),
zipkin.WithSDK(&sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
)
Expand Down
Binary file modified example/zipkin/zipkin
Binary file not shown.
319 changes: 218 additions & 101 deletions exporters/trace/jaeger/jaeger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/api/support/bundler"
"google.golang.org/grpc/codes"

"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/trace"
apitrace "go.opentelemetry.io/otel/api/trace"
gen "go.opentelemetry.io/otel/exporters/trace/jaeger/internal/gen-go/jaeger"
ottest "go.opentelemetry.io/otel/internal/testing"
Expand All @@ -38,72 +40,124 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

const (
collectorEndpoint = "http://localhost:14268/api/traces"
agentEndpoint = "localhost:6831"
)

func TestInstallNewPipeline(t *testing.T) {
fn, err := InstallNewPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
)
defer fn()
assert.NoError(t, err)
assert.IsType(t, &sdktrace.Provider{}, global.TraceProvider())
}
testCases := []struct {
name string
endpoint EndpointOption
options []Option
expectedProvider trace.Provider
}{
{
name: "simple pipeline",
endpoint: WithCollectorEndpoint(collectorEndpoint),
expectedProvider: &sdktrace.Provider{},
},
{
name: "with agent endpoint",
endpoint: WithAgentEndpoint(agentEndpoint),
expectedProvider: &sdktrace.Provider{},
},
{
name: "with disabled",
endpoint: WithCollectorEndpoint(collectorEndpoint),
options: []Option{
WithDisabled(true),
},
expectedProvider: &apitrace.NoopProvider{},
},
}

func TestInstallNewPipeline_WithDisabled(t *testing.T) {
fn, err := InstallNewPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
WithDisabled(true),
)
defer fn()
assert.NoError(t, err)
assert.IsType(t, &apitrace.NoopProvider{}, global.TraceProvider())
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fn, err := InstallNewPipeline(
tc.endpoint,
tc.options...,
)
defer fn()

func TestNewExporterPipeline(t *testing.T) {
tp, fn, err := NewExportPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
)
defer fn()
assert.NoError(t, err)
assert.NotEqual(t, tp, global.TraceProvider())
}
func TestNewExporterPipeline_WithSDK(t *testing.T) {
tp, fn, err := NewExportPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
WithSDK(&sdktrace.Config{
DefaultSampler: sdktrace.AlwaysSample(),
}),
)
defer fn()
assert.NoError(t, err)
_, span := tp.Tracer("jaeger test").Start(context.Background(), "always-on")
spanCtx := span.SpanContext()
assert.True(t, spanCtx.IsSampled())
span.End()
assert.NoError(t, err)
assert.IsType(t, tc.expectedProvider, global.TraceProvider())

tp2, fn, err := NewExportPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
WithSDK(&sdktrace.Config{
DefaultSampler: sdktrace.NeverSample(),
}),
)
defer fn()
assert.NoError(t, err)
_, span2 := tp2.Tracer("jaeger test").Start(context.Background(), "never")
span2Ctx := span2.SpanContext()
assert.False(t, span2Ctx.IsSampled())
span2.End()
global.SetTraceProvider(nil)
})
}
}

func TestNewExporterPipeline_WithDisabled(t *testing.T) {
tp, fn, err := NewExportPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
WithDisabled(true),
)
defer fn()
assert.NoError(t, err)
assert.IsType(t, &apitrace.NoopProvider{}, tp)
func TestNewExportPipeline(t *testing.T) {
testCases := []struct {
name string
endpoint EndpointOption
options []Option
expectedProviderType trace.Provider
testSpanSampling, spanShouldBeSampled bool
}{
{
name: "simple pipeline",
endpoint: WithCollectorEndpoint(collectorEndpoint),
expectedProviderType: &sdktrace.Provider{},
},
{
name: "with disabled",
endpoint: WithCollectorEndpoint(collectorEndpoint),
options: []Option{
WithDisabled(true),
},
expectedProviderType: &apitrace.NoopProvider{},
},
{
name: "always on",
endpoint: WithCollectorEndpoint(collectorEndpoint),
options: []Option{
WithSDK(&sdktrace.Config{
DefaultSampler: sdktrace.AlwaysSample(),
}),
},
expectedProviderType: &sdktrace.Provider{},
testSpanSampling: true,
spanShouldBeSampled: true,
},
{
name: "never",
endpoint: WithCollectorEndpoint(collectorEndpoint),
options: []Option{
WithSDK(&sdktrace.Config{
DefaultSampler: sdktrace.NeverSample(),
}),
},
expectedProviderType: &sdktrace.Provider{},
testSpanSampling: true,
spanShouldBeSampled: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tp, fn, err := NewExportPipeline(
tc.endpoint,
tc.options...,
)
defer fn()

assert.NoError(t, err)
assert.NotEqual(t, tp, global.TraceProvider())
assert.IsType(t, tc.expectedProviderType, tp)

if tc.testSpanSampling {
_, span := tp.Tracer("jaeger test").Start(context.Background(), tc.name)
spanCtx := span.SpanContext()
assert.Equal(t, tc.spanShouldBeSampled, spanCtx.IsSampled())
span.End()
}
})
}
}

func TestNewExporterPipeline_WithDisabledFromEnv(t *testing.T) {
func TestNewExportPipelineWithDisabledFromEnv(t *testing.T) {
envStore, err := ottest.SetEnvVariables(map[string]string{
envDisabled: "true",
})
Expand All @@ -114,37 +168,123 @@ func TestNewExporterPipeline_WithDisabledFromEnv(t *testing.T) {
}()

tp, fn, err := NewExportPipeline(
WithCollectorEndpoint("http://localhost:14268/api/traces"),
WithCollectorEndpoint(collectorEndpoint),
)
defer fn()
assert.NoError(t, err)
assert.IsType(t, &apitrace.NoopProvider{}, tp)
}

func TestNewRawExporter(t *testing.T) {
const (
collectorEndpoint = "http://localhost"
serviceName = "test-service"
tagKey = "key"
tagVal = "val"
)
// Create Jaeger Exporter
exp, err := NewRawExporter(
WithCollectorEndpoint(collectorEndpoint),
WithProcess(Process{
ServiceName: serviceName,
Tags: []kv.KeyValue{
kv.String(tagKey, tagVal),
testCases := []struct {
name string
endpoint EndpointOption
options []Option
expectedServiceName string
expectedTagsLen, expectedBufferMaxCount, expectedBatchMaxCount int
}{
{
name: "default exporter",
endpoint: WithCollectorEndpoint(collectorEndpoint),
expectedServiceName: defaultServiceName,
expectedBufferMaxCount: bundler.DefaultBufferedByteLimit,
expectedBatchMaxCount: bundler.DefaultBundleCountThreshold,
},
{
name: "default exporter with agent endpoint",
endpoint: WithAgentEndpoint(agentEndpoint),
expectedServiceName: defaultServiceName,
expectedBufferMaxCount: bundler.DefaultBufferedByteLimit,
expectedBatchMaxCount: bundler.DefaultBundleCountThreshold,
},
{
name: "with process",
endpoint: WithCollectorEndpoint(collectorEndpoint),
options: []Option{
WithProcess(
Process{
ServiceName: "jaeger-test",
Tags: []kv.KeyValue{
kv.String("key", "val"),
},
},
),
},
}),
)
expectedServiceName: "jaeger-test",
expectedTagsLen: 1,
expectedBufferMaxCount: bundler.DefaultBufferedByteLimit,
expectedBatchMaxCount: bundler.DefaultBundleCountThreshold,
},
{
name: "with buffer and batch max count",
endpoint: WithCollectorEndpoint(collectorEndpoint),
options: []Option{
WithProcess(
Process{
ServiceName: "jaeger-test",
},
),
WithBufferMaxCount(99),
WithBatchMaxCount(99),
},
expectedServiceName: "jaeger-test",
expectedBufferMaxCount: 99,
expectedBatchMaxCount: 99,
},
}

assert.NoError(t, err)
assert.EqualValues(t, serviceName, exp.process.ServiceName)
assert.Len(t, exp.process.Tags, 1)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
exp, err := NewRawExporter(
tc.endpoint,
tc.options...,
)

assert.NoError(t, err)
assert.Equal(t, tc.expectedServiceName, exp.process.ServiceName)
assert.Len(t, exp.process.Tags, tc.expectedTagsLen)
assert.Equal(t, tc.expectedBufferMaxCount, exp.bundler.BufferedByteLimit)
assert.Equal(t, tc.expectedBatchMaxCount, exp.bundler.BundleCountThreshold)
})
}
}

func TestNewRawExporter_ShouldFailIfCollectorEndpointEmpty(t *testing.T) {
func TestNewRawExporterShouldFail(t *testing.T) {
testCases := []struct {
name string
endpoint EndpointOption
expectedErrMsg string
}{
{
name: "with empty collector endpoint",
endpoint: WithCollectorEndpoint(""),
expectedErrMsg: "collectorEndpoint must not be empty",
},
{
name: "with empty agent endpoint",
endpoint: WithAgentEndpoint(""),
expectedErrMsg: "agentEndpoint must not be empty",
},
{
name: "with invalid agent endpoint",
endpoint: WithAgentEndpoint("localhost"),
expectedErrMsg: "address localhost: missing port in address",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewRawExporter(
tc.endpoint,
)

assert.Error(t, err)
assert.EqualError(t, err, tc.expectedErrMsg)
})
}
}

func TestNewRawExporterShouldFailIfCollectorUnset(t *testing.T) {
// Record and restore env
envStore := ottest.NewEnvStore()
envStore.Record(envEndpoint)
Expand All @@ -162,29 +302,6 @@ func TestNewRawExporter_ShouldFailIfCollectorEndpointEmpty(t *testing.T) {
assert.Error(t, err)
}

func TestNewRawExporter_WithAgentEndpoint(t *testing.T) {
const agentEndpoint = "localhost:6831"
// Create Jaeger Exporter
_, err := NewRawExporter(
WithAgentEndpoint(agentEndpoint),
)
assert.NoError(t, err)
}

func TestNewRawExporter_WithAgentShouldFailIfEndpointInvalid(t *testing.T) {
//empty
_, err := NewRawExporter(
WithAgentEndpoint(""),
)
assert.Error(t, err)

//invalid endpoint addr
_, err = NewRawExporter(
WithAgentEndpoint("http://localhost"),
)
assert.Error(t, err)
}

type testCollectorEnpoint struct {
spansUploaded []*gen.Span
}
Expand Down
2 changes: 0 additions & 2 deletions exporters/trace/zipkin/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

const defaultServiceName = "OpenTelemetry"

// Exporter exports SpanData to the zipkin collector. It implements
// the SpanBatcher interface, so it needs to be used together with the
// WithBatcher option when setting up the exporter pipeline.
Expand Down
Loading

0 comments on commit 623f125

Please sign in to comment.