@@ -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
179180var canonicalCodesMap = map [string ]int32 {
0 commit comments