Skip to content

Commit

Permalink
Improve handling of status code/message (#51)
Browse files Browse the repository at this point in the history
Improved how translators handling mapping status codes and messages to and from OC

translator/trace/README.md documents the new behaviour
  • Loading branch information
owais committed Jun 28, 2019
1 parent f28c6b8 commit 8ff86dc
Show file tree
Hide file tree
Showing 33 changed files with 2,114 additions and 76 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jaegertracing/jaeger v1.9.0
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/omnition/scribe-go v0.0.0-20190131012523-9e3c68f31124
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/openzipkin/zipkin-go v0.1.6
Expand All @@ -37,6 +38,7 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.2.1
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/stretchr/testify v1.3.0
github.com/uber-go/atomic v1.3.2 // indirect
github.com/uber/jaeger-client-go v2.16.0+incompatible // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdI
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -323,6 +325,8 @@ github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
2 changes: 1 addition & 1 deletion processor/tracesamplerprocessor/tracesamplerprocessor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
12 changes: 0 additions & 12 deletions receiver/jaegerreceiver/jaeger_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ func testJaegerAgent(t *testing.T, agentEndpoint string, receiverConfig *Configu
},
Attributes: &tracepb.Span_Attributes{
AttributeMap: map[string]*tracepb.AttributeValue{
"status.code": {
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeNotFound},
},
"status.message": {
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Stale indices"}},
},
"error": {
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
},
Expand Down Expand Up @@ -204,12 +198,6 @@ func testJaegerAgent(t *testing.T, agentEndpoint string, receiverConfig *Configu
},
Attributes: &tracepb.Span_Attributes{
AttributeMap: map[string]*tracepb.AttributeValue{
"status.code": {
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeInternal},
},
"status.message": {
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Frontend crash"}},
},
"error": {
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
},
Expand Down
12 changes: 0 additions & 12 deletions receiver/jaegerreceiver/trace_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ func TestReception(t *testing.T) {
},
Attributes: &tracepb.Span_Attributes{
AttributeMap: map[string]*tracepb.AttributeValue{
"status.code": {
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeNotFound},
},
"status.message": {
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Stale indices"}},
},
"error": {
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
},
Expand Down Expand Up @@ -187,12 +181,6 @@ func TestReception(t *testing.T) {
},
Attributes: &tracepb.Span_Attributes{
AttributeMap: map[string]*tracepb.AttributeValue{
"status.code": {
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeInternal},
},
"status.message": {
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Frontend crash"}},
},
"error": {
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
},
Expand Down
2 changes: 1 addition & 1 deletion receiver/prometheusreceiver/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/prometheusreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/prometheusreceiver/factory.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/prometheusreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/zipkinreceiver/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/zipkinreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/zipkinreceiver/factory.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion receiver/zipkinreceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, OpenCensus Authors
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
74 changes: 74 additions & 0 deletions translator/trace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Overview

This package implements a number of translators that help translate spans to and from OpenCensus format to a number of other supported formats such as Jaeger Proto, Jaeger Thrift, Zipkin Thrift, Zipkin JSON. This document mentions how certain non-obvious things should be handled.

## Links:

* [OpenTracing Semantic Conventions](https://github.com/opentracing/specification/blob/master/semantic_conventions.md)

## Status Codes and Messages

### OpenCensus

OpenCensus protocol has a special field to represent the status of an operation. The status field has two fields, an int32 field called `code` and a string field called `message`. When converting from other formats, status field must be set from the relevant tags/attributes of the source format. When converting from OC to other formats, the status field must be translated to appropriate tags/attributes of the target format.


### Jaeger to OC

Jaeger spans may contain two possible sets of tags that can possibly represent the status of an operation:

- `status.code` and `status.message`
- `http.status_code` and `http.status_message`

When converting from Jaeger to OC,

1. OC status should be set from `status.code` and `status.message` tags if `status.code` tag is found on the Jaeger span. Since OC already has a special status field, these tags (`status.code` and `status.message`) are redundant and should be dropped from resultant OC span.
2. If the `status.code` tag is not present, status should be set from `http.status_code` and `http.status_message` if the `http.status_code` tag is found. HTTP status code should be mapped to the appropriate gRPC status code before using it in OC status. These tags should be preserved and added to the resultant OC span as attributes.
3. If none of the tags are found, OC status should not be set.


### Zipkin to OC

In addition to the two sets of tags mentioned in the previous section, Zipkin spans can possibly contain a third set of tags to represent operation status resulting in the following sets of tags:

- `census.status_code` and `census.status_description`
- `status.code` and `status.message`
- `http.status_code` and `http.status_message`

When converting from Zipkin to OC,

1. OC status should be set from `census.status_code` and `census.status_description` if `census.status_code` tag is found on the Zipkin span. These tags should be dropped from the resultant OC span.
2. If the `census.status_code` tag is not found in step 1, OC status should be set from `status.code` and `status.message` tags if the `status.code` tag is present. The tags should be dropped from the resultant OC span.
3. If no tags are found in step 1 and 2, OC status should be set from `http.status_code` and `http.status_message` if either `http.status_code` tag is found. These tags should be preserved and added to the resultant OC span as attributes.
4. If none of the tags are found, OC status should not be set.


Note that codes and messages from different sets of tags should not be mixed to form the status field. For example, OC status should not contain code from `http.status_code` but message from `status.message` and vice-versa. Both fields must be set from the same set of tags even if it means leaving one of the two fields empty.


### OC to Jaeger

When converting from OC to Jaeger, if the OC span has a status field set, then

* `code` should be added as a `status.code` tag.
* `message` should be added as a `status.message` tag.

### OC to Zipkin

When converting from OC to Zipkin, if the OC span has the status field set, then

* `code` should be added as a `census.status_code` tag.
* `message` should be added as a `census.status_description` tag.

In addition to this, if the OC status field represents a non-OK status, then a tag with the key `error` and value `true` should be added to the Zipkin span if one is not already present.

### Note:

If either target tags (`status.*` or `census.status_*`) are already present on the span, then they should be preserved and not overwritten from the status field. This is extremely unlikely to happen within the collector because of how things are implemented but any other implementations should still follow this rule.


## Converting HTTP status codes to OC codes

The following guidelines should be followed for translating HTTP status codes to OC ones. https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto

This is implemented by the `tracetranslator` package as `HTTPToOCCodeMapper`.
61 changes: 61 additions & 0 deletions translator/trace/grpc_http_mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2019, 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 tracetranslator

// 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{
400: OCInvalidArgument,
401: OCUnauthenticated,
403: OCPermissionDenied,
404: OCNotFound,
409: OCAborted,
429: OCResourceExhausted,
499: OCCancelled,
500: OCInternal,
501: OCUnimplemented,
503: OCUnavailable,
504: OCDeadlineExceeded,
}

// OCStatusCodeFromHTTP takes an HTTP status code and return the appropriate OC status code
func OCStatusCodeFromHTTP(code int32) int32 {
if code >= 200 && code < 300 {
return OCOK
}
if code, ok := httpToOCCodeMap[code]; ok {
return code
}
return OCUnknown
}
22 changes: 18 additions & 4 deletions translator/trace/jaeger/jaegerthrift_to_protospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func jtagsToAttributes(tags []*jaeger.Tag) (string, tracepb.Span_SpanKind, *trac

var statusCodePtr *int32
var statusMessage string
var httpStatusCodePtr *int32
var httpStatusMessage string
var message string

sAttribs := make(map[string]*tracepb.AttributeValue)
Expand All @@ -206,12 +208,19 @@ func jtagsToAttributes(tags []*jaeger.Tag) (string, tracepb.Span_SpanKind, *trac
sKind = tracepb.Span_SERVER
}

case "http.status_code", "status.code": // It is expected to be an int
statusCodePtr = new(int32)
*statusCodePtr = int32(tag.GetVLong())
case tracetranslator.TagStatusCode:
statusCodePtr = statusCodeFromTag(tag)
continue

case "http.status_message", "status.message":
case tracetranslator.TagStatusMsg:
statusMessage = tag.GetVStr()
continue

case tracetranslator.TagHTTPStatusCode:
httpStatusCodePtr = statusCodeFromHTTPTag(tag)

case tracetranslator.TagHTTPStatusMsg:
httpStatusMessage = tag.GetVStr()

case "message":
message = tag.GetVStr()
Expand Down Expand Up @@ -248,6 +257,11 @@ func jtagsToAttributes(tags []*jaeger.Tag) (string, tracepb.Span_SpanKind, *trac
sAttribs[tag.Key] = attrib
}

if statusCodePtr == nil {
statusCodePtr = httpStatusCodePtr
statusMessage = httpStatusMessage
}

var sStatus *tracepb.Status
if statusCodePtr != nil || statusMessage != "" {
statusCode := int32(0)
Expand Down
Loading

0 comments on commit 8ff86dc

Please sign in to comment.