1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-04 09:43:23 +02:00

new commit

This commit is contained in:
Huy Vo 2021-04-02 11:51:36 -07:00
parent c46c5f58f8
commit 06e24cc38d
2 changed files with 90 additions and 97 deletions

View File

@ -43,7 +43,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
This changes it to make `SamplingParameters` conform with the OpenTelemetry specification. (#1749)
- Modify `BatchSpanProcessor.ForceFlush` to abort after timeout/cancellation. (#1757)
- Improve OTLP/gRPC exporter connection errors. (#1737)
- Make `ExportSpans` in Jaeger Exporter honor context deadline. (#TBD)
- Make `ExportSpans` in Jaeger Exporter honor context deadline. (#1773)
### Removed

View File

@ -277,47 +277,38 @@ func TestNewRawExporterShouldFailIfCollectorUnset(t *testing.T) {
assert.Error(t, err)
}
type testCollectorEndpoint struct {
type testCollectorEnpoint struct {
batchesUploaded []*gen.Batch
}
func (c *testCollectorEndpoint) upload(batch *gen.Batch) error {
func (c *testCollectorEnpoint) upload(batch *gen.Batch) error {
c.batchesUploaded = append(c.batchesUploaded, batch)
return nil
}
var _ batchUploader = (*testCollectorEndpoint)(nil)
var _ batchUploader = (*testCollectorEnpoint)(nil)
func withTestCollectorEndpoint() func() (batchUploader, error) {
return func() (batchUploader, error) {
return &testCollectorEndpoint{}, nil
return &testCollectorEnpoint{}, nil
}
}
func withTestCollectorEndpointInjected(ce *testCollectorEndpoint) func() (batchUploader, error) {
func withTestCollectorEndpointInjected(ce *testCollectorEnpoint) func() (batchUploader, error) {
return func() (batchUploader, error) {
return ce, nil
}
}
func TestExporter_ExportSpan(t *testing.T) {
envStore, err := ottest.SetEnvVariables(map[string]string{
envDisabled: "false",
})
require.NoError(t, err)
defer func() {
require.NoError(t, envStore.Restore())
}()
const (
serviceName = "test-service"
tagKey = "key"
tagVal = "val"
)
testCollector := &testCollectorEndpoint{}
tp, spanFlush, err := NewExportPipeline(
withTestCollectorEndpointInjected(testCollector),
// Create Jaeger Exporter
exp, err := NewRawExporter(
withTestCollectorEndpoint(),
WithSDKOptions(
sdktrace.WithResource(resource.NewWithAttributes(
semconv.ServiceNameKey.String(serviceName),
@ -325,25 +316,26 @@ func TestExporter_ExportSpan(t *testing.T) {
)),
),
)
fmt.Printf("%+v\n", exp.bundler)
assert.Equal(t, 1, 2)
assert.NoError(t, err)
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exp),
)
otel.SetTracerProvider(tp)
tracer := otel.Tracer("test-tracer")
for i := 0; i < 3; i++ {
_, span := tracer.Start(context.Background(), fmt.Sprintf("test-span-%d", i))
span.End()
assert.True(t, span.SpanContext().IsValid())
}
_, span := otel.Tracer("test-tracer").Start(context.Background(), "test-span")
span.End()
spanFlush()
batchesUploaded := testCollector.batchesUploaded
require.Len(t, batchesUploaded, 1)
uploadedBatch := batchesUploaded[0]
assert.Equal(t, serviceName, uploadedBatch.GetProcess().GetServiceName())
assert.Len(t, uploadedBatch.GetSpans(), 3)
assert.True(t, span.SpanContext().IsValid())
require.Len(t, uploadedBatch.GetProcess().GetTags(), 1)
assert.Equal(t, tagKey, uploadedBatch.GetProcess().GetTags()[0].GetKey())
assert.Equal(t, tagVal, uploadedBatch.GetProcess().GetTags()[0].GetVStr())
exp.Flush()
tc := exp.uploader.(*testCollectorEnpoint)
assert.True(t, len(tc.batchesUploaded) == 1)
assert.True(t, len(tc.batchesUploaded[0].GetSpans()) == 1)
}
func Test_spanSnapshotToThrift(t *testing.T) {
@ -374,40 +366,6 @@ func Test_spanSnapshotToThrift(t *testing.T) {
data *export.SpanSnapshot
want *gen.Span
}{
{
name: "no status description",
data: &export.SpanSnapshot{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
}),
Name: "/foo",
StartTime: now,
EndTime: now,
StatusCode: codes.Error,
SpanKind: trace.SpanKindClient,
InstrumentationLibrary: instrumentation.Library{
Name: instrLibName,
Version: instrLibVersion,
},
},
want: &gen.Span{
TraceIdLow: 651345242494996240,
TraceIdHigh: 72623859790382856,
SpanId: 72623859790382856,
OperationName: "/foo",
StartTime: now.UnixNano() / 1000,
Duration: 0,
Tags: []*gen.Tag{
{Key: keyError, VType: gen.TagType_BOOL, VBool: &boolTrue},
{Key: keyInstrumentationLibraryName, VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: keyInstrumentationLibraryVersion, VType: gen.TagType_STRING, VStr: &instrLibVersion},
{Key: keyStatusCode, VType: gen.TagType_LONG, VLong: &statusCodeValue},
// Should not have a status message because it was unset
{Key: keySpanKind, VType: gen.TagType_STRING, VStr: &spanKind},
},
},
},
{
name: "no parent",
data: &export.SpanSnapshot{
@ -453,12 +411,12 @@ func Test_spanSnapshotToThrift(t *testing.T) {
{Key: "double", VType: gen.TagType_DOUBLE, VDouble: &doubleValue},
{Key: "key", VType: gen.TagType_STRING, VStr: &keyValue},
{Key: "int", VType: gen.TagType_LONG, VLong: &intValue},
{Key: keyError, VType: gen.TagType_BOOL, VBool: &boolTrue},
{Key: keyInstrumentationLibraryName, VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: keyInstrumentationLibraryVersion, VType: gen.TagType_STRING, VStr: &instrLibVersion},
{Key: keyStatusCode, VType: gen.TagType_LONG, VLong: &statusCodeValue},
{Key: keyStatusMessage, VType: gen.TagType_STRING, VStr: &statusMessage},
{Key: keySpanKind, VType: gen.TagType_STRING, VStr: &spanKind},
{Key: "error", VType: gen.TagType_BOOL, VBool: &boolTrue},
{Key: "otel.library.name", VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: "otel.library.version", VType: gen.TagType_STRING, VStr: &instrLibVersion},
{Key: "status.code", VType: gen.TagType_LONG, VLong: &statusCodeValue},
{Key: "status.message", VType: gen.TagType_STRING, VStr: &statusMessage},
{Key: "span.kind", VType: gen.TagType_STRING, VStr: &spanKind},
},
References: []*gen.SpanRef{
{
@ -490,14 +448,11 @@ func Test_spanSnapshotToThrift(t *testing.T) {
{
name: "with parent",
data: &export.SpanSnapshot{
ParentSpanID: parentSpanID,
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
}),
Parent: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: parentSpanID,
}),
Links: []trace.Link{
{
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
@ -531,8 +486,8 @@ func Test_spanSnapshotToThrift(t *testing.T) {
Tags: []*gen.Tag{
// status code, message and span kind should NOT be populated
{Key: "arr", VType: gen.TagType_STRING, VStr: &arrValue},
{Key: keyInstrumentationLibraryName, VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: keyInstrumentationLibraryVersion, VType: gen.TagType_STRING, VStr: &instrLibVersion},
{Key: "otel.library.name", VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: "otel.library.version", VType: gen.TagType_STRING, VStr: &instrLibVersion},
},
References: []*gen.SpanRef{
{
@ -547,14 +502,11 @@ func Test_spanSnapshotToThrift(t *testing.T) {
{
name: "resources do not affect the tags",
data: &export.SpanSnapshot{
ParentSpanID: parentSpanID,
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
}),
Parent: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: parentSpanID,
}),
Name: "/foo",
StartTime: now,
EndTime: now,
@ -580,8 +532,8 @@ func Test_spanSnapshotToThrift(t *testing.T) {
StartTime: now.UnixNano() / 1000,
Duration: 0,
Tags: []*gen.Tag{
{Key: keyInstrumentationLibraryName, VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: keyInstrumentationLibraryVersion, VType: gen.TagType_STRING, VStr: &instrLibVersion},
{Key: "otel.library.name", VType: gen.TagType_STRING, VStr: &instrLibName},
{Key: "otel.library.version", VType: gen.TagType_STRING, VStr: &instrLibVersion},
},
},
},
@ -649,6 +601,54 @@ func TestErrorOnExportShutdownExporter(t *testing.T) {
assert.NoError(t, e.ExportSpans(context.Background(), nil))
}
func TestExporterExportSpansSuccessfully(t *testing.T) {
e, err := NewRawExporter(withTestCollectorEndpoint())
require.NoError(t, err)
now := time.Now()
ss := []*export.SpanSnapshot{
{
Name: "s1",
Resource: resource.NewWithAttributes(
semconv.ServiceNameKey.String("name"),
attribute.Key("r1").String("v1"),
),
StartTime: now,
EndTime: now,
},
{
Name: "s2",
Resource: resource.NewWithAttributes(
semconv.ServiceNameKey.String("name"),
attribute.Key("r2").String("v2"),
),
StartTime: now,
EndTime: now,
},
}
e.ExportSpans(context.Background(), ss)
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(e),
)
otel.SetTracerProvider(tp)
_, span := otel.Tracer("test-tracer").Start(context.Background(), "test-span")
span.End()
assert.True(t, span.SpanContext().IsValid())
e.Flush()
tc := e.uploader.(*testCollectorEnpoint)
assert.True(t, len(tc.batchesUploaded) == 1)
assert.True(t, len(tc.batchesUploaded[0].GetSpans()) == 2)
/*fmt.Printf("%+v\n", e.bundler.curBundle)
e.ExportSpans(context.Background(), ss)
fmt.Printf("%+v\n", e.bundler.curBundle)
assert.Equal(t, 1, 2)*/
}
func TestExporterExportSpansHonorsCancel(t *testing.T) {
e, err := NewRawExporter(withTestCollectorEndpoint())
require.NoError(t, err)
@ -755,7 +755,7 @@ func TestJaegerBatchList(t *testing.T) {
{
OperationName: "s1",
Tags: []*gen.Tag{
{Key: keySpanKind, VType: gen.TagType_STRING, VStr: &spanKind},
{Key: "span.kind", VType: gen.TagType_STRING, VStr: &spanKind},
},
StartTime: now.UnixNano() / 1000,
},
@ -1015,17 +1015,14 @@ func TestNewExporterPipelineWithOptions(t *testing.T) {
const (
serviceName = "test-service"
eventCountLimit = 10
tagKey = "key"
tagVal = "val"
)
testCollector := &testCollectorEndpoint{}
testCollector := &testCollectorEnpoint{}
tp, spanFlush, err := NewExportPipeline(
withTestCollectorEndpointInjected(testCollector),
WithSDKOptions(
sdktrace.WithResource(resource.NewWithAttributes(
semconv.ServiceNameKey.String(serviceName),
attribute.String(tagKey, tagVal),
)),
sdktrace.WithSpanLimits(sdktrace.SpanLimits{
EventCountLimit: eventCountLimit,
@ -1045,14 +1042,10 @@ func TestNewExporterPipelineWithOptions(t *testing.T) {
assert.True(t, span.SpanContext().IsValid())
batchesUploaded := testCollector.batchesUploaded
require.Len(t, batchesUploaded, 1)
assert.True(t, len(batchesUploaded) == 1)
uploadedBatch := batchesUploaded[0]
assert.Equal(t, serviceName, uploadedBatch.GetProcess().GetServiceName())
require.Len(t, uploadedBatch.GetSpans(), 1)
assert.True(t, len(uploadedBatch.GetSpans()) == 1)
uploadedSpan := uploadedBatch.GetSpans()[0]
assert.Len(t, uploadedSpan.GetLogs(), eventCountLimit)
require.Len(t, uploadedBatch.GetProcess().GetTags(), 1)
assert.Equal(t, tagKey, uploadedBatch.GetProcess().GetTags()[0].GetKey())
assert.Equal(t, tagVal, uploadedBatch.GetProcess().GetTags()[0].GetVStr())
assert.Equal(t, eventCountLimit, len(uploadedSpan.GetLogs()))
}