Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Add FormatStatsPath to ochttp Transport to reduce stats path cardinality #1229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion plugin/ochttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type Transport struct {
// name equals the URL Path.
FormatSpanName func(*http.Request) string

// FormatStatsPath holds the function to use for standardizing the path
// supplied to metrics. By default the path equals the URL path.
FormatStatsPath func(*http.Request) string

// 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.
Expand Down Expand Up @@ -95,7 +99,13 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
formatSpanName: spanNameFormatter,
newClientTrace: t.NewClientTrace,
}
rt = statsTransport{base: rt}

statsPathFormatter := t.FormatStatsPath
if statsPathFormatter == nil {
statsPathFormatter = statsPath
}

rt = statsTransport{base: rt, pathFormatter: statsPathFormatter}
return rt.RoundTrip(req)
}

Expand Down
12 changes: 9 additions & 3 deletions plugin/ochttp/client_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ import (

// statsTransport is an http.RoundTripper that collects stats for the outgoing requests.
type statsTransport struct {
base http.RoundTripper
base http.RoundTripper
pathFormatter func(*http.Request) string
}

// RoundTrip implements http.RoundTripper, delegating to Base and recording stats for the request.
func (t statsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx, _ := tag.New(req.Context(),
tag.Upsert(KeyClientHost, req.Host),
tag.Upsert(Host, req.Host),
tag.Upsert(KeyClientPath, req.URL.Path),
tag.Upsert(Path, req.URL.Path),
tag.Upsert(KeyClientPath, t.pathFormatter(req)),
tag.Upsert(Path, t.pathFormatter(req)),
tag.Upsert(KeyClientMethod, req.Method),
tag.Upsert(Method, req.Method))
req = req.WithContext(ctx)
Expand Down Expand Up @@ -141,3 +142,8 @@ func (t *tracker) Close() error {
t.end()
return t.body.Close()
}

// statsPath gets the path from a request url
func statsPath(req *http.Request) string {
return req.URL.Path
}
50 changes: 50 additions & 0 deletions plugin/ochttp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ochttp_test
import (
"log"
"net/http"
"regexp"

"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/plugin/ochttp/propagation/b3"
Expand Down Expand Up @@ -54,6 +55,55 @@ func ExampleTransport() {
_ = client
}

// FormatStatsPath allows paths to be standardized before the tag is assigned
//
// For example https://jsonplaceholder.typicode.com/posts/1 and https://jsonplaceholder.typicode.com/posts/2
// can have the path `/posts/:id` while https://jsonplaceholder.typicode.com/posts/1/comments has the path
// `/posts/:id/comments`
//
// Using FormatStatsPath can reduce cardinality when using KeyClientPath
func ExampleTransport_statsPathFormatter() {
// import (
// "go.opencensus.io/plugin/ochttp"
// "go.opencensus.io/stats/view"
// )

// Add KeyClientPath and KeyClientHost tags to a few views.
ochttp.ClientRoundtripLatencyDistribution.TagKeys = append(ochttp.ClientRoundtripLatencyDistribution.TagKeys, ochttp.KeyClientPath, ochttp.KeyClientHost)
ochttp.ClientCompletedCount.TagKeys = append(ochttp.ClientCompletedCount.TagKeys, ochttp.KeyClientPath, ochttp.KeyClientHost)

if err := view.Register(
// Register a few default views.
ochttp.ClientCompletedCount,
ochttp.ClientRoundtripLatencyDistribution,
); err != nil {
log.Fatal(err)
}

postRegexp := regexp.MustCompile(`^\/?posts\/[0-9]+\/?$`)
postCommentsRegexp := regexp.MustCompile(`^\/?posts\/[0-9]+\/comments\/?$`)

client := &http.Client{
Transport: &ochttp.Transport{
FormatStatsPath: func(req *http.Request) string {
if req.URL.Host == "jsonplaceholder.typicode.com" {
if postRegexp.MatchString(req.URL.Path) { // format the post endpoint
return "/posts/:id"
} else if postCommentsRegexp.MatchString(req.URL.Path) { // format the post comment endpoint
return "/posts/:id/comments"
}
}

// use the URL path as the default path
return req.URL.Path
},
},
}

// Use client to perform requests.
_ = client
}

var usersHandler http.Handler

func ExampleHandler() {
Expand Down