-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from signalfx/translator
moving translator from signalfx/sapm-proto to golib to avoid circular
- Loading branch information
Showing
11 changed files
with
1,227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2019 Splunk, Inc. | ||
// | ||
// 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 translator | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
jaegerpb "github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
type bucketID [32]byte | ||
|
||
// spanBatcher is simpler version of OpenTelemetry's Node Batcher. | ||
// spanBatcher takes spans and groups them into Jaeger Batches using | ||
// the Span Process objects. | ||
type spanBatcher struct { | ||
buckets map[bucketID]*jaegerpb.Batch | ||
} | ||
|
||
func (b *spanBatcher) add(span *jaegerpb.Span) { | ||
if b.buckets == nil { | ||
b.buckets = make(map[bucketID]*jaegerpb.Batch) | ||
} | ||
|
||
batchByProcess := span.Process | ||
id, err := b.genBucketID(span.Process) | ||
if err != nil { | ||
batchByProcess = nil | ||
} | ||
|
||
batch := b.getOrAddBatch(id, batchByProcess) | ||
if batch.Process != nil { | ||
span.Process = nil | ||
} | ||
batch.Spans = append(batch.Spans, span) | ||
} | ||
|
||
func (b *spanBatcher) batches() []*jaegerpb.Batch { | ||
batches := make([]*jaegerpb.Batch, 0, len(b.buckets)) | ||
for _, b := range b.buckets { | ||
batches = append(batches, b) | ||
} | ||
return batches | ||
} | ||
|
||
func (b *spanBatcher) genBucketID(process *jaegerpb.Process) (bucketID, error) { | ||
if process != nil { | ||
sortTags(process.Tags) | ||
key, err := proto.Marshal(process) | ||
if err != nil { | ||
return bucketID{}, fmt.Errorf("error generating bucket ID: %s", err.Error()) | ||
} | ||
return sha256.Sum256(key), nil | ||
} | ||
return bucketID{}, nil | ||
} | ||
|
||
func (b *spanBatcher) getOrAddBatch(id bucketID, p *jaegerpb.Process) *jaegerpb.Batch { | ||
batch, ok := b.buckets[id] | ||
if !ok { | ||
batch = &jaegerpb.Batch{ | ||
Process: p, | ||
} | ||
b.buckets[id] = batch | ||
} | ||
return batch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2019 Splunk, Inc. | ||
// | ||
// 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 translator | ||
|
||
import ( | ||
"testing" | ||
|
||
jaegerpb "github.com/jaegertracing/jaeger/model" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBatcher(t *testing.T) { | ||
p1 := &jaegerpb.Process{ | ||
ServiceName: "s1", | ||
Tags: []jaegerpb.KeyValue{ | ||
{ | ||
Key: "k1", | ||
VType: jaegerpb.ValueType_STRING, | ||
VStr: "v1", | ||
}, | ||
{ | ||
Key: "k2", | ||
VType: jaegerpb.ValueType_STRING, | ||
VStr: "v2", | ||
}, | ||
}, | ||
} | ||
p2 := &jaegerpb.Process{ | ||
ServiceName: "s1", | ||
Tags: []jaegerpb.KeyValue{ | ||
{ | ||
Key: "k2", | ||
VType: jaegerpb.ValueType_STRING, | ||
VStr: "v2", | ||
}, | ||
{ | ||
Key: "k1", | ||
VType: jaegerpb.ValueType_STRING, | ||
VStr: "v1", | ||
}, | ||
}, | ||
} | ||
p3 := &jaegerpb.Process{ServiceName: "s2"} | ||
p4 := &jaegerpb.Process{ServiceName: "s3"} | ||
|
||
spans := []*jaegerpb.Span{ | ||
{Process: p1, SpanID: jaegerpb.SpanID(1)}, | ||
{Process: p1, SpanID: jaegerpb.SpanID(2)}, | ||
{Process: p2, SpanID: jaegerpb.SpanID(3)}, | ||
{Process: p3, SpanID: jaegerpb.SpanID(4)}, | ||
{Process: p3, SpanID: jaegerpb.SpanID(5)}, | ||
{Process: p4, SpanID: jaegerpb.SpanID(6)}, | ||
{Process: p4, SpanID: jaegerpb.SpanID(7)}, | ||
{SpanID: jaegerpb.SpanID(8)}, | ||
{SpanID: jaegerpb.SpanID(9)}, | ||
} | ||
|
||
b := &spanBatcher{} | ||
for _, s := range spans { | ||
b.add(s) | ||
} | ||
|
||
batches := b.batches() | ||
assert.Len(t, batches, 4) | ||
|
||
b1 := findBatchWithProcessServiceName(batches, p1) | ||
assert.NotNil(t, b1) | ||
assert.Equal(t, b1.Process, p1) | ||
assertSpansAreEqual(t, b1.Spans, []*jaegerpb.Span{spans[0], spans[1], spans[2]}) | ||
|
||
b2 := findBatchWithProcessServiceName(batches, p2) | ||
assert.Equal(t, b1, b2) | ||
|
||
b3 := findBatchWithProcessServiceName(batches, p3) | ||
assert.NotNil(t, b3) | ||
assert.Equal(t, b3.Process, p3) | ||
assertSpansAreEqual(t, b3.Spans, []*jaegerpb.Span{spans[3], spans[4]}) | ||
|
||
b4 := findBatchWithProcessServiceName(batches, p4) | ||
assert.NotNil(t, b4) | ||
assert.Equal(t, b4.Process, p4) | ||
assertSpansAreEqual(t, b4.Spans, []*jaegerpb.Span{spans[5], spans[6]}) | ||
|
||
var nilProcess *jaegerpb.Process | ||
b5 := findBatchWithProcessServiceName(batches, nil) | ||
assert.NotNil(t, b5) | ||
assert.Equal(t, b5.Process, nilProcess) | ||
assertSpansAreEqual(t, b5.Spans, []*jaegerpb.Span{spans[7], spans[8]}) | ||
|
||
for _, s := range spans { | ||
assert.Nil(t, s.Process) | ||
} | ||
} | ||
|
||
func findBatchWithProcessServiceName(batches []*jaegerpb.Batch, p *jaegerpb.Process) *jaegerpb.Batch { | ||
for _, b := range batches { | ||
if p == nil { | ||
if b.Process == nil { | ||
return b | ||
} | ||
} else { | ||
if b.Process != nil && b.Process.ServiceName == p.ServiceName { | ||
return b | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2019 Splunk, Inc. | ||
// | ||
// 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 translator | ||
|
||
const ( | ||
peerHostIPv4 = "peer.ipv4" | ||
peerHostIPv6 = "peer.ipv6" | ||
peerPort = "peer.port" | ||
spanKind = "span.kind" | ||
spanKindRPCClient = "client" | ||
spanKindRPCServer = "server" | ||
spanKindProducer = "producer" | ||
spanKindConsumer = "consumer" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2019 Splunk, Inc. | ||
// | ||
// 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 translator | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// This code is taken from OpenTelemetry project in order to avoid adding the whole project as a dependency. | ||
|
||
// https://github.com/googleapis/googleapis/blob/bee79fbe03254a35db125dc6d2f1e9b752b390fe/google/rpc/code.proto#L33-L186 | ||
const ( | ||
OCOK = 0 | ||
OCCancelled = 1 | ||
OCUnknown = 2 | ||
OCInvalidArgument = 3 | ||
OCDeadlineExceeded = 4 | ||
OCNotFound = 5 | ||
OCAlreadyExists = 6 | ||
OCPermissionDenied = 7 | ||
OCResourceExhausted = 8 | ||
OCFailedPrecondition = 9 | ||
OCAborted = 10 | ||
OCOutOfRange = 11 | ||
OCUnimplemented = 12 | ||
OCInternal = 13 | ||
OCUnavailable = 14 | ||
OCDataLoss = 15 | ||
OCUnauthenticated = 16 | ||
) | ||
|
||
var httpToOCCodeMap = map[int32]int32{ | ||
401: OCUnauthenticated, | ||
403: OCPermissionDenied, | ||
404: OCNotFound, | ||
429: OCResourceExhausted, | ||
499: OCCancelled, | ||
501: OCUnimplemented, | ||
503: OCUnavailable, | ||
504: OCDeadlineExceeded, | ||
} | ||
|
||
// OCStatusCodeFromHTTP takes an HTTP status code and return the appropriate OpenTelemetry status code | ||
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-http.md | ||
func OCStatusCodeFromHTTP(code int32) int32 { | ||
if code >= 100 && code < 400 { | ||
return OCOK | ||
} | ||
if rvCode, ok := httpToOCCodeMap[code]; ok { | ||
return rvCode | ||
} | ||
if code >= 400 && code < 500 { | ||
return OCInvalidArgument | ||
} | ||
if code >= 500 && code < 600 { | ||
return OCInternal | ||
} | ||
return OCUnknown | ||
} | ||
|
||
var ocToHTTPCodeMap = map[int32]int32{ | ||
OCOK: http.StatusOK, | ||
OCCancelled: 499, | ||
OCUnknown: http.StatusInternalServerError, | ||
OCInvalidArgument: http.StatusBadRequest, | ||
OCDeadlineExceeded: http.StatusGatewayTimeout, | ||
OCNotFound: http.StatusNotFound, | ||
OCAlreadyExists: http.StatusConflict, | ||
OCPermissionDenied: http.StatusForbidden, | ||
OCResourceExhausted: http.StatusTooManyRequests, | ||
OCFailedPrecondition: http.StatusPreconditionFailed, | ||
OCAborted: http.StatusConflict, | ||
OCOutOfRange: http.StatusRequestedRangeNotSatisfiable, | ||
OCUnimplemented: http.StatusNotImplemented, | ||
OCInternal: http.StatusInternalServerError, | ||
OCUnavailable: http.StatusServiceUnavailable, | ||
OCDataLoss: http.StatusUnprocessableEntity, | ||
OCUnauthenticated: http.StatusUnauthorized, | ||
} | ||
|
||
// HTTPStatusCodeFromOCStatus takes an OpenTelemetry status code and return the appropriate HTTP status code | ||
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-http.md | ||
func HTTPStatusCodeFromOCStatus(code int32) int32 { | ||
if rvCode, ok := ocToHTTPCodeMap[code]; ok { | ||
return rvCode | ||
} | ||
return http.StatusInternalServerError | ||
} |
Oops, something went wrong.