-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib: refactor http request span tags (#1286)
Refactor the start and finish of http request spans into functions shared across contribs so that the expected set of http request span tags are properly implemented.
- Loading branch information
1 parent
70e9456
commit b2387df
Showing
13 changed files
with
174 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
// Package httptrace provides functionalities to trace HTTP requests that are commonly required and used across | ||
// contrib/** integrations. | ||
package httptrace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// StartRequestSpan starts an HTTP request span with the standard list of HTTP request span tags (http.method, http.url, | ||
// http.useragent). Any further span start option can be added with opts. | ||
func StartRequestSpan(r *http.Request, opts ...ddtrace.StartSpanOption) (tracer.Span, context.Context) { | ||
// Append our span options before the given ones so that the caller can "overwrite" them. | ||
opts = append([]ddtrace.StartSpanOption{ | ||
tracer.SpanType(ext.SpanTypeWeb), | ||
tracer.Tag(ext.HTTPMethod, r.Method), | ||
tracer.Tag(ext.HTTPURL, r.URL.Path), | ||
tracer.Tag(ext.HTTPUserAgent, r.UserAgent()), | ||
tracer.Measured(), | ||
}, opts...) | ||
if r.URL.Host != "" { | ||
opts = append([]ddtrace.StartSpanOption{ | ||
tracer.Tag("http.host", r.URL.Host), | ||
}, opts...) | ||
} | ||
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header)); err == nil { | ||
opts = append(opts, tracer.ChildOf(spanctx)) | ||
} | ||
return tracer.StartSpanFromContext(r.Context(), "http.request", opts...) | ||
} | ||
|
||
// FinishRequestSpan finishes the given HTTP request span and sets the expected response-related tags such as the status | ||
// code. Any further span finish option can be added with opts. | ||
func FinishRequestSpan(s tracer.Span, status int, opts ...tracer.FinishOption) { | ||
var statusStr string | ||
if status == 0 { | ||
statusStr = "200" | ||
} else { | ||
statusStr = strconv.Itoa(status) | ||
} | ||
s.SetTag(ext.HTTPCode, statusStr) | ||
if status >= 500 && status < 600 { | ||
s.SetTag(ext.Error, fmt.Errorf("%s: %s", statusStr, http.StatusText(status))) | ||
} | ||
s.Finish(opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.