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

Upgrade OTLP exporter to opentelemetry-proto matching the opentelemetry-collector v0.4.0 release #866

Merged
merged 10 commits into from
Jun 24, 2020
2 changes: 2 additions & 0 deletions example/otel-collector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ github.com/open-telemetry/opentelemetry-collector v0.3.0 h1:/i106g9t6xNQ4hOAuczE
github.com/open-telemetry/opentelemetry-collector v0.3.0/go.mod h1:c5EgyLBK6FoGCaJpOEQ0j+sHqmfuoRzOm29tdVA/wDg=
github.com/open-telemetry/opentelemetry-proto v0.3.0 h1:+ASAtcayvoELyCF40+rdCMlBOhZIn5TPDez85zSYc30=
github.com/open-telemetry/opentelemetry-proto v0.3.0/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
github.com/open-telemetry/opentelemetry-proto v0.4.0 h1:7EGs7QkdnR039zcQv71/wPLeeUUzqpH855VEWN4IHTE=
github.com/open-telemetry/opentelemetry-proto v0.4.0/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/google/go-cmp v0.5.0
github.com/grpc-ecosystem/grpc-gateway v1.14.3 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/open-telemetry/opentelemetry-proto v0.3.0
github.com/open-telemetry/opentelemetry-proto v0.4.0
github.com/stretchr/testify v1.6.1
go.opentelemetry.io/otel v0.6.0
golang.org/x/text v0.3.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/open-telemetry/opentelemetry-proto v0.3.0 h1:+ASAtcayvoELyCF40+rdCMlBOhZIn5TPDez85zSYc30=
github.com/open-telemetry/opentelemetry-proto v0.3.0/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
github.com/open-telemetry/opentelemetry-proto v0.4.0 h1:7EGs7QkdnR039zcQv71/wPLeeUUzqpH855VEWN4IHTE=
github.com/open-telemetry/opentelemetry-proto v0.4.0/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
39 changes: 16 additions & 23 deletions exporters/otlp/internal/transform/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,69 +24,62 @@ import (
)

