Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit 9480295

Browse files
author
Steven Karis
authored
Re-package tracetranslators by translation type (#418)
Re-package tracetranslators by translation type
1 parent 080c9e3 commit 9480295

31 files changed

Lines changed: 125 additions & 98 deletions

cmd/occollector/app/sender/jaeger_thrift_http_sender.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"go.uber.org/zap"
2727

2828
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
29-
"github.com/census-instrumentation/opencensus-service/translator/trace"
29+
jaegertranslator "github.com/census-instrumentation/opencensus-service/translator/trace/jaeger"
3030
)
3131

3232
// Default timeout for http request in seconds
@@ -86,7 +86,7 @@ func (s *JaegerThriftHTTPSender) ProcessSpans(batch *agenttracepb.ExportTraceSer
8686
return 0, fmt.Errorf("Jaeger sender received nil batch")
8787
}
8888

89-
tBatch, err := tracetranslator.OCProtoToJaegerThrift(batch)
89+
tBatch, err := jaegertranslator.OCProtoToJaegerThrift(batch)
9090
if err != nil {
9191
return uint64(len(batch.Spans)), err
9292
}

cmd/occollector/app/sender/jaeger_thrift_tchannel_sender.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
2323
"github.com/census-instrumentation/opencensus-service/internal/collector/processor"
24-
"github.com/census-instrumentation/opencensus-service/translator/trace"
24+
jaegertranslator "github.com/census-instrumentation/opencensus-service/translator/trace/jaeger"
2525
)
2626

