Skip to content

Commit ea5ba4f

Browse files
Prioritize valid oc status code in error over http
1 parent 8d07b93 commit ea5ba4f

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

translator/trace/zipkin/status_code.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type statusMapper struct {
3939
fromHTTP status
4040
// oc status code extracted from "error" tags
4141
fromErrorTag status
42+
// oc status code 'unknown' when the "error" tag exists but is invalid
43+
fromErrorTagUnknown status
4244
}
4345

4446
// ocStatus returns an OC status from the best possible extraction source.
@@ -53,25 +55,17 @@ func (m *statusMapper) ocStatus() *tracepb.Status {
5355
s = m.fromCensus
5456
case m.fromStatus.codePtr != nil:
5557
s = m.fromStatus
56-
default:
57-
s = m.fromHTTP
58-
}
59-
60-
// If no codePtr was provided, fallback to the first source with a message
61-
// and use the error tag code
62-
if s.codePtr == nil {
63-
switch {
64-
case m.fromCensus.message != "":
65-
s = m.fromCensus
66-
case m.fromStatus.message != "":
67-
s = m.fromStatus
68-
default:
69-
s = m.fromHTTP
70-
}
71-
72-
if s.codePtr == nil {
73-
s.codePtr = m.fromErrorTag.codePtr
58+
case m.fromErrorTag.codePtr != nil:
59+
s = m.fromErrorTag
60+
if m.fromCensus.message != "" {
61+
s.message = m.fromCensus.message
62+
} else if m.fromStatus.message != "" {
63+
s.message = m.fromStatus.message
7464
}
65+
case m.fromHTTP.codePtr != nil:
66+
s = m.fromHTTP
67+
default:
68+
s = m.fromErrorTagUnknown
7569
}
7670

7771
if s.codePtr != nil {
@@ -122,7 +116,14 @@ func (m *statusMapper) fromAttribute(key string, attrib *tracepb.AttributeValue)
122116
m.fromHTTP.message = attrib.GetStringValue().GetValue()
123117

124118
case tracetranslator.TagError:
125-
m.fromErrorTag.codePtr = extractStatusFromError(attrib)
119+
code, ok := extractStatusFromError(attrib)
120+
if ok {
121+
m.fromErrorTag.codePtr = code
122+
return true
123+
} else {
124+
m.fromErrorTagUnknown.codePtr = code
125+
}
126+
126127
}
127128
return false
128129
}
@@ -154,7 +155,7 @@ func toInt32(i int) (int32, error) {
154155
return 0, fmt.Errorf("outside of the int32 range")
155156
}
156157

157-
func extractStatusFromError(attrib *tracepb.AttributeValue) *int32 {
158+
func extractStatusFromError(attrib *tracepb.AttributeValue) (*int32, bool) {
158159
// The status is stored with the "error" key
159160
// See https://github.com/census-instrumentation/opencensus-go/blob/1eb9a13c7dd02141e065a665f6bf5c99a090a16a/exporter/zipkin/zipkin.go#L160-L165
160161
var unknown int32 = 2
@@ -163,17 +164,17 @@ func extractStatusFromError(attrib *tracepb.AttributeValue) *int32 {
163164
case *tracepb.AttributeValue_StringValue:
164165
canonicalCodeStr := val.StringValue.GetValue()
165166
if canonicalCodeStr == "" {
166-
return nil
167+
return nil, true
167168
}
168169
code, set := canonicalCodesMap[canonicalCodeStr]
169170
if set {
170-
return &code
171+
return &code, true
171172
}
172173
default:
173174
break
174175
}
175176

176-
return &unknown
177+
return &unknown, false
177178
}
178179

179180
var canonicalCodesMap = map[string]int32{

translator/trace/zipkin/status_code_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,28 @@ func TestStatusCodeMapperCases(t *testing.T) {
196196
},
197197
},
198198

199+
{
200+
name: "error: valid oc status priority over http",
201+
expected: &tracepb.Status{Code: 4},
202+
attributes: map[string]string{
203+
"error": "DEADLINE_EXCEEDED",
204+
205+
"http.status_message": "a description",
206+
"http.status_code": "500",
207+
},
208+
},
209+
210+
{
211+
name: "error: invalid oc status uses http",
212+
expected: &tracepb.Status{Code: 13, Message: "a description"},
213+
attributes: map[string]string{
214+
"error": "123",
215+
216+
"http.status_message": "a description",
217+
"http.status_code": "500",
218+
},
219+
},
220+
199221
{
200222
name: "error only: string description",
201223
expected: &tracepb.Status{Code: 2},

0 commit comments

Comments
 (0)