diff --git a/translator/trace/zipkin/protospan_to_zipkinv1.go b/translator/trace/zipkin/protospan_to_zipkinv1.go deleted file mode 100644 index d3be1c867a5..00000000000 --- a/translator/trace/zipkin/protospan_to_zipkinv1.go +++ /dev/null @@ -1,374 +0,0 @@ -// 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 zipkin - -import ( - "net" - "strconv" - "time" - - commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1" - resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" - tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1" - zipkinmodel "github.com/openzipkin/zipkin-go/model" - "github.com/pkg/errors" - - "go.opentelemetry.io/collector/internal" - tracetranslator "go.opentelemetry.io/collector/translator/trace" -) - -const ( - statusCodeTagKey = "error" - statusDescriptionTagKey = "opencensus.status_description" -) - -var ( - sampledTrue = true - canonicalCodes = [...]string{ - "OK", - "CANCELLED", - "UNKNOWN", - "INVALID_ARGUMENT", - "DEADLINE_EXCEEDED", - "NOT_FOUND", - "ALREADY_EXISTS", - "PERMISSION_DENIED", - "RESOURCE_EXHAUSTED", - "FAILED_PRECONDITION", - "ABORTED", - "OUT_OF_RANGE", - "UNIMPLEMENTED", - "INTERNAL", - "UNAVAILABLE", - "DATA_LOSS", - "UNAUTHENTICATED", - } - errNilSpan = errors.New("expected a non-nil span") -) - -func canonicalCodeString(code int32) string { - if code < 0 || int(code) >= len(canonicalCodes) { - return "error code " + strconv.FormatInt(int64(code), 10) - } - return canonicalCodes[code] -} - -// zipkinEndpointFromAttributes extracts zipkin endpoint information -// from a set of attributes (in the format of OC SpanData). It returns the built -// zipkin endpoint and insert the attribute keys that were made redundant -// (because now they can be represented by the endpoint) into the redundantKeys -// map (function assumes that this was created by the caller). Per call at most -// 3 attribute keys can be made redundant. -func zipkinEndpointFromAttributes( - attrMap map[string]*tracepb.AttributeValue, - serviceName string, - endpointType zipkinDirection, - redundantKeys map[string]bool, -) (endpoint *zipkinmodel.Endpoint) { - - if attrMap == nil { - if serviceName != "" { - return &zipkinmodel.Endpoint{ - ServiceName: serviceName, - } - } - return nil - } - - // The data in the Attributes map was saved in the format - // { - // "ipv4": "192.168.99.101", - // "port": "9000", - // "serviceName": "backend", - // } - - var ipv4Key, ipv6Key, portKey string - if endpointType == isLocalEndpoint { - ipv4Key, ipv6Key, portKey = LocalEndpointIPv4, LocalEndpointIPv6, LocalEndpointPort - } else { - ipv4Key, ipv6Key, portKey = RemoteEndpointIPv4, RemoteEndpointIPv6, RemoteEndpointPort - } - - var ip net.IP - ipv6Selected := false - - if ipv4Str, ok := extractStringAttribute(attrMap, ipv4Key); ok { - ip = net.ParseIP(ipv4Str) - redundantKeys[ipv4Key] = true - } else if ipv6Str, ok := extractStringAttribute(attrMap, ipv6Key); ok { - ip = net.ParseIP(ipv6Str) - ipv6Selected = true - redundantKeys[ipv6Key] = true - } - - var port uint64 - if portStr, ok := extractStringAttribute(attrMap, portKey); ok { - port, _ = strconv.ParseUint(portStr, 10, 16) - redundantKeys[portKey] = true - } - - if serviceName == "" && len(ip) == 0 && port == 0 { - // Nothing to put on the endpoint - return nil - } - - zEndpoint := &zipkinmodel.Endpoint{ - ServiceName: serviceName, - Port: uint16(port), - } - - if ipv6Selected { - zEndpoint.IPv6 = ip - } else { - zEndpoint.IPv4 = ip - } - - return zEndpoint -} - -func extractStringAttribute(attributes map[string]*tracepb.AttributeValue, key string) (string, bool) { - av, ok := attributes[key] - if !ok || av == nil || av.Value == nil { - return "", false - } - - if value, ok := av.Value.(*tracepb.AttributeValue_StringValue); ok { - return value.StringValue.GetValue(), true - } - - return "", false -} - -func convertTraceID(t []byte) zipkinmodel.TraceID { - h, l, _ := tracetranslator.BytesToUInt64TraceID(t) - return zipkinmodel.TraceID{High: h, Low: l} -} - -func convertSpanID(s []byte) zipkinmodel.ID { - id, _ := tracetranslator.BytesToUInt64SpanID(s) - return zipkinmodel.ID(id) -} - -// spanKind returns the Kind and a boolean indicating if the kind was extracted from the attributes. -func spanKind(s *tracepb.Span) (zipkinmodel.Kind, bool) { - // First try span kinds that are set directly in SpanKind field. - switch s.Kind { - case tracepb.Span_CLIENT: - return zipkinmodel.Client, false - case tracepb.Span_SERVER: - return zipkinmodel.Server, false - } - - // SpanKind==SpanKindUnspecified, check if TagSpanKind attribute is set. - // This can happen if span kind had no equivalent in OC, so we could represent it in - // the SpanKind. In that case we had set a special attribute TagSpanKind in - // the span to preserve the span kind. Now we will do a reverse translation. - if s.Attributes == nil || s.Attributes.AttributeMap == nil { - return zipkinmodel.Undetermined, false - } - - spanKindStr, ok := extractStringAttribute(s.Attributes.AttributeMap, tracetranslator.TagSpanKind) - if ok { - // Check if it is one of the kinds that are defined by conventions. - switch tracetranslator.OpenTracingSpanKind(spanKindStr) { - case tracetranslator.OpenTracingSpanKindClient: - return zipkinmodel.Client, true - case tracetranslator.OpenTracingSpanKindServer: - return zipkinmodel.Server, true - case tracetranslator.OpenTracingSpanKindConsumer: - return zipkinmodel.Consumer, true - case tracetranslator.OpenTracingSpanKindProducer: - return zipkinmodel.Producer, true - default: - // Unknown kind. Keep the attribute, but return Undetermined to our caller. - return zipkinmodel.Undetermined, false - } - } - return zipkinmodel.Undetermined, false -} - -type zipkinDirection bool - -const ( - isLocalEndpoint zipkinDirection = true - isRemoteEndpoint zipkinDirection = false -) - -func serviceNameOrDefault(node *commonpb.Node, defaultServiceName string) string { - if node == nil || node.ServiceInfo == nil { - return defaultServiceName - } - - if node.ServiceInfo.Name == "" { - return defaultServiceName - } - - return node.ServiceInfo.Name -} - -func OCSpanProtoToZipkin( - node *commonpb.Node, - resource *resourcepb.Resource, - s *tracepb.Span, - defaultServiceName string, -) (*zipkinmodel.SpanModel, error) { - if s == nil { - return nil, errNilSpan - } - - var attrMap map[string]*tracepb.AttributeValue - if s.Attributes != nil { - attrMap = s.Attributes.AttributeMap - } - - // Per call to zipkinEndpointFromAttributes at most 3 attribute keys can be made redundant, - // give a hint when calling make that we expect at most 2*3+2 items on the map. - redundantKeys := make(map[string]bool, 8) - localEndpointServiceName := serviceNameOrDefault(node, defaultServiceName) - localEndpoint := zipkinEndpointFromAttributes(attrMap, localEndpointServiceName, isLocalEndpoint, redundantKeys) - - remoteServiceName, okServiceName := extractStringAttribute(attrMap, RemoteEndpointServiceName) - if okServiceName { - redundantKeys[RemoteEndpointServiceName] = true - } - remoteEndpoint := zipkinEndpointFromAttributes(attrMap, remoteServiceName, isRemoteEndpoint, redundantKeys) - - sk, spanKindFromAttributes := spanKind(s) - if spanKindFromAttributes { - redundantKeys[tracetranslator.TagSpanKind] = true - } - - startTime := internal.TimestampToTime(s.StartTime) - if _, ok := attrMap[StartTimeAbsent]; ok { - redundantKeys[StartTimeAbsent] = true - startTime = time.Time{} - } - - z := &zipkinmodel.SpanModel{ - SpanContext: zipkinmodel.SpanContext{ - TraceID: convertTraceID(s.TraceId), - ID: convertSpanID(s.SpanId), - Sampled: &sampledTrue, - }, - Kind: sk, - Name: s.Name.GetValue(), - Timestamp: startTime, - Shared: false, - LocalEndpoint: localEndpoint, - RemoteEndpoint: remoteEndpoint, - } - - resourceLabelesCount := 0 - if resource != nil { - resourceLabelesCount = len(resource.Labels) - } - // - z.Tags = make(map[string]string, len(attrMap)+2+resourceLabelesCount) - resourceToSpanAttributes(resource, z.Tags) - - if s.ParentSpanId != nil { - id := convertSpanID(s.ParentSpanId) - z.ParentID = &id - } - - if endTime := internal.TimestampToTime(s.EndTime); !startTime.IsZero() && !endTime.IsZero() { - z.Duration = endTime.Sub(startTime) - } - - // construct Tags from s.Attributes and s.Status. - attributesToSpanTags(attrMap, redundantKeys, z.Tags) - status := s.Status - if status != nil && (status.Code != 0 || status.Message != "") { - if z.Tags == nil { - z.Tags = make(map[string]string, 2) - } - if s.Status.Code != 0 { - z.Tags[statusCodeTagKey] = canonicalCodeString(s.Status.Code) - } - if s.Status.Message != "" { - z.Tags[statusDescriptionTagKey] = s.Status.Message - } - } - - // construct Annotations from s.Annotations and s.MessageEvents. - z.Annotations = timeEventsToSpanAnnotations(s.TimeEvents) - - return z, nil -} - -func resourceToSpanAttributes(resource *resourcepb.Resource, tags map[string]string) { - if resource != nil { - for key, value := range resource.Labels { - tags[key] = value - } - } -} - -func timeEventsToSpanAnnotations(timeEvents *tracepb.Span_TimeEvents) []zipkinmodel.Annotation { - var annotations []zipkinmodel.Annotation - // construct Annotations from s.Annotations and s.MessageEvents. - if timeEvents != nil && len(timeEvents.TimeEvent) != 0 { - annotations = make([]zipkinmodel.Annotation, 0, len(timeEvents.TimeEvent)) - for _, te := range timeEvents.TimeEvent { - if te == nil || te.Value == nil { - continue - } - - switch tme := te.Value.(type) { - case *tracepb.Span_TimeEvent_Annotation_: - annotations = append(annotations, zipkinmodel.Annotation{ - Timestamp: internal.TimestampToTime(te.Time), - Value: tme.Annotation.Description.GetValue(), - }) - case *tracepb.Span_TimeEvent_MessageEvent_: - a := zipkinmodel.Annotation{ - Timestamp: internal.TimestampToTime(te.Time), - Value: tme.MessageEvent.Type.String(), - } - annotations = append(annotations, a) - - } - } - } - return annotations -} - -func attributesToSpanTags(attrMap map[string]*tracepb.AttributeValue, redundantKeys map[string]bool, tags map[string]string) { - if len(attrMap) == 0 { - return - } - for key, value := range attrMap { - if redundantKeys[key] { - // Already represented by something other than an attribute, - // skip it. - continue - } - - switch v := value.Value.(type) { - case *tracepb.AttributeValue_BoolValue: - if v.BoolValue { - tags[key] = "true" - } else { - tags[key] = "false" - } - case *tracepb.AttributeValue_IntValue: - tags[key] = strconv.FormatInt(v.IntValue, 10) - case *tracepb.AttributeValue_DoubleValue: - tags[key] = strconv.FormatFloat(v.DoubleValue, 'f', -1, 64) - case *tracepb.AttributeValue_StringValue: - tags[key] = v.StringValue.GetValue() - } - } -} diff --git a/translator/trace/zipkin/protospan_to_zipkinv1_test.go b/translator/trace/zipkin/protospan_to_zipkinv1_test.go deleted file mode 100644 index 7a114295e0d..00000000000 --- a/translator/trace/zipkin/protospan_to_zipkinv1_test.go +++ /dev/null @@ -1,312 +0,0 @@ -// 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 zipkin - -import ( - "net" - "testing" - "time" - - commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1" - resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" - tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1" - "github.com/golang/protobuf/ptypes/wrappers" - zipkinmodel "github.com/openzipkin/zipkin-go/model" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "go.opencensus.io/trace/tracestate" - - "go.opentelemetry.io/collector/internal" -) - -func TestZipkinEndpointFromNode(t *testing.T) { - type args struct { - attributes map[string]*tracepb.AttributeValue - serviceName string - endpointType zipkinDirection - } - type want struct { - endpoint *zipkinmodel.Endpoint - redundantKeys map[string]bool - } - tests := []struct { - name string - args args - want want - }{ - { - name: "Nil attributes", - args: args{attributes: nil, serviceName: "", endpointType: isLocalEndpoint}, - want: want{ - redundantKeys: make(map[string]bool), - }, - }, - { - name: "Only svc name", - args: args{ - attributes: make(map[string]*tracepb.AttributeValue), - serviceName: "test", - endpointType: isLocalEndpoint, - }, - want: want{ - endpoint: &zipkinmodel.Endpoint{ServiceName: "test"}, - redundantKeys: make(map[string]bool), - }, - }, - { - name: "Only ipv4", - args: args{ - attributes: map[string]*tracepb.AttributeValue{ - "ipv4": {Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "1.2.3.4"}}}, - }, - serviceName: "", - endpointType: isLocalEndpoint, - }, - want: want{ - endpoint: &zipkinmodel.Endpoint{IPv4: net.ParseIP("1.2.3.4")}, - redundantKeys: map[string]bool{"ipv4": true}, - }, - }, - { - name: "Only ipv6 remote", - args: args{ - attributes: map[string]*tracepb.AttributeValue{ - RemoteEndpointIPv6: {Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}}}, - }, - serviceName: "", - endpointType: isRemoteEndpoint, - }, - want: want{ - endpoint: &zipkinmodel.Endpoint{IPv6: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")}, - redundantKeys: map[string]bool{RemoteEndpointIPv6: true}, - }, - }, - { - name: "Only port", - args: args{ - attributes: map[string]*tracepb.AttributeValue{ - "port": {Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "42"}}}, - }, - serviceName: "", - endpointType: isLocalEndpoint, - }, - want: want{ - endpoint: &zipkinmodel.Endpoint{Port: 42}, - redundantKeys: map[string]bool{"port": true}, - }, - }, - { - name: "Service name, ipv4, and port", - args: args{ - attributes: map[string]*tracepb.AttributeValue{ - "ipv4": {Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "4.3.2.1"}}}, - "port": {Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "2"}}}, - }, - serviceName: "test-svc", - endpointType: isLocalEndpoint, - }, - want: want{ - endpoint: &zipkinmodel.Endpoint{ServiceName: "test-svc", IPv4: net.ParseIP("4.3.2.1"), Port: 2}, - redundantKeys: map[string]bool{"ipv4": true, "port": true}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - redundantKeys := make(map[string]bool) - endpoint := zipkinEndpointFromAttributes( - tt.args.attributes, - tt.args.serviceName, - tt.args.endpointType, - redundantKeys) - assert.Equal(t, tt.want.endpoint, endpoint) - assert.Equal(t, tt.want.redundantKeys, redundantKeys) - }) - } -} - -func TestOCSpanProtoToZipkin_endToEnd(t *testing.T) { - // The goal of this test is to ensure that each - // tracepb.Span is transformed to its *trace.SpanData correctly! - - endTime := time.Now().Round(time.Second) - startTime := endTime.Add(-90 * time.Second) - - protoSpan := &tracepb.Span{ - TraceId: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}, - SpanId: []byte{0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8}, - ParentSpanId: []byte{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8}, - Name: &tracepb.TruncatableString{Value: "End-To-End Here"}, - Kind: tracepb.Span_SERVER, - StartTime: internal.TimeToTimestamp(startTime), - EndTime: internal.TimeToTimestamp(endTime), - Status: &tracepb.Status{ - Code: 13, - Message: "This is not a drill!", - }, - SameProcessAsParentSpan: &wrappers.BoolValue{Value: false}, - TimeEvents: &tracepb.Span_TimeEvents{ - TimeEvent: []*tracepb.Span_TimeEvent{ - { - Time: internal.TimeToTimestamp(startTime), - Value: &tracepb.Span_TimeEvent_MessageEvent_{ - MessageEvent: &tracepb.Span_TimeEvent_MessageEvent{ - Type: tracepb.Span_TimeEvent_MessageEvent_RECEIVED, UncompressedSize: 1024, CompressedSize: 512, - }, - }, - }, - { - Time: internal.TimeToTimestamp(endTime), - Value: &tracepb.Span_TimeEvent_MessageEvent_{ - MessageEvent: &tracepb.Span_TimeEvent_MessageEvent{ - Type: tracepb.Span_TimeEvent_MessageEvent_SENT, UncompressedSize: 1024, CompressedSize: 1000, - }, - }, - }, - { - Time: internal.TimeToTimestamp(endTime), - Value: &tracepb.Span_TimeEvent_Annotation_{ - Annotation: &tracepb.Span_TimeEvent_Annotation{ - Description: &tracepb.TruncatableString{Value: "Description"}, - }, - }, - }, - }, - }, - Links: &tracepb.Span_Links{ - Link: []*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}, - Type: tracepb.Span_Link_PARENT_LINKED_SPAN, - }, - { - 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}, - Type: tracepb.Span_Link_CHILD_LINKED_SPAN, - }, - }, - }, - Tracestate: &tracepb.Span_Tracestate{ - Entries: []*tracepb.Span_Tracestate_Entry{ - {Key: "foo", Value: "bar"}, - {Key: "a", Value: "b"}, - }, - }, - Attributes: &tracepb.Span_Attributes{ - AttributeMap: map[string]*tracepb.AttributeValue{ - "cache_hit": {Value: &tracepb.AttributeValue_BoolValue{BoolValue: true}}, - "ping_count": {Value: &tracepb.AttributeValue_IntValue{IntValue: 25}}, - "timeout": {Value: &tracepb.AttributeValue_DoubleValue{DoubleValue: 12.3}}, - "agent": {Value: &tracepb.AttributeValue_StringValue{ - StringValue: &tracepb.TruncatableString{Value: "ocagent"}, - }}, - }, - }, - } - - resource := &resourcepb.Resource{ - Type: "k8s", - Labels: map[string]string{ - "namespace": "kube-system", - }, - } - - gotOCSpanData, err := OCSpanProtoToZipkin(nil, resource, protoSpan, "default_service") - require.NoError(t, err, "Failed to convert from ProtoSpan to OCSpanData") - - ocTracestate, err := tracestate.New(new(tracestate.Tracestate), tracestate.Entry{Key: "foo", Value: "bar"}, - tracestate.Entry{Key: "a", Value: "b"}) - require.NotNil(t, ocTracestate) - require.NoError(t, err) - - sampled := true - parentID := zipkinmodel.ID(uint64(0xEFEEEDECEBEAE9E8)) - wantZipkinSpan := &zipkinmodel.SpanModel{ - SpanContext: zipkinmodel.SpanContext{ - TraceID: zipkinmodel.TraceID{ - High: 0x0001020304050607, - Low: 0x08090A0B0C0D0E0F}, - ID: zipkinmodel.ID(uint64(0xF1F2F3F4F5F6F7F8)), - ParentID: &parentID, - Sampled: &sampled, - }, - LocalEndpoint: &zipkinmodel.Endpoint{ - ServiceName: "default_service", - }, - Kind: zipkinmodel.Server, - Name: "End-To-End Here", - Timestamp: startTime, - Duration: endTime.Sub(startTime), - Annotations: []zipkinmodel.Annotation{ - { - Timestamp: startTime, - Value: "RECEIVED", - }, - { - Timestamp: endTime, - Value: "SENT", - }, - { - Timestamp: endTime, - Value: "Description", - }, - }, - Tags: map[string]string{ - "namespace": "kube-system", - "timeout": "12.3", - "agent": "ocagent", - "cache_hit": "true", - "ping_count": "25", - statusCodeTagKey: "INTERNAL", - statusDescriptionTagKey: "This is not a drill!", - }, - } - - assert.EqualValues(t, wantZipkinSpan, gotOCSpanData) -} - -func TestOCSpanProtoToZipkin_serviceNameNoAttributes(t *testing.T) { - // When Service Name is available, it should be extracted into LocalEndpoint - // even when other attributes (IP, Port) are not available - protoSpan := &tracepb.Span{} - resource := &resourcepb.Resource{} - node := &commonpb.Node{ - ServiceInfo: &commonpb.ServiceInfo{ - Name: "servicename", - }, - } - - gotOCSpanData, err := OCSpanProtoToZipkin(node, resource, protoSpan, "default_service") - require.NoError(t, err, "Failed to convert from ProtoSpan to OCSpanData") - - sampled := true - wantZipkinSpan := &zipkinmodel.SpanModel{ - SpanContext: zipkinmodel.SpanContext{Sampled: &sampled}, - LocalEndpoint: &zipkinmodel.Endpoint{ - ServiceName: "servicename", - }, - Tags: map[string]string{}, - } - - assert.EqualValues(t, wantZipkinSpan, gotOCSpanData) -} - -func TestOCSpanProtoToZipkin_NilSpan(t *testing.T) { - span, err := OCSpanProtoToZipkin(nil, nil, nil, "") - assert.Error(t, err) - assert.Nil(t, span) -} diff --git a/translator/trace/zipkin/zipkinv1_thrift_to_protospan.go b/translator/trace/zipkin/zipkinv1_thrift_to_protospan.go index bfbfc75c3ea..9c013b159ff 100644 --- a/translator/trace/zipkin/zipkinv1_thrift_to_protospan.go +++ b/translator/trace/zipkin/zipkinv1_thrift_to_protospan.go @@ -31,8 +31,8 @@ import ( tracetranslator "go.opentelemetry.io/collector/translator/trace" ) -// V1ThriftBatchToOCProto converts Zipkin v1 spans to OC Proto. -func V1ThriftBatchToOCProto(zSpans []*zipkincore.Span) ([]consumerdata.TraceData, error) { +// v1ThriftBatchToOCProto converts Zipkin v1 spans to OC Proto. +func v1ThriftBatchToOCProto(zSpans []*zipkincore.Span) ([]consumerdata.TraceData, error) { ocSpansAndParsedAnnotations := make([]ocSpanAndParsedAnnotations, 0, len(zSpans)) for _, zSpan := range zSpans { ocSpan, parsedAnnotations := zipkinV1ThriftToOCSpan(zSpan) diff --git a/translator/trace/zipkin/zipkinv1_thrift_to_protospan_test.go b/translator/trace/zipkin/zipkinv1_thrift_to_protospan_test.go index 599bffe8ad9..530f22944f6 100644 --- a/translator/trace/zipkin/zipkinv1_thrift_to_protospan_test.go +++ b/translator/trace/zipkin/zipkinv1_thrift_to_protospan_test.go @@ -38,7 +38,7 @@ func TestZipkinThriftFallbackToLocalComponent(t *testing.T) { err = json.Unmarshal(blob, &ztSpans) require.NoError(t, err, "Failed to unmarshal json into zipkin v1 thrift") - reqs, err := V1ThriftBatchToOCProto(ztSpans) + reqs, err := v1ThriftBatchToOCProto(ztSpans) require.NoError(t, err, "Failed to translate zipkinv1 thrift to OC proto") require.Equal(t, 2, len(reqs), "Invalid trace service requests count") @@ -64,7 +64,7 @@ func TestV1ThriftToOCProto(t *testing.T) { err = json.Unmarshal(blob, &ztSpans) require.NoError(t, err, "Failed to unmarshal json into zipkin v1 thrift") - got, err := V1ThriftBatchToOCProto(ztSpans) + got, err := v1ThriftBatchToOCProto(ztSpans) require.NoError(t, err, "Failed to translate zipkinv1 thrift to OC proto") want := ocBatchesFromZipkinV1 @@ -83,7 +83,7 @@ func BenchmarkV1ThriftToOCProto(b *testing.B) { require.NoError(b, err, "Failed to unmarshal json into zipkin v1 thrift") for n := 0; n < b.N; n++ { - V1ThriftBatchToOCProto(ztSpans) + v1ThriftBatchToOCProto(ztSpans) } } @@ -406,7 +406,7 @@ func TestZipkinThriftAnnotationsToOCStatus(t *testing.T) { TraceID: 1, BinaryAnnotations: c.haveTags, }} - gb, err := V1ThriftBatchToOCProto(zSpans) + gb, err := v1ThriftBatchToOCProto(zSpans) if err != nil { t.Errorf("#%d: Unexpected error: %v", i, err) continue @@ -420,7 +420,7 @@ func TestZipkinThriftAnnotationsToOCStatus(t *testing.T) { func TestThriftHTTPToGRPCStatusCode(t *testing.T) { for i := int32(100); i <= 600; i++ { wantStatus := tracetranslator.OCStatusCodeFromHTTP(i) - gb, err := V1ThriftBatchToOCProto([]*zipkincore.Span{{ + gb, err := v1ThriftBatchToOCProto([]*zipkincore.Span{{ ID: 1, TraceID: 1, BinaryAnnotations: []*zipkincore.BinaryAnnotation{ diff --git a/translator/trace/zipkin/zipkinv1_thrift_to_traces.go b/translator/trace/zipkin/zipkinv1_thrift_to_traces.go index 65399c2b2e0..85f814bcf07 100644 --- a/translator/trace/zipkin/zipkinv1_thrift_to_traces.go +++ b/translator/trace/zipkin/zipkinv1_thrift_to_traces.go @@ -24,7 +24,7 @@ import ( func V1ThriftBatchToInternalTraces(zSpans []*zipkincore.Span) (pdata.Traces, error) { traces := pdata.NewTraces() - ocTraces, _ := V1ThriftBatchToOCProto(zSpans) + ocTraces, _ := v1ThriftBatchToOCProto(zSpans) for _, td := range ocTraces { tmp := internaldata.OCToTraceData(td) diff --git a/translator/trace/zipkin/zipkinv1_to_protospan.go b/translator/trace/zipkin/zipkinv1_to_protospan.go index 0aae16f2276..fa0d7926264 100644 --- a/translator/trace/zipkin/zipkinv1_to_protospan.go +++ b/translator/trace/zipkin/zipkinv1_to_protospan.go @@ -86,8 +86,8 @@ type binaryAnnotation struct { Endpoint *endpoint `json:"endpoint"` } -// V1JSONBatchToOCProto converts a JSON blob with a list of Zipkin v1 spans to OC Proto. -func V1JSONBatchToOCProto(blob []byte) ([]consumerdata.TraceData, error) { +// v1JSONBatchToOCProto converts a JSON blob with a list of Zipkin v1 spans to OC Proto. +func v1JSONBatchToOCProto(blob []byte) ([]consumerdata.TraceData, error) { var zSpans []*zipkinV1Span if err := json.Unmarshal(blob, &zSpans); err != nil { return nil, errors.WithMessage(err, msgZipkinV1JSONUnmarshalError) diff --git a/translator/trace/zipkin/zipkinv1_to_protospan_test.go b/translator/trace/zipkin/zipkinv1_to_protospan_test.go index d81ec4ab690..64e469a1000 100644 --- a/translator/trace/zipkin/zipkinv1_to_protospan_test.go +++ b/translator/trace/zipkin/zipkinv1_to_protospan_test.go @@ -145,7 +145,7 @@ func TestZipkinJSONFallbackToLocalComponent(t *testing.T) { blob, err := ioutil.ReadFile("./testdata/zipkin_v1_local_component.json") require.NoError(t, err, "Failed to load test data") - reqs, err := V1JSONBatchToOCProto(blob) + reqs, err := v1JSONBatchToOCProto(blob) require.NoError(t, err, "Failed to translate zipkinv1 to OC proto") require.Equal(t, 2, len(reqs), "Invalid trace service requests count") @@ -167,7 +167,7 @@ func TestSingleJSONV1BatchToOCProto(t *testing.T) { blob, err := ioutil.ReadFile("./testdata/zipkin_v1_single_batch.json") require.NoError(t, err, "Failed to load test data") - got, err := V1JSONBatchToOCProto(blob) + got, err := v1JSONBatchToOCProto(blob) require.NoError(t, err, "Failed to translate zipkinv1 to OC proto") want := ocBatchesFromZipkinV1 @@ -191,7 +191,7 @@ func TestMultipleJSONV1BatchesToOCProto(t *testing.T) { jsonBatch, err := json.Marshal(batch) require.NoError(t, err, "Failed to marshal interface back to blob") - g, err := V1JSONBatchToOCProto(jsonBatch) + g, err := v1JSONBatchToOCProto(jsonBatch) require.NoError(t, err, "Failed to translate zipkinv1 to OC proto") // Coalesce the nodes otherwise they will differ due to multiple @@ -502,7 +502,7 @@ func TestZipkinAnnotationsToOCStatus(t *testing.T) { t.Errorf("#%d: Unexpected error: %v", i, err) return } - gb, err := V1JSONBatchToOCProto(zBytes) + gb, err := v1JSONBatchToOCProto(zBytes) if err != nil { t.Errorf("#%d: Unexpected error: %v", i, err) return @@ -532,7 +532,7 @@ func TestSpanWithoutTimestampGetsTag(t *testing.T) { testStart := time.Now() - gb, err := V1JSONBatchToOCProto(zBytes) + gb, err := v1JSONBatchToOCProto(zBytes) if err != nil { t.Errorf("Unexpected error: %v", err) return @@ -576,7 +576,7 @@ func TestJSONHTTPToGRPCStatusCode(t *testing.T) { t.Errorf("#%d: Unexpected error: %v", i, err) continue } - gb, err := V1JSONBatchToOCProto(zBytes) + gb, err := v1JSONBatchToOCProto(zBytes) if err != nil { t.Errorf("#%d: Unexpected error: %v", i, err) continue @@ -766,11 +766,6 @@ func TestSpanKindTranslation(t *testing.T) { } assert.EqualValues(t, expected, ocSpan.Attributes.AttributeMap[tracetranslator.TagSpanKind]) } - - // Translate to Zipkin V2 (which is used for internal representation by Zipkin exporter). - zSpanTranslated, err := OCSpanProtoToZipkin(nil, nil, ocSpan, "") - assert.NoError(t, err) - assert.EqualValues(t, test.zipkinV2Kind, zSpanTranslated.Kind) }) } } diff --git a/translator/trace/zipkin/zipkinv1_to_traces.go b/translator/trace/zipkin/zipkinv1_to_traces.go index c37b831f7e4..88ef5c18cd6 100644 --- a/translator/trace/zipkin/zipkinv1_to_traces.go +++ b/translator/trace/zipkin/zipkinv1_to_traces.go @@ -22,7 +22,7 @@ import ( func V1JSONBatchToInternalTraces(blob []byte) (pdata.Traces, error) { traces := pdata.NewTraces() - ocTraces, err := V1JSONBatchToOCProto(blob) + ocTraces, err := v1JSONBatchToOCProto(blob) if err != nil { return traces, err } diff --git a/translator/trace/zipkin/zipkinv2_to_traces_test.go b/translator/trace/zipkin/zipkinv2_to_traces_test.go index 9d9e943bf4a..2fadd732fe2 100644 --- a/translator/trace/zipkin/zipkinv2_to_traces_test.go +++ b/translator/trace/zipkin/zipkinv2_to_traces_test.go @@ -24,6 +24,7 @@ import ( "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/internal/data/testdata" "go.opentelemetry.io/collector/translator/conventions" + tracetranslator "go.opentelemetry.io/collector/translator/trace" ) func TestZipkinSpansToInternalTraces(t *testing.T) { @@ -131,3 +132,13 @@ func generateTraceSingleSpanMinmalResource() pdata.Traces { rsc.Attributes().UpsertString(conventions.AttributeServiceName, "SoleAttr") return td } + +func convertTraceID(t []byte) zipkinmodel.TraceID { + h, l, _ := tracetranslator.BytesToUInt64TraceID(t) + return zipkinmodel.TraceID{High: h, Low: l} +} + +func convertSpanID(s []byte) zipkinmodel.ID { + id, _ := tracetranslator.BytesToUInt64SpanID(s) + return zipkinmodel.ID(id) +}