]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/go.opencensus.io/plugin/ochttp/route.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / go.opencensus.io / plugin / ochttp / route.go
1 // Copyright 2018, OpenCensus Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ochttp
16
17 import (
18 "net/http"
19
20 "go.opencensus.io/tag"
21 )
22
23 // WithRouteTag returns an http.Handler that records stats with the
24 // http_server_route tag set to the given value.
25 func WithRouteTag(handler http.Handler, route string) http.Handler {
26 return taggedHandlerFunc(func(w http.ResponseWriter, r *http.Request) []tag.Mutator {
27 addRoute := []tag.Mutator{tag.Upsert(KeyServerRoute, route)}
28 ctx, _ := tag.New(r.Context(), addRoute...)
29 r = r.WithContext(ctx)
30 handler.ServeHTTP(w, r)
31 return addRoute
32 })
33 }
34
35 // taggedHandlerFunc is a http.Handler that returns tags describing the
36 // processing of the request. These tags will be recorded along with the
37 // measures in this package at the end of the request.
38 type taggedHandlerFunc func(w http.ResponseWriter, r *http.Request) []tag.Mutator
39
40 func (h taggedHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
41 tags := h(w, r)
42 if a, ok := r.Context().Value(addedTagsKey{}).(*addedTags); ok {
43 a.t = append(a.t, tags...)
44 }
45 }
46
47 type addedTagsKey struct{}
48
49 type addedTags struct {
50 t []tag.Mutator
51 }