Code Examples
package main
import (
"go.opencensus.io/plugin/ochttp"
"log"
"net/http"
)
var usersHandler http.Handler
func main() {
// import "go.opencensus.io/plugin/ochttp"
http.Handle("/users", ochttp.WithRouteTag(usersHandler, "/users"))
// If no handler is specified, the default mux is used.
log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{}))
}
package main
import (
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/plugin/ochttp/propagation/b3"
"log"
"net/http"
)
var usersHandler http.Handler
func main() {
// import "go.opencensus.io/plugin/ochttp"
mux := http.NewServeMux()
mux.Handle("/users", ochttp.WithRouteTag(usersHandler, "/users"))
log.Fatal(http.ListenAndServe("localhost:8080", &ochttp.Handler{
Handler: mux,
Propagation: &b3.HTTPFormat{},
}))
}
package main
import (
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"log"
"net/http"
)
func main() {
// import (
// "go.opencensus.io/plugin/ochttp"
// "go.opencensus.io/stats/view"
// )
if err := view.Register(
// Register a few default views.
ochttp.ClientSentBytesDistribution,
ochttp.ClientReceivedBytesDistribution,
ochttp.ClientRoundtripLatencyDistribution,
// Register a custom view.
&view.View{
Name: "httpclient_latency_by_path",
TagKeys: []tag.Key{ochttp.KeyClientPath},
Measure: ochttp.ClientRoundtripLatency,
Aggregation: ochttp.DefaultLatencyDistribution,
},
); err != nil {
log.Fatal(err)
}
client := &http.Client{
Transport: &ochttp.Transport{},
}
// Use client to perform requests.
_ = client
}
Package-Level Type Names (total 11, in which 2 are exported)
/* sort exporteds by: | */
Handler is an http.Handler wrapper to instrument your HTTP server with
OpenCensus. It supports both stats and tracing.
Tracing
This handler is aware of the incoming request's span, reading it from request
headers as configured using the Propagation field.
The extracted span can be accessed from the incoming request's
context.
span := trace.FromContext(r.Context())
The server span will be automatically ended at the end of ServeHTTP.
FormatSpanName holds the function to use for generating the span name
from the information found in the incoming HTTP Request. By default the
name equals the URL Path.
GetStartOptions allows to set start options per request. If set,
StartOptions is going to be ignored.
Handler is the handler used to handle the incoming request.
IsHealthEndpoint holds the function to use for determining if the
incoming HTTP request should be considered a health check. This is in
addition to the private isHealthEndpoint func which may also indicate
tracing should be skipped.
IsPublicEndpoint should be set to true for publicly accessible HTTP(S)
servers. If true, any trace metadata set on the incoming request will
be added as a linked trace instead of being added as a parent of the
current trace.
Propagation defines how traces are propagated. If unspecified,
B3 propagation will be used.
StartOptions are applied to the span started by this Handler around each
request.
StartOptions.SpanKind will always be set to trace.SpanKindServer
for spans started by this transport.
(*T) ServeHTTP(w http.ResponseWriter, r *http.Request)(*T) extractSpanContext(r *http.Request) (trace.SpanContext, bool)(*T) startStats(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, func(tags *addedTags))(*T) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func())
*T : net/http.Handler
Transport is an http.RoundTripper that instruments all outgoing requests with
OpenCensus stats and tracing.
The zero value is intended to be a useful default, but for
now it's recommended that you explicitly set Propagation, since the default
for this may change.
Base may be set to wrap another http.RoundTripper that does the actual
requests. By default http.DefaultTransport is used.
If base HTTP roundtripper implements CancelRequest,
the returned round tripper will be cancelable.
NameFromRequest holds the function to use for generating the span name
from the information found in the outgoing HTTP Request. By default the
name equals the URL Path.
GetStartOptions allows to set start options per request. If set,
StartOptions is going to be ignored.
NewClientTrace may be set to a function allowing the current *trace.Span
to be annotated with HTTP request event information emitted by the
httptrace package.
Propagation defines how traces are propagated. If unspecified, a default
(currently B3 format) will be used.
StartOptions are applied to the span started by this Transport around each
request.
StartOptions.SpanKind will always be set to trace.SpanKindClient
for spans started by this transport.
CancelRequest cancels an in-flight request by closing its connection.
RoundTrip implements http.RoundTripper, delegating to Base and recording stats and traces for the request.
(*T) base() http.RoundTripper
*T : net/http.RoundTripper
statsTransport is an http.RoundTripper that collects stats for the outgoing requests.
basehttp.RoundTripper
CancelRequest cancels an in-flight request by closing its connection.
RoundTrip implements http.RoundTripper, delegating to Base and recording stats for the request.
T : net/http.RoundTripper
taggedHandlerFunc is a http.Handler that returns tags describing the
processing of the request. These tags will be recorded along with the
measures in this package at the end of the request.
( T) ServeHTTP(w http.ResponseWriter, r *http.Request)
T : net/http.Handler
Package-Level Functions (total 10, in which 5 are exported)
NewSpanAnnotatingClientTrace returns a httptrace.ClientTrace which annotates
all emitted httptrace events on the provided Span.
NewSpanAnnotator returns a httptrace.ClientTrace which annotates
all emitted httptrace events on the provided Span.
Deprecated: Use NewSpanAnnotatingClientTrace instead
SetRoute sets the http_server_route tag to the given value.
It's useful when an HTTP framework does not support the http.Handler interface
and using WithRouteTag is not an option, but provides a way to hook into the request flow.
TraceStatus is a utility to convert the HTTP status code to a trace.Status that
represents the outcome as closely as possible.
WithRouteTag returns an http.Handler that records stats with the
http_server_route tag set to the given value.
wrappedBody returns a wrapped version of the original
Body and only implements the same combination of additional
interfaces as the original.
Package-Level Variables (total 42, in which 40 are exported)
Package ochttp provides some convenience views for client measures.
You still need to register these views for data to actually be collected.
Deprecated: Use ClientRoundtripLatency.
Deprecated: Use ClientRoundtripLatencyDistribution instead.
The following client HTTP measures are supported for use in custom views.
Package ochttp provides some convenience views for client measures.
You still need to register these views for data to actually be collected.
Deprecated: Use ClientSentBytes.
Deprecated: Use ClientSentBytesDistribution.
Deprecated: Use a Count aggregation over one of the other client measures to achieve the same effect.
Deprecated: Use ClientCompletedCount instead.
Deprecated: No direct replacement, but see ClientCompletedCount.
Deprecated: Use ClientReceivedBytes.
Deprecated: Use ClientReceivedBytesDistribution instead.
Deprecated: Use ClientCompletedCount instead.
The following client HTTP measures are supported for use in custom views.
Package ochttp provides some convenience views for client measures.
You still need to register these views for data to actually be collected.
The following client HTTP measures are supported for use in custom views.
Package ochttp provides some convenience views for client measures.
You still need to register these views for data to actually be collected.
DefaultClientViews are the default client views provided by this package.
Deprecated: No replacement. Register the views you would like individually.
Default distributions used by views in this package.
DefaultServerViews are the default server views provided by this package.
Deprecated: No replacement. Register the views you would like individually.
Default distributions used by views in this package.
Host is the value of the HTTP Host header.
The value of this tag can be controlled by the HTTP client, so you need
to watch out for potentially generating high-cardinality labels in your
metrics backend if you use this tag in views.
KeyClientHost is the value of the request Host header.
KeyClientMethod is the HTTP method, capitalized (i.e. GET, POST, PUT, DELETE, etc.).
KeyClientPath is the URL path (not including query string).
KeyClientStatus is the HTTP status code as an integer (e.g. 200, 404, 500.), or "error" if no response status line was received.
KeyServerRoute is a low cardinality string representing the logical
handler of the request. This is usually the pattern registered on the a
ServeMux (or similar string).
Method is the HTTP method of the request, capitalized (GET, POST, etc.).
Path is the URL path (not including query string) in the request.
The value of this tag can be controlled by the HTTP client, so you need
to watch out for potentially generating high-cardinality labels in your
metrics backend if you use this tag in views.
The following server HTTP measures are supported for use in custom views:
Package ochttp provides some convenience views for server measures.
You still need to register these views for data to actually be collected.
The following server HTTP measures are supported for use in custom views:
Package ochttp provides some convenience views for server measures.
You still need to register these views for data to actually be collected.
The following server HTTP measures are supported for use in custom views:
Package ochttp provides some convenience views for server measures.
You still need to register these views for data to actually be collected.
Package ochttp provides some convenience views for server measures.
You still need to register these views for data to actually be collected.
The following server HTTP measures are supported for use in custom views:
Package ochttp provides some convenience views for server measures.
You still need to register these views for data to actually be collected.
Package ochttp provides some convenience views for server measures.
You still need to register these views for data to actually be collected.
StatusCode is the numeric HTTP response status code,
or "error" if a transport error occurred and no status code was read.
Package-Level Constants (total 6, all are exported)
Attributes recorded on the span for the requests.
Only trace exporters will need them.
Attributes recorded on the span for the requests.
Only trace exporters will need them.
Attributes recorded on the span for the requests.
Only trace exporters will need them.
Attributes recorded on the span for the requests.
Only trace exporters will need them.
Attributes recorded on the span for the requests.
Only trace exporters will need them.
Attributes recorded on the span for the requests.
Only trace exporters will need them.
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.