@@ -264,29 +264,34 @@ var validRangesPerCategory = map[int][]codeRange{
264264// SpanStatusFromHTTPStatusCode generates a status code and a message
265265// as specified by the OpenTelemetry specification for a span.
266266func SpanStatusFromHTTPStatusCode (code int ) (codes.Code , string ) {
267- spanCode , valid := func () (codes.Code , bool ) {
268- category := code / 100
269- ranges , ok := validRangesPerCategory [category ]
270- if ! ok {
271- return codes .Error , false
272- }
273- ok = false
274- for _ , crange := range ranges {
275- ok = crange .contains (code )
276- if ok {
277- break
278- }
279- }
280- if ! ok {
281- return codes .Error , false
282- }
283- if category > 0 && category < 4 {
284- return codes .Unset , true
285- }
286- return codes .Error , true
287- }()
267+ spanCode , valid := validateHTTPStatusCode (code )
288268 if ! valid {
289269 return spanCode , fmt .Sprintf ("Invalid HTTP status code %d" , code )
290270 }
291- return spanCode , fmt .Sprintf ("HTTP status code: %d" , code )
271+ return spanCode , ""
272+ }
273+
274+ // Validates the HTTP status code and returns corresponding span status code.
275+ // If the `code` is not a valid HTTP status code, returns span status Error
276+ // and false.
277+ func validateHTTPStatusCode (code int ) (codes.Code , bool ) {
278+ category := code / 100
279+ ranges , ok := validRangesPerCategory [category ]
280+ if ! ok {
281+ return codes .Error , false
282+ }
283+ ok = false
284+ for _ , crange := range ranges {
285+ ok = crange .contains (code )
286+ if ok {
287+ break
288+ }
289+ }
290+ if ! ok {
291+ return codes .Error , false
292+ }
293+ if category > 0 && category < 4 {
294+ return codes .Unset , true
295+ }
296+ return codes .Error , true
292297}
0 commit comments