// Attributes transforms a slice of KeyValues into a slice of OTLP attribute key-values.
func Attributes(attrs []kv.KeyValue) []*commonpb.AttributeKeyValue {
func Attributes(attrs []kv.KeyValue) []*commonpb.KeyValue {
if len(attrs) == 0 {
return nil
}

out := make([]*commonpb.AttributeKeyValue, 0, len(attrs))
out := make([]*commonpb.KeyValue, 0, len(attrs))
for _, kv := range attrs {
out = append(out, toAttribute(kv))
}
return out
}

// ResourceAttributes transforms a Resource into a slice of OTLP attribute key-values.
func ResourceAttributes(resource *resource.Resource) []*commonpb.AttributeKeyValue {
func ResourceAttributes(resource *resource.Resource) []*commonpb.KeyValue {
if resource.Len() == 0 {
return nil
}

out := make([]*commonpb.AttributeKeyValue, 0, resource.Len())
out := make([]*commonpb.KeyValue, 0, resource.Len())
for iter := resource.Iter(); iter.Next(); {
out = append(out, toAttribute(iter.Attribute()))
}

return out
}

func toAttribute(v kv.KeyValue) *commonpb.AttributeKeyValue {
func toAttribute(v kv.KeyValue) *commonpb.KeyValue {
result := &commonpb.KeyValue{
Key: string(v.Key),
Value: new(commonpb.AnyValue),
}
switch v.Value.Type() {
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
case value.BOOL:
return &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_BOOL,
result.Value.Value = &commonpb.AnyValue_BoolValue{
BoolValue: v.Value.AsBool(),
}
case value.INT64, value.INT32, value.UINT32, value.UINT64:
return &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_INT,
result.Value.Value = &commonpb.AnyValue_IntValue{
IntValue: v.Value.AsInt64(),
}
case value.FLOAT32:
return &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_DOUBLE,
result.Value.Value = &commonpb.AnyValue_DoubleValue{
DoubleValue: float64(v.Value.AsFloat32()),
}
case value.FLOAT64:
return &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_DOUBLE,
result.Value.Value = &commonpb.AnyValue_DoubleValue{
DoubleValue: v.Value.AsFloat64(),
}
case value.STRING:
return &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_STRING,
result.Value.Value = &commonpb.AnyValue_StringValue{
StringValue: v.Value.AsString(),
}
default:
return &commonpb.AttributeKeyValue{
Key: string(v.Key),
Type: commonpb.AttributeKeyValue_STRING,
result.Value.Value = &commonpb.AnyValue_StringValue{
StringValue: "INVALID",
}
}
return result
}
105 changes: 70 additions & 35 deletions exporters/otlp/internal/transform/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
func TestAttributes(t *testing.T) {
for _, test := range []struct {
attrs []kv.KeyValue
expected []*commonpb.AttributeKeyValue
expected []*commonpb.KeyValue
}{
{nil, nil},
{
Expand All @@ -42,56 +42,86 @@ func TestAttributes(t *testing.T) {
kv.String("string to string", "string"),
kv.Bool("bool to bool", true),
},
[]*commonpb.AttributeKeyValue{
[]*commonpb.KeyValue{
{
Key: "int to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 123,
Key: "int to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 123,
},
},
},
{
Key: "uint to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 1234,
Key: "uint to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 1234,
},
},
},
{
Key: "int32 to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 12345,
Key: "int32 to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 12345,
},
},
},
{
Key: "uint32 to int",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 123456,
Key: "uint32 to int",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 123456,
},
},
},
{
Key: "int64 to int64",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 1234567,
Key: "int64 to int64",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 1234567,
},
},
},
{
Key: "uint64 to int64",
Type: commonpb.AttributeKeyValue_INT,
IntValue: 12345678,
Key: "uint64 to int64",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: 12345678,
},
},
},
{
Key: "float32 to double",
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: 3.14,
Key: "float32 to double",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_DoubleValue{
DoubleValue: 3.14,
},
},
},
{
Key: "float64 to double",
Type: commonpb.AttributeKeyValue_DOUBLE,
DoubleValue: 1.61,
Key: "float64 to double",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_DoubleValue{
DoubleValue: 1.61,
},
},
},
{
Key: "string to string",
Type: commonpb.AttributeKeyValue_STRING,
StringValue: "string",
Key: "string to string",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_StringValue{
StringValue: "string",
},
},
},
{
Key: "bool to bool",
Type: commonpb.AttributeKeyValue_BOOL,
BoolValue: true,
Key: "bool to bool",
Value: &commonpb.AnyValue{
Value: &commonpb.AnyValue_BoolValue{
BoolValue: true,
},
},
},
},
},
Expand All @@ -101,11 +131,16 @@ func TestAttributes(t *testing.T) {
continue
}
for i, actual := range got {
if actual.Type == commonpb.AttributeKeyValue_DOUBLE {
if !assert.InDelta(t, test.expected[i].DoubleValue, actual.DoubleValue, 0.01) {
if a, ok := actual.Value.Value.(*commonpb.AnyValue_DoubleValue); ok {
e, ok := test.expected[i].Value.Value.(*commonpb.AnyValue_DoubleValue)
if !ok {
t.Errorf("expected AnyValue_DoubleValue, got %T", test.expected[i].Value.Value)
continue
}
test.expected[i].DoubleValue = actual.DoubleValue
if !assert.InDelta(t, e.DoubleValue, a.DoubleValue, 0.01) {
continue
}
e.DoubleValue = a.DoubleValue
}
assert.Equal(t, test.expected[i], actual)
}
Expand Down
13 changes: 7 additions & 6 deletions exporters/otlp/internal/transform/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,26 @@ func sum(record export.Record, a aggregation.Sum) (*metricpb.Metric, error) {
Name: desc.Name(),
Description: desc.Description(),
Unit: string(desc.Unit()),
Labels: stringKeyValues(labels.Iter()),
},
}

switch n := desc.NumberKind(); n {
case metric.Int64NumberKind:
m.MetricDescriptor.Type = metricpb.MetricDescriptor_COUNTER_INT64
m.MetricDescriptor.Type = metricpb.MetricDescriptor_INT64
m.Int64DataPoints = []*metricpb.Int64DataPoint{
{
Value: sum.CoerceToInt64(n),
Labels: stringKeyValues(labels.Iter()),
StartTimeUnixNano: uint64(record.StartTime().UnixNano()),
TimeUnixNano: uint64(record.EndTime().UnixNano()),
},
}
case metric.Float64NumberKind:
m.MetricDescriptor.Type = metricpb.MetricDescriptor_COUNTER_DOUBLE
m.MetricDescriptor.Type = metricpb.MetricDescriptor_DOUBLE
m.DoubleDataPoints = []*metricpb.DoubleDataPoint{
{
Value: sum.CoerceToFloat64(n),
Labels: stringKeyValues(labels.Iter()),
StartTimeUnixNano: uint64(record.StartTime().UnixNano()),
TimeUnixNano: uint64(record.EndTime().UnixNano()),
},
Expand Down Expand Up @@ -322,12 +323,12 @@ func minMaxSumCount(record export.Record, a aggregation.MinMaxSumCount) (*metric
Description: desc.Description(),
Unit: string(desc.Unit()),
Type: metricpb.MetricDescriptor_SUMMARY,
Labels: stringKeyValues(labels.Iter()),
},
SummaryDataPoints: []*metricpb.SummaryDataPoint{
{
Count: uint64(count),
Sum: sum.CoerceToFloat64(numKind),
Labels: stringKeyValues(labels.Iter()),
Count: uint64(count),
Sum: sum.CoerceToFloat64(numKind),
PercentileValues: []*metricpb.SummaryDataPoint_ValueAtPercentile{
{
Percentile: 0.0,
Expand Down
8 changes: 2 additions & 6 deletions exporters/otlp/internal/transform/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func TestMinMaxSumCountMetricDescriptor(t *testing.T) {
Description: "test-a-description",
Unit: "1",
Type: metricpb.MetricDescriptor_SUMMARY,
Labels: nil,
},
},
{
Expand All @@ -148,7 +147,6 @@ func TestMinMaxSumCountMetricDescriptor(t *testing.T) {
Description: "test-b-description",
Unit: "By",
Type: metricpb.MetricDescriptor_SUMMARY,
Labels: []*commonpb.StringKeyValue{{Key: "A", Value: "1"}},
},
},
}
Expand Down Expand Up @@ -239,8 +237,7 @@ func TestSumMetricDescriptor(t *testing.T) {
Name: "sum-test-a",
Description: "test-a-description",
Unit: "1",
Type: metricpb.MetricDescriptor_COUNTER_INT64,
Labels: nil,
Type: metricpb.MetricDescriptor_INT64,
},
},
{
Expand All @@ -254,8 +251,7 @@ func TestSumMetricDescriptor(t *testing.T) {
Name: "sum-test-b",
Description: "test-b-description",
Unit: "ms",
Type: metricpb.MetricDescriptor_COUNTER_DOUBLE,
Labels: []*commonpb.StringKeyValue{{Key: "A", Value: "1"}},
Type: metricpb.MetricDescriptor_DOUBLE,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/mock_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func resourceString(res *resourcepb.Resource) string {
return rstr
}

func sortedAttributes(attrs []*commonpb.AttributeKeyValue) []*commonpb.AttributeKeyValue {
func sortedAttributes(attrs []*commonpb.KeyValue) []*commonpb.KeyValue {
sort.Slice(attrs[:], func(i, j int) bool {
return attrs[i].Key < attrs[j].Key
})
Expand Down
7 changes: 4 additions & 3 deletions exporters/otlp/otlp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
metricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"

"go.opentelemetry.io/otel/api/kv"
Expand Down Expand Up @@ -212,7 +213,7 @@ func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption)
if gotName, want := s.Name, "AlwaysSample"; gotName != want {
t.Fatalf("span name: got %s, want %s", gotName, want)
}
attrMap[s.Attributes[0].IntValue] = true
attrMap[s.Attributes[0].Value.Value.(*commonpb.AnyValue_IntValue).IntValue] = true
}
if got, want := len(attrMap), m; got != want {
t.Fatalf("span attribute unique values: got %d want %d", got, want)
Expand Down Expand Up @@ -241,12 +242,12 @@ func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption)
case metric.CounterKind:
switch data.nKind {
case metricapi.Int64NumberKind:
assert.Equal(t, metricpb.MetricDescriptor_COUNTER_INT64.String(), desc.GetType().String())
assert.Equal(t, metricpb.MetricDescriptor_INT64.String(), desc.GetType().String())
if dp := m.GetInt64DataPoints(); assert.Len(t, dp, 1) {
assert.Equal(t, data.val, dp[0].Value, "invalid value for %q", desc.Name)
}
case metricapi.Float64NumberKind:
assert.Equal(t, metricpb.MetricDescriptor_COUNTER_DOUBLE.String(), desc.GetType().String())
assert.Equal(t, metricpb.MetricDescriptor_DOUBLE.String(), desc.GetType().String())
if dp := m.GetDoubleDataPoints(); assert.Len(t, dp, 1) {
assert.Equal(t, float64(data.val), dp[0].Value, "invalid value for %q", desc.Name)
}
Expand Down
Loading