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

Move span transforms of the OTLP exporter to internal (#593)

* Move span transforms of the OTLP exporter to internal

Breakup and move functionality of the `transform_spans.go` file into
appropriate files in the `internal/transform` sub-package. This is in
preparation of using some of the overlapping functionality to implement
Resource support in the metric side of the exporter.

Adds more specific unit tests for some of the functionality transferred.
The tests removed used the exporter as a processing engine and the
replacement tests do not do this. The test found in the `oltp_test.go`
seem to comprehensively cover this type of test.

Include Link `Name` in the exporter span link and adds a test to check
for this.

Resolves #527

* Apply suggestions from code review

Co-Authored-By: Rahul Patel <rghetia@yahoo.com>

* Fix SpanData doc

* Consolidate span comparison

Co-authored-by: Rahul Patel <rghetia@yahoo.com>
Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
This commit is contained in:
Tyler Yahn 2020-03-25 09:04:31 -07:00 committed by GitHub
parent cfe74cf91f
commit c9dcc1be28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 645 additions and 536 deletions

View File

@ -3,7 +3,8 @@ module go.opentelemetry.io/otel/exporters/otlp
replace go.opentelemetry.io/otel => ../..
require (
github.com/golang/protobuf v1.3.4
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.3.4 // indirect
github.com/google/go-cmp v0.4.0
github.com/grpc-ecosystem/grpc-gateway v1.14.3 // indirect
github.com/open-telemetry/opentelemetry-proto v0.3.0

View File

@ -14,6 +14,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
@ -31,6 +33,8 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/grpc-ecosystem/grpc-gateway v1.14.3 h1:OCJlWkOUoTnl0neNGlf4fUm3TmbEtguw7vR+nGtnDjY=
github.com/grpc-ecosystem/grpc-gateway v1.14.3/go.mod h1:6CwZWGDSPRJidgKAtJVvND6soZe6fT7iteq8wDPdhb0=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -72,6 +76,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

View File

@ -0,0 +1,66 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transform
import (
commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
"go.opentelemetry.io/otel/api/core"
)
// Attributes transforms a slice of KeyValues into a slice of OTLP attribute key-values.
func Attributes(attrs []core.KeyValue) []*commonpb.AttributeKeyValue {
if len(attrs) == 0 {
return nil
}
out := make([]*commonpb.AttributeKeyValue, 0, len(attrs))
for _, v := range attrs {
switch v.Value.Type() {
case core.BOOL:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_BOOL,
BoolValue: v.Value.AsBool(),
})
case core.INT64, core.INT32, core.UINT32, core.UINT64:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_INT,
IntValue: v.Value.AsInt64(),
})
case core.FLOAT32:
f32 := v.Value.AsFloat32()
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: float64(f32),
})
case core.FLOAT64:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: v.Value.AsFloat64(),
})
case core.STRING:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_STRING,
StringValue: v.Value.AsString(),
})
}
}
return out
}

View File

@ -0,0 +1,113 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transform
import (
"testing"
commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/api/core"
)
func TestAttributes(t *testing.T) {
for _, test := range []struct {
attrs []core.KeyValue
expected []*commonpb.AttributeKeyValue
}{
{nil, nil},
{
[]core.KeyValue{
core.Key("int to int").Int(123),
core.Key("uint to int").Uint(1234),
core.Key("int32 to int").Int32(12345),
core.Key("uint32 to int").Uint32(123456),
core.Key("int64 to int64").Int64(1234567),
core.Key("uint64 to int64").Uint64(12345678),
core.Key("float32 to double").Float32(3.14),
core.Key("float64 to double").Float32(1.61),
core.Key("string to string").String("string"),
core.Key("bool to bool").Bool(true),
},
[]*commonpb.AttributeKeyValue{
{
Key: "int to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 123,
},
{
Key: "uint to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 1234,
},
{
Key: "int32 to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 12345,
},
{
Key: "uint32 to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 123456,
},
{
Key: "int64 to int64",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 1234567,
},
{
Key: "uint64 to int64",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 12345678,
},
{
Key: "float32 to double",
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: 3.14,
},
{
Key: "float64 to double",
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: 1.61,
},
{
Key: "string to string",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "string",
},
{
Key: "bool to bool",
Type: commonpb.AttributeKeyValue_BOOL,
BoolValue: true,
},
},
},
} {
got := Attributes(test.attrs)
if !assert.Len(t, got, len(test.expected)) {
continue
}
for i, actual := range got {
if actual.Type == commonpb.AttributeKeyValue_DOUBLE {
if !assert.InDelta(t, test.expected[i].DoubleValue, actual.DoubleValue, 0.01) {
continue
}
test.expected[i].DoubleValue = actual.DoubleValue
}
assert.Equal(t, test.expected[i], actual)
}
}
}

