Skip to content

Commit 8ff86dc

Browse files
committed
Improve handling of status code/message (#51)
Improved how translators handling mapping status codes and messages to and from OC translator/trace/README.md documents the new behaviour
1 parent f28c6b8 commit 8ff86dc

33 files changed

+2114
-76
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2222
github.com/jaegertracing/jaeger v1.9.0
2323
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024
24+
github.com/mitchellh/mapstructure v1.1.2 // indirect
2425
github.com/omnition/scribe-go v0.0.0-20190131012523-9e3c68f31124
2526
github.com/opentracing/opentracing-go v1.1.0 // indirect
2627
github.com/openzipkin/zipkin-go v0.1.6
@@ -37,6 +38,7 @@ require (
3738
github.com/spf13/cobra v0.0.3
3839
github.com/spf13/viper v1.2.1
3940
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
41+
github.com/stretchr/objx v0.1.1 // indirect
4042
github.com/stretchr/testify v1.3.0
4143
github.com/uber-go/atomic v1.3.2 // indirect
4244
github.com/uber/jaeger-client-go v2.16.0+incompatible // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdI
216216
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
217217
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
218218
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
219+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
220+
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
219221
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
220222
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
221223
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -323,6 +325,8 @@ github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6
323325
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
324326
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
325327
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
328+
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
329+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
326330
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
327331
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
328332
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

processor/tracesamplerprocessor/tracesamplerprocessor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

processor/tracesamplerprocessor/tracesamplerprocessor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/jaegerreceiver/jaeger_agent_test.go

-12
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,6 @@ func testJaegerAgent(t *testing.T, agentEndpoint string, receiverConfig *Configu
171171
},
172172
Attributes: &tracepb.Span_Attributes{
173173
AttributeMap: map[string]*tracepb.AttributeValue{
174-
"status.code": {
175-
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeNotFound},
176-
},
177-
"status.message": {
178-
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Stale indices"}},
179-
},
180174
"error": {
181175
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
182176
},
@@ -204,12 +198,6 @@ func testJaegerAgent(t *testing.T, agentEndpoint string, receiverConfig *Configu
204198
},
205199
Attributes: &tracepb.Span_Attributes{
206200
AttributeMap: map[string]*tracepb.AttributeValue{
207-
"status.code": {
208-
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeInternal},
209-
},
210-
"status.message": {
211-
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Frontend crash"}},
212-
},
213201
"error": {
214202
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
215203
},

receiver/jaegerreceiver/trace_receiver_test.go

-12
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ func TestReception(t *testing.T) {
154154
},
155155
Attributes: &tracepb.Span_Attributes{
156156
AttributeMap: map[string]*tracepb.AttributeValue{
157-
"status.code": {
158-
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeNotFound},
159-
},
160-
"status.message": {
161-
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Stale indices"}},
162-
},
163157
"error": {
164158
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
165159
},
@@ -187,12 +181,6 @@ func TestReception(t *testing.T) {
187181
},
188182
Attributes: &tracepb.Span_Attributes{
189183
AttributeMap: map[string]*tracepb.AttributeValue{
190-
"status.code": {
191-
Value: &tracepb.AttributeValue_IntValue{IntValue: trace.StatusCodeInternal},
192-
},
193-
"status.message": {
194-
Value: &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: "Frontend crash"}},
195-
},
196184
"error": {
197185
Value: &tracepb.AttributeValue_BoolValue{BoolValue: true},
198186
},

receiver/prometheusreceiver/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/prometheusreceiver/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/prometheusreceiver/factory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/prometheusreceiver/factory_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/zipkinreceiver/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/zipkinreceiver/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/zipkinreceiver/factory.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

receiver/zipkinreceiver/factory_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, OpenCensus Authors
1+
// Copyright 2019, OpenTelemetry Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

translator/trace/README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Overview
2+
3+
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.
4+
5+
## Links:
6+
7+
* [OpenTracing Semantic Conventions](https://github.com/opentracing/specification/blob/master/semantic_conventions.md)
8+
9+
## Status Codes and Messages
10+
11+
### OpenCensus
12+
13+
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.
14+
15+
16+
### Jaeger to OC
17+
18+
Jaeger spans may contain two possible sets of tags that can possibly represent the status of an operation:
19+
20+
- `status.code` and `status.message`
21+
- `http.status_code` and `http.status_message`
22+
23+
When converting from Jaeger to OC,
24+
25+
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.
26+
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.
27+
3. If none of the tags are found, OC status should not be set.
28+
29+
30+
### Zipkin to OC
31+
32+
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:
33+
34+
- `census.status_code` and `census.status_description`
35+
- `status.code` and `status.message`
36+
- `http.status_code` and `http.status_message`
37+
38+
When converting from Zipkin to OC,
39+
40+
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.
41+
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.
42+
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.
43+
4. If none of the tags are found, OC status should not be set.
44+
45+
46+
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.
47+
48+
49+
### OC to Jaeger
50+
51+
When converting from OC to Jaeger, if the OC span has a status field set, then
52+
53+
* `code` should be added as a `status.code` tag.
54+
* `message` should be added as a `status.message` tag.
55+
56+
### OC to Zipkin
57+
58+
When converting from OC to Zipkin, if the OC span has the status field set, then
59+
60+
* `code` should be added as a `census.status_code` tag.
61+
* `message` should be added as a `census.status_description` tag.
62+
63+
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.
64+
65+
### Note:
66+
67+
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.
68+
69+
70+
## Converting HTTP status codes to OC codes
71+
72+
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
73+
74+
This is implemented by the `tracetranslator` package as `HTTPToOCCodeMapper`.

translator/trace/grpc_http_mapper.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019, OpenTelemetry 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+
// https://github.com/googleapis/googleapis/blob/bee79fbe03254a35db125dc6d2f1e9b752b390fe/google/rpc/code.proto#L33-L186
18+
const (
19+
OCOK = 0
20+
OCCancelled = 1
21+
OCUnknown = 2
22+
OCInvalidArgument = 3
23+
OCDeadlineExceeded = 4
24+
OCNotFound = 5
25+
OCAlreadyExists = 6
26+
OCPermissionDenied = 7
27+
OCResourceExhausted = 8
28+
OCFailedPrecondition = 9
29+
OCAborted = 10
30+
OCOutOfRange = 11
31+
OCUnimplemented = 12
32+
OCInternal = 13
33+
OCUnavailable = 14
34+
OCDataLoss = 15
35+
OCUnauthenticated = 16
36+
)
37+
38+
var httpToOCCodeMap = map[int32]int32{
39+
400: OCInvalidArgument,
40+
401: OCUnauthenticated,
41+
403: OCPermissionDenied,
42+
404: OCNotFound,
43+
409: OCAborted,
44+
429: OCResourceExhausted,
45+
499: OCCancelled,
46+
500: OCInternal,
47+
501: OCUnimplemented,
48+
503: OCUnavailable,
49+
504: OCDeadlineExceeded,
50+
}
51+
52+
// OCStatusCodeFromHTTP takes an HTTP status code and return the appropriate OC status code
53+
func OCStatusCodeFromHTTP(code int32) int32 {
54+
if code >= 200 && code < 300 {
55+
return OCOK
56+
}
57+
if code, ok := httpToOCCodeMap[code]; ok {
58+
return code
59+
}
60+
return OCUnknown
61+
}

translator/trace/jaeger/jaegerthrift_to_protospan.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ func jtagsToAttributes(tags []*jaeger.Tag) (string, tracepb.Span_SpanKind, *trac
190190

191191
var statusCodePtr *int32
192192
var statusMessage string
193+
var httpStatusCodePtr *int32
194+
var httpStatusMessage string
193195
var message string
194196

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

209-
case "http.status_code", "status.code": // It is expected to be an int
210-
statusCodePtr = new(int32)
211-
*statusCodePtr = int32(tag.GetVLong())
211+
case tracetranslator.TagStatusCode:
212+
statusCodePtr = statusCodeFromTag(tag)
213+
continue
212214

213-
case "http.status_message", "status.message":
215+
case tracetranslator.TagStatusMsg:
214216
statusMessage = tag.GetVStr()
217+
continue
218+
219+
case tracetranslator.TagHTTPStatusCode:
220+
httpStatusCodePtr = statusCodeFromHTTPTag(tag)
221+
222+
case tracetranslator.TagHTTPStatusMsg:
223+
httpStatusMessage = tag.GetVStr()
215224

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

260+
if statusCodePtr == nil {
261+
statusCodePtr = httpStatusCodePtr
262+
statusMessage = httpStatusMessage
263+
}
264+
251265
var sStatus *tracepb.Status
252266
if statusCodePtr != nil || statusMessage != "" {
253267
statusCode := int32(0)

0 commit comments

Comments
 (0)