2727
// JaegerThriftTChannelSender takes span batches and sends them
@@ -47,7 +47,7 @@ func NewJaegerThriftTChannelSender(
4747
// ProcessSpans sends the received data to the configured Jaeger Thrift end-point.
4848
func (s *JaegerThriftTChannelSender) ProcessSpans(batch *agenttracepb.ExportTraceServiceRequest, spanFormat string) (uint64, error) {
4949
// TODO: (@pjanotti) In case of failure the translation to Jaeger Thrift is going to be remade, cache it somehow.
50-
tBatch, err := tracetranslator.OCProtoToJaegerThrift(batch)
50+
tBatch, err := jaegertranslator.OCProtoToJaegerThrift(batch)
5151
if err != nil {
5252
return uint64(len(tBatch.Spans)), err
5353
}

exporter/exporterparser/exparser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
3030
"github.com/census-instrumentation/opencensus-service/data"
3131
"github.com/census-instrumentation/opencensus-service/internal"
32-
tracetranslator "github.com/census-instrumentation/opencensus-service/translator/trace"
32+
spandatatranslator "github.com/census-instrumentation/opencensus-service/translator/trace/spandata"
3333
)
3434

3535
// OcProtoSpansToOCSpanDataInstrumented converts
@@ -61,7 +61,7 @@ func OcProtoSpansToOCSpanDataInstrumented(ctx context.Context, exporterName stri
6161
var errs []error
6262
var goodSpans []*tracepb.Span
6363
for _, span := range td.Spans {
64-
sd, err := tracetranslator.ProtoSpanToOCSpanData(span)
64+
sd, err := spandatatranslator.ProtoSpanToOCSpanData(span)
6565
if err == nil {
6666
te.ExportSpan(sd)
6767
goodSpans = append(goodSpans, span)

exporter/zipkinexporter/zipkin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"github.com/census-instrumentation/opencensus-service/data"
3535
"github.com/census-instrumentation/opencensus-service/exporter"
3636
"github.com/census-instrumentation/opencensus-service/internal"
37-
tracetranslator "github.com/census-instrumentation/opencensus-service/translator/trace"
37+
spandatatranslator "github.com/census-instrumentation/opencensus-service/translator/trace/spandata"
3838
)
3939

4040
// ZipkinConfig holds the configuration of a Zipkin exporter.
@@ -206,7 +206,7 @@ func (ze *zipkinExporter) ExportSpans(ctx context.Context, td data.TraceData) (z
206206

207207
goodSpans := make([]*tracepb.Span, 0, len(td.Spans))
208208
for _, span := range td.Spans {
209-
sd, err := tracetranslator.ProtoSpanToOCSpanData(span)
209+
sd, err := spandatatranslator.ProtoSpanToOCSpanData(span)
210210
if err != nil {
211211
return err
212212
}

receiver/jaegerreceiver/trace_receiver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
"github.com/census-instrumentation/opencensus-service/data"
4141
"github.com/census-instrumentation/opencensus-service/internal"
4242
"github.com/census-instrumentation/opencensus-service/receiver"
43-
tracetranslator "github.com/census-instrumentation/opencensus-service/translator/trace"
43+
jaegertranslator "github.com/census-instrumentation/opencensus-service/translator/trace/jaeger"
4444
)
4545

4646
// Configuration defines the behavior and the ports that
@@ -230,7 +230,7 @@ func (jr *jReceiver) SubmitBatches(ctx thrift.Context, batches []*jaeger.Batch)
230230
spansMetricsFn := internal.NewReceivedSpansRecorderStreaming(ctx, "jaeger-collector")
231231

232232
for _, batch := range batches {
233-
octrace, err := tracetranslator.JaegerThriftBatchToOCProto(batch)
233+
octrace, err := jaegertranslator.ThriftBatchToOCProto(batch)
234234
// TODO: (@odeke-em) add this error for Jaeger observability
235235
ok := false
236236

@@ -260,7 +260,7 @@ func (jr *jReceiver) EmitZipkinBatch(spans []*zipkincore.Span) error {
260260
// EmitBatch implements cmd/agent/reporter.Reporter and it forwards
261261
// Jaeger spans received by the Jaeger agent processor.
262262
func (jr *jReceiver) EmitBatch(batch *jaeger.Batch) error {
263-
octrace, err := tracetranslator.JaegerThriftBatchToOCProto(batch)
263+
octrace, err := jaegertranslator.ThriftBatchToOCProto(batch)
264264
if err != nil {
265265
// TODO: (@odeke-em) add this error for Jaeger observability metrics
266266
return err

receiver/zipkinreceiver/scribe/scribe_receiver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/census-instrumentation/opencensus-service/data"
3030
"github.com/census-instrumentation/opencensus-service/internal"
3131
"github.com/census-instrumentation/opencensus-service/receiver"
32-
"github.com/census-instrumentation/opencensus-service/translator/trace"
32+
zipkintranslator "github.com/census-instrumentation/opencensus-service/translator/trace/zipkin"
3333
)
3434

3535
var (
@@ -153,7 +153,7 @@ func (sc *scribeCollector) Log(messages []*scribe.LogEntry) (r scribe.ResultCode
153153
return scribe.ResultCode_OK, nil
154154
}
155155

156-
ocBatches, err := tracetranslator.ZipkinV1ThriftBatchToOCProto(zSpans)
156+
ocBatches, err := zipkintranslator.V1ThriftBatchToOCProto(zSpans)
157157
if err != nil {
158158
return scribe.ResultCode_OK, err
159159
}

receiver/zipkinreceiver/trace_receiver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
"github.com/census-instrumentation/opencensus-service/data"
4343
"github.com/census-instrumentation/opencensus-service/internal"
4444
"github.com/census-instrumentation/opencensus-service/receiver"
45-
"github.com/census-instrumentation/opencensus-service/translator/trace"
45+
zipkintranslator "github.com/census-instrumentation/opencensus-service/translator/trace/zipkin"
4646
)
4747

4848
// ZipkinReceiver type is used to handle spans received in the Zipkin format.
@@ -120,9 +120,9 @@ func (zr *ZipkinReceiver) v1ToTraceSpans(blob []byte, hdr http.Header) (reqs []*
120120
return nil, err
121121
}
122122

123-
return tracetranslator.ZipkinV1ThriftBatchToOCProto(zSpans)
123+
return zipkintranslator.V1ThriftBatchToOCProto(zSpans)
124124
}
125-
return tracetranslator.ZipkinV1JSONBatchToOCProto(blob)
125+
return zipkintranslator.V1JSONBatchToOCProto(blob)
126126
}
127127

128128
// deserializeThrift decodes Thrift bytes to a list of spans.

receiver/zipkinreceiver/trace_receiver_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/census-instrumentation/opencensus-service/internal"
3939
"github.com/census-instrumentation/opencensus-service/internal/testutils"
4040
"github.com/census-instrumentation/opencensus-service/receiver"
41-
"github.com/census-instrumentation/opencensus-service/translator/trace"
41+
spandatatranslator "github.com/census-instrumentation/opencensus-service/translator/trace/spandata"
4242
)
4343

4444
func TestTraceIDConversion(t *testing.T) {
@@ -386,7 +386,7 @@ func TestConversionRoundtrip(t *testing.T) {
386386
reporterShutdownFns = append(reporterShutdownFns, re.Close)
387387

388388
for _, span := range treq.Spans {
389-
sd, _ := tracetranslator.ProtoSpanToOCSpanData(span)
389+
sd, _ := spandatatranslator.ProtoSpanToOCSpanData(span)
390390
ze.ExportSpan(sd)
391391
}
392392
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tracetranslator
16+
17+
import (
18+
"encoding/binary"
19+
)
20+
21+
// Int64TraceIDToByteTraceID takes a long representaition of a trace id
22+
// and converts it to a []byte representation.
23+
func Int64TraceIDToByteTraceID(high, low int64) []byte {
24+
if high == 0 && low == 0 {
25+
return nil
26+
}
27+
traceID := make([]byte, 16)
28+
binary.BigEndian.PutUint64(traceID[:8], uint64(high))
29+
binary.BigEndian.PutUint64(traceID[8:], uint64(low))
30+
return traceID
31+
}
32+
33+
// Int64SpanIDToByteSpanID takes a long represenation of a span id and
34+
// converts it to a []byte representation.
35+
func Int64SpanIDToByteSpanID(id int64) []byte {
36+
if id == 0 {
37+
return nil
38+
}
39+
spanID := make([]byte, 8)
40+
binary.BigEndian.PutUint64(spanID, uint64(id))
41+
return spanID
42+
}

translator/trace/jaegerthrift_to_protospan.go renamed to translator/trace/jaeger/jaegerthrift_to_protospan.go

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package tracetranslator
15+
package jaeger
1616

1717
import (
1818
"encoding/base64"
19-
"encoding/binary"
2019
"fmt"
2120
"reflect"
2221
"strconv"
2322
"time"
2423

25-
"github.com/census-instrumentation/opencensus-service/internal"
26-
2724
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
2825
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
2926
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
3027
"github.com/jaegertracing/jaeger/thrift-gen/jaeger"
28+
29+
"github.com/census-instrumentation/opencensus-service/internal"
30+
"github.com/census-instrumentation/opencensus-service/translator/trace"
3131
)
3232

33-
// JaegerThriftBatchToOCProto converts a single Jaeger Thrift batch of spans to a OC proto batch.
34-
func JaegerThriftBatchToOCProto(jbatch *jaeger.Batch) (*agenttracepb.ExportTraceServiceRequest, error) {
33+
// ThriftBatchToOCProto converts a single Jaeger Thrift batch of spans to a OC proto batch.
34+
func ThriftBatchToOCProto(jbatch *jaeger.Batch) (*agenttracepb.ExportTraceServiceRequest, error) {
3535
ocbatch := &agenttracepb.ExportTraceServiceRequest{
3636
Node: jProcessToOCProtoNode(jbatch.GetProcess()),
3737
Spans: jSpansToOCProtoSpans(jbatch.GetSpans()),
@@ -106,10 +106,10 @@ func jSpansToOCProtoSpans(jspans []*jaeger.Span) []*tracepb.Span {
106106
startTime := epochMicrosecondsAsTime(uint64(jspan.StartTime))
107107
_, sKind, sStatus, sAttributes := jtagsToAttributes(jspan.Tags)
108108
span := &tracepb.Span{
109-
TraceId: jTraceIDToOCProtoTraceID(jspan.TraceIdHigh, jspan.TraceIdLow),
110-
SpanId: jSpanIDToOCProtoSpanID(jspan.SpanId),
109+
TraceId: tracetranslator.Int64TraceIDToByteTraceID(jspan.TraceIdHigh, jspan.TraceIdLow),
110+
SpanId: tracetranslator.Int64SpanIDToByteSpanID(jspan.SpanId),
111111
// TODO: Tracestate: Check RFC status and if is applicable,
112-
ParentSpanId: jSpanIDToOCProtoSpanID(jspan.ParentSpanId),
112+
ParentSpanId: tracetranslator.Int64SpanIDToByteSpanID(jspan.ParentSpanId),
113113
Name: strToTruncatableString(jspan.OperationName),
114114
Kind: sKind,
115115
StartTime: internal.TimeToTimestamp(startTime),
@@ -171,8 +171,8 @@ func jReferencesToOCProtoLinks(jrefs []*jaeger.SpanRef) *tracepb.Span_Links {
171171
}
172172

173173
link := &tracepb.Span_Link{
174-
TraceId: jTraceIDToOCProtoTraceID(jref.TraceIdHigh, jref.TraceIdLow),
175-
SpanId: jSpanIDToOCProtoSpanID(jref.SpanId),
174+
TraceId: tracetranslator.Int64TraceIDToByteTraceID(jref.TraceIdHigh, jref.TraceIdLow),
175+
SpanId: tracetranslator.Int64SpanIDToByteSpanID(jref.SpanId),
176176
Type: linkType,
177177
}
178178
links = append(links, link)
@@ -181,25 +181,6 @@ func jReferencesToOCProtoLinks(jrefs []*jaeger.SpanRef) *tracepb.Span_Links {
181181
return &tracepb.Span_Links{Link: links}
182182
}
183183

184-
func jTraceIDToOCProtoTraceID(high, low int64) []byte {
185-
if high == 0 && low == 0 {
186-
return nil
187-
}
188-
traceID := make([]byte, 16)
189-
binary.BigEndian.PutUint64(traceID[:8], uint64(high))
190-
binary.BigEndian.PutUint64(traceID[8:], uint64(low))
191-
return traceID
192-
}
193-
194-
func jSpanIDToOCProtoSpanID(id int64) []byte {
195-
if id == 0 {
196-
return nil
197-
}
198-
spanID := make([]byte, 8)
199-
binary.BigEndian.PutUint64(spanID, uint64(id))
200-
return spanID
201-
}
202-
203184
func jtagsToAttributes(tags []*jaeger.Tag) (string, tracepb.Span_SpanKind, *tracepb.Status, *tracepb.Span_Attributes) {
204185
if tags == nil {
205186
return "", tracepb.Span_SPAN_KIND_UNSPECIFIED, nil, nil

0 commit comments

Comments
 (0)