View File

@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transform
import (
resourcepb "github.com/open-telemetry/opentelemetry-proto/gen/go/resource/v1"
"go.opentelemetry.io/otel/sdk/resource"
)
// Resource transforms a Resource into an OTLP Resource.
func Resource(r *resource.Resource) *resourcepb.Resource {
if r == nil {
return nil
}
return &resourcepb.Resource{Attributes: Attributes(r.Attributes())}
}

View File

@ -0,0 +1,34 @@
package transform
import (
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/sdk/resource"
)
func TestNilResource(t *testing.T) {
assert.Empty(t, Resource(nil))
}
func TestEmptyResource(t *testing.T) {
assert.Empty(t, Resource(&resource.Resource{}))
}
/*
* This does not include any testing on the ordering of Resource Attributes.
* They are stored as a map internally to the Resource and their order is not
* guaranteed.
*/
func TestResourceAttributes(t *testing.T) {
attrs := []core.KeyValue{core.Key("one").Int(1), core.Key("two").Int(2)}
got := Resource(resource.New(attrs...)).GetAttributes()
if !assert.Len(t, attrs, 2) {
return
}
assert.ElementsMatch(t, Attributes(attrs), got)
}

View File

@ -1,4 +1,4 @@
// Copyright 2020, OpenTelemetry Authors
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -12,37 +12,56 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package otlp
package transform
import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/otel/sdk/resource"
commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
resourcepb "github.com/open-telemetry/opentelemetry-proto/gen/go/resource/v1"
tracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"
"go.opentelemetry.io/otel/api/core"
apitrace "go.opentelemetry.io/otel/api/trace"
export "go.opentelemetry.io/otel/sdk/export/trace"
"go.opentelemetry.io/otel/sdk/resource"
)
const (
maxMessageEventsPerSpan = 128
)
func otResourceToProtoResource(res *resource.Resource) *resourcepb.Resource {
if res == nil {
// SpanData transforms a slice of SpanData into a slice of OTLP ResourceSpans.
func SpanData(sdl []*export.SpanData) []*tracepb.ResourceSpans {
if len(sdl) == 0 {
return nil
}
resProto := &resourcepb.Resource{
Attributes: otAttributesToProtoAttributes(res.Attributes()),
rsm := make(map[*resource.Resource]*tracepb.ResourceSpans)
for _, sd := range sdl {
if sd != nil {
rs, ok := rsm[sd.Resource]
if !ok {
rs = &tracepb.ResourceSpans{
Resource: Resource(sd.Resource),
InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{
{
Spans: []*tracepb.Span{},
},
},
}
rsm[sd.Resource] = rs
}
rs.InstrumentationLibrarySpans[0].Spans =
append(rs.InstrumentationLibrarySpans[0].Spans, span(sd))
}
}
return resProto
rss := make([]*tracepb.ResourceSpans, 0, len(rsm))
for _, rs := range rsm {
rss = append(rss, rs)
}
return rss
}
func otSpanToProtoSpan(sd *export.SpanData) *tracepb.Span {
// span transforms a Span into an OTLP span.
func span(sd *export.SpanData) *tracepb.Span {
if sd == nil {
return nil
}
@ -50,14 +69,14 @@ func otSpanToProtoSpan(sd *export.SpanData) *tracepb.Span {
TraceId: sd.SpanContext.TraceID[:],
SpanId: sd.SpanContext.SpanID[:],
ParentSpanId: sd.ParentSpanID[:],
Status: otStatusToProtoStatus(sd.StatusCode, sd.StatusMessage),
Status: status(sd.StatusCode, sd.StatusMessage),
StartTimeUnixNano: uint64(sd.StartTime.Nanosecond()),
EndTimeUnixNano: uint64(sd.EndTime.Nanosecond()),
Links: otLinksToProtoLinks(sd.Links),
Kind: otSpanKindToProtoSpanKind(sd.SpanKind),
Links: links(sd.Links),
Kind: spanKind(sd.SpanKind),
Name: sd.Name,
Attributes: otAttributesToProtoAttributes(sd.Attributes),
Events: otTimeEventsToProtoTimeEvents(sd.MessageEvents),
Attributes: Attributes(sd.Attributes),
Events: spanEvents(sd.MessageEvents),
// TODO (rghetia): Add Tracestate: when supported.
DroppedAttributesCount: uint32(sd.DroppedAttributeCount),
DroppedEventsCount: uint32(sd.DroppedMessageEventCount),
@ -65,14 +84,16 @@ func otSpanToProtoSpan(sd *export.SpanData) *tracepb.Span {
}
}
func otStatusToProtoStatus(status codes.Code, message string) *tracepb.Status {
// status transform a span code and message into an OTLP span status.
func status(status codes.Code, message string) *tracepb.Status {
return &tracepb.Status{
Code: tracepb.Status_StatusCode(status),
Message: message,
}
}
func otLinksToProtoLinks(links []apitrace.Link) []*tracepb.Span_Link {
// links transforms span Links to OTLP span links.
func links(links []apitrace.Link) []*tracepb.Span_Link {
if len(links) == 0 {
return nil
}
@ -86,56 +107,14 @@ func otLinksToProtoLinks(links []apitrace.Link) []*tracepb.Span_Link {
sl = append(sl, &tracepb.Span_Link{
TraceId: otLink.TraceID[:],
SpanId: otLink.SpanID[:],
Attributes: otAttributesToProtoAttributes(otLink.Attributes),
Attributes: Attributes(otLink.Attributes),
})
}
return sl
}
func otAttributesToProtoAttributes(attrs []core.KeyValue) []*commonpb.AttributeKeyValue {
if len(attrs) == 0 {
return nil
}
out := make([]*commonpb.AttributeKeyValue, 0, len(attrs))
for _, v := range attrs {
switch v.Value.Type() {
case core.BOOL:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_BOOL,
BoolValue: v.Value.AsBool(),
})
case core.INT64, core.INT32, core.UINT32, core.UINT64:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_INT,
IntValue: v.Value.AsInt64(),
})
case core.FLOAT32:
f32 := v.Value.AsFloat32()
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: float64(f32),
})
case core.FLOAT64:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: v.Value.AsFloat64(),
})
case core.STRING:
out = append(out, &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_STRING,
StringValue: v.Value.AsString(),
})
}
}
return out
}
func otTimeEventsToProtoTimeEvents(es []export.Event) []*tracepb.Span_Event {
// spanEvents transforms span Events to an OTLP span events.
func spanEvents(es []export.Event) []*tracepb.Span_Event {
if len(es) == 0 {
return nil
}
@ -155,8 +134,9 @@ func otTimeEventsToProtoTimeEvents(es []export.Event) []*tracepb.Span_Event {
messageEvents++
events = append(events,
&tracepb.Span_Event{
Name: e.Name,
TimeUnixNano: uint64(e.Time.Nanosecond()),
Attributes: otAttributesToProtoAttributes(e.Attributes),
Attributes: Attributes(e.Attributes),
// TODO (rghetia) : Add Drop Counts when supported.
},
)
@ -165,7 +145,8 @@ func otTimeEventsToProtoTimeEvents(es []export.Event) []*tracepb.Span_Event {
return events
}
func otSpanKindToProtoSpanKind(kind apitrace.SpanKind) tracepb.Span_SpanKind {
// spanKind transforms a SpanKind to an OTLP span kind.
func spanKind(kind apitrace.SpanKind) tracepb.Span_SpanKind {
switch kind {
case apitrace.SpanKindInternal:
return tracepb.Span_INTERNAL

View File

@ -0,0 +1,347 @@
package transform
import (
"strconv"
"testing"
"time"
"github.com/gogo/protobuf/proto"
"github.com/google/go-cmp/cmp"
tracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"go.opentelemetry.io/otel/api/core"
apitrace "go.opentelemetry.io/otel/api/trace"
export "go.opentelemetry.io/otel/sdk/export/trace"
"go.opentelemetry.io/otel/sdk/resource"
)
func TestSpanKind(t *testing.T) {
for _, test := range []struct {
kind apitrace.SpanKind
expected tracepb.Span_SpanKind
}{
{
apitrace.SpanKindInternal,
tracepb.Span_INTERNAL,
},
{
apitrace.SpanKindClient,
tracepb.Span_CLIENT,
},
{
apitrace.SpanKindServer,
tracepb.Span_SERVER,
},
{
apitrace.SpanKindProducer,
tracepb.Span_PRODUCER,
},
{
apitrace.SpanKindConsumer,
tracepb.Span_CONSUMER,
},
{
apitrace.SpanKind(-1),
tracepb.Span_SPAN_KIND_UNSPECIFIED,
},
} {
assert.Equal(t, test.expected, spanKind(test.kind))
}
}
func TestNilSpanEvent(t *testing.T) {
assert.Nil(t, spanEvents(nil))
}
func TestEmptySpanEvent(t *testing.T) {
assert.Nil(t, spanEvents([]export.Event{}))
}
func TestSpanEvent(t *testing.T) {
attrs := []core.KeyValue{core.Key("one").Int(1), core.Key("two").Int(2)}
now := time.Now()
got := spanEvents([]export.Event{
{
Name: "test 1",
Attributes: []core.KeyValue{},
Time: now,
},
{
Name: "test 2",
Attributes: attrs,
Time: now,
},
})
if !assert.Len(t, got, 2) {
return
}
uNow := uint64(now.Nanosecond())
assert.Equal(t, &tracepb.Span_Event{Name: "test 1", Attributes: nil, TimeUnixNano: uNow}, got[0])
// Do not test Attributes directly, just that the return value goes to the correct field.
assert.Equal(t, &tracepb.Span_Event{Name: "test 2", Attributes: Attributes(attrs), TimeUnixNano: uNow}, got[1])
}
func TestExcessiveSpanEvents(t *testing.T) {
e := make([]export.Event, maxMessageEventsPerSpan+1)
for i := 0; i < maxMessageEventsPerSpan+1; i++ {
e[i] = export.Event{Name: strconv.Itoa(i)}
}
assert.Len(t, e, maxMessageEventsPerSpan+1)
got := spanEvents(e)
assert.Len(t, got, maxMessageEventsPerSpan)
// Ensure the drop order.
assert.Equal(t, strconv.Itoa(maxMessageEventsPerSpan-1), got[len(got)-1].Name)
}
func TestNilLinks(t *testing.T) {
assert.Nil(t, links(nil))
}
func TestEmptyLinks(t *testing.T) {
assert.Nil(t, links([]apitrace.Link{}))
}
func TestLinks(t *testing.T) {
attrs := []core.KeyValue{core.Key("one").Int(1), core.Key("two").Int(2)}
l := []apitrace.Link{
{},
{
SpanContext: core.EmptySpanContext(),
Attributes: attrs,
},
}
got := links(l)
// Make sure we get the same number back first.
if !assert.Len(t, got, 2) {
return
}
// Empty should be empty.
expected := &tracepb.Span_Link{
TraceId: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
SpanId: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
}
assert.Equal(t, expected, got[0])
// Do not test Attributes directly, just that the return value goes to the correct field.
expected.Attributes = Attributes(attrs)
assert.Equal(t, expected, got[1])
// Changes to our links should not change the produced links.
l[1].TraceID[0] = byte(0x1)
l[1].SpanID[0] = byte(0x1)
assert.Equal(t, expected, got[1])
}
func TestStatus(t *testing.T) {
for _, test := range []struct {
grpcCode codes.Code
message string
otlpStatus tracepb.Status_StatusCode
}{
{
codes.OK,
"test OK",
tracepb.Status_Ok,
},
{
codes.Canceled,
//nolint
"test CANCELLED",
//nolint
tracepb.Status_Cancelled,
},
{
codes.Unknown,
"test UNKNOWN",
tracepb.Status_UnknownError,
},
{
codes.InvalidArgument,
"test INVALID_ARGUMENT",
tracepb.Status_InvalidArgument,
},
{
codes.DeadlineExceeded,
"test DEADLINE_EXCEEDED",
tracepb.Status_DeadlineExceeded,
},
{
codes.NotFound,
"test NOT_FOUND",
tracepb.Status_NotFound,
},
{
codes.AlreadyExists,
"test ALREADY_EXISTS",
tracepb.Status_AlreadyExists,
},
{
codes.PermissionDenied,
"test PERMISSION_DENIED",
tracepb.Status_PermissionDenied,
},
{
codes.ResourceExhausted,
"test RESOURCE_EXHAUSTED",
tracepb.Status_ResourceExhausted,
},
{
codes.FailedPrecondition,
"test FAILED_PRECONDITION",
tracepb.Status_FailedPrecondition,
},
{
codes.Aborted,
"test ABORTED",
tracepb.Status_Aborted,
},
{
codes.OutOfRange,
"test OUT_OF_RANGE",
tracepb.Status_OutOfRange,
},
{
codes.Unimplemented,
"test UNIMPLEMENTED",
tracepb.Status_Unimplemented,
},
{
codes.Internal,
"test INTERNAL",
tracepb.Status_InternalError,
},
{
codes.Unavailable,
"test UNAVAILABLE",
tracepb.Status_Unavailable,
},
{
codes.DataLoss,
"test DATA_LOSS",
tracepb.Status_DataLoss,
},
{
codes.Unauthenticated,
"test UNAUTHENTICATED",
tracepb.Status_Unauthenticated,
},
} {
expected := &tracepb.Status{Code: test.otlpStatus, Message: test.message}
assert.Equal(t, expected, status(test.grpcCode, test.message))
}
}
func TestNilSpan(t *testing.T) {
assert.Nil(t, span(nil))
}
func TestNilSpanData(t *testing.T) {
assert.Nil(t, SpanData(nil))
}
func TestEmptySpanData(t *testing.T) {
assert.Nil(t, SpanData(nil))
}
func TestSpanData(t *testing.T) {
// Full test of span data transform.
startTime := time.Now()
endTime := startTime.Add(10 * time.Second)
spanData := &export.SpanData{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
SpanID: core.SpanID{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
},
SpanKind: apitrace.SpanKindServer,
ParentSpanID: core.SpanID{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
Name: "span data to span data",
StartTime: startTime,
EndTime: endTime,
MessageEvents: []export.Event{
{Time: startTime,
Attributes: []core.KeyValue{
core.Key("CompressedByteSize").Uint64(512),
},
},
{Time: endTime,
Attributes: []core.KeyValue{
core.Key("MessageEventType").String("Recv"),
},
},
},
Links: []apitrace.Link{
{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF},
SpanID: core.SpanID{0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7},
TraceFlags: 0,
},
Attributes: []core.KeyValue{
core.Key("LinkType").String("Parent"),
},
},
{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF},
SpanID: core.SpanID{0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7},
TraceFlags: 0,
},
Attributes: []core.KeyValue{
core.Key("LinkType").String("Child"),
},
},
},
StatusCode: codes.Internal,
StatusMessage: "utterly unrecognized",
HasRemoteParent: true,
Attributes: []core.KeyValue{
core.Key("timeout_ns").Int64(12e9),
},
DroppedAttributeCount: 1,
DroppedMessageEventCount: 2,
DroppedLinkCount: 3,
Resource: resource.New(core.Key("rk1").String("rv1"), core.Key("rk2").Int64(5)),
}
// Not checking resource as the underlying map of our Resource makes
// ordering impossible to guarantee on the output. The Resource
// transform function has unit tests that should suffice.
expectedSpan := &tracepb.Span{
TraceId: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
SpanId: []byte{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
ParentSpanId: []byte{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
Name: spanData.Name,
Kind: tracepb.Span_SERVER,
StartTimeUnixNano: uint64(startTime.Nanosecond()),
EndTimeUnixNano: uint64(endTime.Nanosecond()),
Status: status(spanData.StatusCode, spanData.StatusMessage),
Events: spanEvents(spanData.MessageEvents),
Links: links(spanData.Links),
Attributes: Attributes(spanData.Attributes),
DroppedAttributesCount: 1,
DroppedEventsCount: 2,
DroppedLinksCount: 3,
}
got := SpanData([]*export.SpanData{spanData})
if !assert.Len(t, got, 1) {
return
}
// Break the span down as large diffs can be hard to read.
actualSpans := got[0].GetInstrumentationLibrarySpans()
if !assert.Len(t, actualSpans, 1) && !assert.Len(t, actualSpans[0].Spans, 1) {
return
}
actualSpan := actualSpans[0].Spans[0]
if diff := cmp.Diff(expectedSpan, actualSpan, cmp.Comparer(proto.Equal)); diff != "" {
t.Fatalf("transformed span differs %v\n", diff)
}
}

View File

@ -23,15 +23,12 @@ import (
"sync"
"unsafe"
"go.opentelemetry.io/otel/sdk/resource"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
colmetricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/collector/metrics/v1"
coltracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/collector/trace/v1"
metricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"
tracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"
"go.opentelemetry.io/otel/exporters/otlp/internal/transform"
metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
@ -349,37 +346,6 @@ func (e *Exporter) ExportSpans(ctx context.Context, sds []*tracesdk.SpanData) {
e.uploadTraces(ctx, sds)
}
func otSpanDataToPbSpans(sdl []*tracesdk.SpanData) []*tracepb.ResourceSpans {
if len(sdl) == 0 {
return nil
}
rsm := make(map[*resource.Resource]*tracepb.ResourceSpans)
for _, sd := range sdl {
if sd != nil {
rs, ok := rsm[sd.Resource]
if !ok {
rs = &tracepb.ResourceSpans{
Resource: otResourceToProtoResource(sd.Resource),
InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{
{
Spans: []*tracepb.Span{},
},
},
}
rsm[sd.Resource] = rs
}
rs.InstrumentationLibrarySpans[0].Spans =
append(rs.InstrumentationLibrarySpans[0].Spans, otSpanToProtoSpan(sd))
}
}
rss := make([]*tracepb.ResourceSpans, 0, len(rsm))
for _, rs := range rsm {
rss = append(rss, rs)
}
return rss
}
func (e *Exporter) uploadTraces(ctx context.Context, sdl []*tracesdk.SpanData) {
select {
case <-e.stopCh:
@ -390,7 +356,7 @@ func (e *Exporter) uploadTraces(ctx context.Context, sdl []*tracesdk.SpanData) {
return
}
protoSpans := otSpanDataToPbSpans(sdl)
protoSpans := transform.SpanData(sdl)
if len(protoSpans) == 0 {
return
}

View File

@ -1,433 +0,0 @@
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otlp_test
import (
"context"
"testing"
"time"
"go.opentelemetry.io/otel/sdk/resource"
"github.com/golang/protobuf/proto"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/codes"
commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
resourcepb "github.com/open-telemetry/opentelemetry-proto/gen/go/resource/v1"
tracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"
"go.opentelemetry.io/otel/api/core"
apitrace "go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/exporters/otlp"
export "go.opentelemetry.io/otel/sdk/export/trace"
)
type testCases struct {
otSpan []*export.SpanData
otlpResSpan *tracepb.ResourceSpans
}
func TestOtSpanToOtlpSpan_Basic(t *testing.T) {
// The goal of this test is to ensure that each
// spanData is transformed and exported correctly!
testAndVerify("Basic End-2-End", t, func(t *testing.T) testCases {
startTime := time.Now()
endTime := startTime.Add(10 * time.Second)
tc := testCases{
otSpan: []*export.SpanData{
{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
SpanID: core.SpanID{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
},
SpanKind: apitrace.SpanKindServer,
ParentSpanID: core.SpanID{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
Name: "End-To-End Here",
StartTime: startTime,
EndTime: endTime,
MessageEvents: []export.Event{
{Time: startTime,
Attributes: []core.KeyValue{
core.Key("CompressedByteSize").Uint64(512),
},
},
{Time: endTime,
Attributes: []core.KeyValue{
core.Key("MessageEventType").String("Recv"),
},
},
},
Links: []apitrace.Link{
{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF},
SpanID: core.SpanID{0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7},
TraceFlags: 0,
},
Attributes: []core.KeyValue{
core.Key("LinkType").String("Parent"),
},
},
{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF},
SpanID: core.SpanID{0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7},
TraceFlags: 0,
},
Attributes: []core.KeyValue{
core.Key("LinkType").String("Child"),
},
},
},
StatusCode: codes.Internal,
StatusMessage: "utterly unrecognized",
HasRemoteParent: true,
Attributes: []core.KeyValue{
core.Key("timeout_ns").Int64(12e9),
},
DroppedAttributeCount: 1,
DroppedMessageEventCount: 2,
DroppedLinkCount: 3,
Resource: resource.New(core.Key("rk1").String("rv1"),
core.Key("rk2").Int64(5)),
},
},
otlpResSpan: &tracepb.ResourceSpans{
InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{
{
Spans: []*tracepb.Span{
{
TraceId: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
SpanId: []byte{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
ParentSpanId: []byte{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
Name: "End-To-End Here",
Kind: tracepb.Span_SERVER,
StartTimeUnixNano: uint64(startTime.Nanosecond()),
EndTimeUnixNano: uint64(endTime.Nanosecond()),
Status: &tracepb.Status{
Code: 13,
Message: "utterly unrecognized",
},
Events: []*tracepb.Span_Event{
{
TimeUnixNano: uint64(startTime.Nanosecond()),
Attributes: []*commonpb.AttributeKeyValue{
{
Key: "CompressedByteSize",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 512,
},
},
},
{
TimeUnixNano: uint64(endTime.Nanosecond()),
Attributes: []*commonpb.AttributeKeyValue{
{
Key: "MessageEventType",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "Recv",
},
},
},
},
Links: []*tracepb.Span_Link{
{
TraceId: []byte{0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF},
SpanId: []byte{0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7},
Attributes: []*commonpb.AttributeKeyValue{
{
Key: "LinkType",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "Parent",
},
},
},
{
TraceId: []byte{0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF},
SpanId: []byte{0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7},
Attributes: []*commonpb.AttributeKeyValue{
{
Key: "LinkType",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "Child",
},
},
},
},
Attributes: []*commonpb.AttributeKeyValue{
{
Key: "timeout_ns",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 12e9,
},
},
DroppedAttributesCount: 1,
DroppedEventsCount: 2,
DroppedLinksCount: 3,
},
},
},
},
Resource: &resourcepb.Resource{
Attributes: []*commonpb.AttributeKeyValue{
{
Key: "rk1",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "rv1",
},
{
Key: "rk2",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 5,
},
},
},
},
}
return tc
})
}
func TestOtSpanToOtlpSpan_SpanKind(t *testing.T) {
testAndVerify("Test SpanKind", t, func(t *testing.T) testCases {
kinds := []struct {
in apitrace.SpanKind
out tracepb.Span_SpanKind
}{
{
in: apitrace.SpanKindClient,
out: tracepb.Span_CLIENT,
},
{
in: apitrace.SpanKindServer,
out: tracepb.Span_SERVER,
},
{
in: apitrace.SpanKindProducer,
out: tracepb.Span_PRODUCER,
},
{
in: apitrace.SpanKindConsumer,
out: tracepb.Span_CONSUMER,
},
{
in: apitrace.SpanKindInternal,
out: tracepb.Span_INTERNAL,
},
{
in: apitrace.SpanKindUnspecified,
out: tracepb.Span_SPAN_KIND_UNSPECIFIED,
},
}
otSpans := make([]*export.SpanData, 0, len(kinds))
otlpSpans := make([]*tracepb.Span, 0, len(kinds))
for _, kind := range kinds {
otSpan, otlpSpan := getSpan()
otSpan.SpanKind = kind.in
otlpSpan.Kind = kind.out
otSpans = append(otSpans, otSpan)
otlpSpans = append(otlpSpans, otlpSpan)
}
tc := testCases{
otSpan: otSpans,
otlpResSpan: &tracepb.ResourceSpans{
Resource: nil,
InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{
{
Spans: otlpSpans,
},
},
},
}
return tc
})
}
func TestOtSpanToOtlpSpan_Attribute(t *testing.T) {
testAndVerify("Test SpanAttribute", t, func(t *testing.T) testCases {
attrInt := &commonpb.AttributeKeyValue{
Key: "commonInt",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 25,
}
attrInt64 := &commonpb.AttributeKeyValue{
Key: "commonInt64",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 12e9,
}
kinds := []struct {
in core.KeyValue
out *commonpb.AttributeKeyValue
}{
{
in: core.Key("commonInt").Int(25),
out: attrInt,
},
{
in: core.Key("commonInt").Uint(25),
out: attrInt,
},
{
in: core.Key("commonInt").Int32(25),
out: attrInt,
},
{
in: core.Key("commonInt").Uint32(25),
out: attrInt,
},
{
in: core.Key("commonInt64").Int64(12e9),
out: attrInt64,
},
{
in: core.Key("commonInt64").Uint64(12e9),
out: attrInt64,
},
{
in: core.Key("float32").Float32(3.598549),
out: &commonpb.AttributeKeyValue{
Key: "float32",
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: 3.5985488891601562,
},
},
{
in: core.Key("float64").Float64(14.598549),
out: &commonpb.AttributeKeyValue{
Key: "float64",
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: 14.598549,
},
},
{
in: core.Key("string").String("string"),
out: &commonpb.AttributeKeyValue{
Key: "string",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "string",
},
},
{
in: core.Key("bool").Bool(true),
out: &commonpb.AttributeKeyValue{
Key: "bool",
Type: commonpb.AttributeKeyValue_BOOL,
BoolValue: true,
},
},
}
otSpans := make([]*export.SpanData, 0, len(kinds))
otlpSpans := make([]*tracepb.Span, 0, len(kinds))
for _, kind := range kinds {
otSpan, otlpSpan := getSpan()
otSpan.Attributes = []core.KeyValue{kind.in}
otlpSpan.Attributes = []*commonpb.AttributeKeyValue{kind.out}
otSpans = append(otSpans, otSpan)
otlpSpans = append(otlpSpans, otlpSpan)
}
tc := testCases{
otSpan: otSpans,
otlpResSpan: &tracepb.ResourceSpans{
Resource: nil,
InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{
{
Spans: otlpSpans,
},
},
},
}
return tc
})
}
func getSpan() (*export.SpanData, *tracepb.Span) {
startTime := time.Now()
endTime := startTime.Add(10 * time.Second)
otSpan := &export.SpanData{
SpanContext: core.SpanContext{
TraceID: core.TraceID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
SpanID: core.SpanID{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
},
SpanKind: apitrace.SpanKindServer,
ParentSpanID: core.SpanID{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
Name: "Test Span",
StartTime: startTime,
EndTime: endTime,
}
otlpSpan := &tracepb.Span{
TraceId: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F},
SpanId: []byte{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
ParentSpanId: []byte{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
Name: "Test Span",
Kind: tracepb.Span_SERVER,
StartTimeUnixNano: uint64(startTime.Nanosecond()),
EndTimeUnixNano: uint64(endTime.Nanosecond()),
Status: &tracepb.Status{
Code: 0,
},
}
return otSpan, otlpSpan
}
func testAndVerify(name string, t *testing.T, f func(t *testing.T) testCases) {
// The goal of this test is to ensure that each
// spanData is transformed and exported correctly!
collector := runMockCol(t)
defer func() {
_ = collector.stop()
}()
exp, err := otlp.NewExporter(otlp.WithInsecure(),
otlp.WithAddress(collector.address),
otlp.WithReconnectionPeriod(50*time.Millisecond))
if err != nil {
t.Fatalf("Failed to create a new collector exporter: %v", err)
}
defer func() {
_ = exp.Stop()
}()
// Give the background collector connection sometime to setup.
<-time.After(20 * time.Millisecond)
tc := f(t)
exp.ExportSpans(context.Background(), tc.otSpan)
_ = exp.Stop()
_ = collector.stop()
rss := collector.getResourceSpans()
gotCount := len(rss)
wantCount := 1
if gotCount != wantCount {
t.Fatalf("%s: ResourceSpans: got %d, want %d", name, gotCount, wantCount)
}
if diff := cmp.Diff(rss[0], tc.otlpResSpan, cmp.Comparer(proto.Equal)); diff != "" {
t.Fatalf("%s transformed span differs %v\n", name, diff)
}
}