forked from prometheus/client_golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request prometheus#6 from prometheus/feature/user-love/htt…
…p-mux-wrapper Add HTTP multiplexor wrapper for automatic telemetry.
- Loading branch information
Showing
11 changed files
with
342 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ _cgo_export.* | |
_testmain.go | ||
|
||
*.exe | ||
|
||
*~ | ||
*# |
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,39 @@ | ||
# Copyright 2013 Prometheus Team | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
MAKE_ARTIFACTS = search_index | ||
|
||
all: test | ||
|
||
build: | ||
go build ./... | ||
|
||
test: build | ||
go test ./... $(GO_TEST_FLAGS) | ||
|
||
format: | ||
find . -iname '*.go' -exec gofmt -w -s=true '{}' ';' | ||
|
||
advice: | ||
go tool vet . | ||
|
||
search_index: | ||
godoc -index -write_index -index_files='search_index' | ||
|
||
documentation: search_index | ||
godoc -http=:6060 -index -index_files='search_index' | ||
|
||
clean: | ||
rm -f $(MAKE_ARTIFACTS) | ||
|
||
.PHONY: advice build clean documentation format test |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
// Copyright (c) 2012, Matt T. Proud | ||
// All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// This skeletal example of the telemetry library is provided to demonstrate the | ||
// use of boilerplate HTTP delegation telemetry methods. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"github.com/prometheus/client_golang" | ||
"github.com/prometheus/client_golang/exp" | ||
"net/http" | ||
) | ||
|
||
// helloHandler demonstrates the DefaultCoarseMux's ability to sniff a | ||
// http.ResponseWriter (specifically http.response) implicit setting of | ||
// a response code. | ||
func helloHandler(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("Hello, hello, hello...")) | ||
} | ||
|
||
// goodbyeHandler demonstrates the DefaultCoarseMux's ability to sniff an | ||
// http.ResponseWriter (specifically http.response) explicit setting of | ||
// a response code. | ||
func goodbyeHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusGone) | ||
w.Write([]byte("... and now for the big goodbye!")) | ||
} | ||
|
||
// teapotHandler demonstrates the DefaultCoarseMux's ability to sniff an | ||
// http.ResponseWriter (specifically http.response) explicit setting of | ||
// a response code for pure comedic value. | ||
func teapotHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusTeapot) | ||
w.Write([]byte("Short and stout...")) | ||
} | ||
|
||
var ( | ||
listeningAddress = flag.String("listeningAddress", ":8080", "The address to listen to requests on.") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
exp.HandleFunc("/hello", helloHandler) | ||
exp.HandleFunc("/goodbye", goodbyeHandler) | ||
exp.HandleFunc("/teapot", teapotHandler) | ||
exp.Handle(registry.ExpositionResource, registry.DefaultHandler) | ||
|
||
http.ListenAndServe(*listeningAddress, exp.DefaultCoarseMux) | ||
} |
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,111 @@ | ||
// Copyright (c) 2013, Matt T. Proud | ||
// All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
package exp | ||
|
||
import ( | ||
"fmt" | ||
"github.com/prometheus/client_golang" | ||
"github.com/prometheus/client_golang/metrics" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
handler = "handler" | ||
code = "code" | ||
method = "method" | ||
) | ||
|
||
type ( | ||
coarseMux struct { | ||
*http.ServeMux | ||
} | ||
|
||
handlerDelegator struct { | ||
delegate http.Handler | ||
pattern string | ||
} | ||
) | ||
|
||
var ( | ||
requestCounts = metrics.NewCounter() | ||
requestDuration = metrics.NewCounter() | ||
requestDurations = metrics.NewDefaultHistogram() | ||
requestBytes = metrics.NewCounter() | ||
responseBytes = metrics.NewCounter() | ||
|
||
// DefaultCoarseMux is a drop-in replacement for http.DefaultServeMux that | ||
// provides standardized telemetry for Go's standard HTTP handler registration | ||
// and dispatch API. | ||
// | ||
// The name is due to the coarse grouping of telemetry by (HTTP Method, HTTP Response Code, | ||
// and handler match pattern) triples. | ||
DefaultCoarseMux = newCoarseMux() | ||
) | ||
|
||
func (h handlerDelegator) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
start := time.Now() | ||
rwd := NewResponseWriterDelegator(w) | ||
|
||
defer func() { | ||
duration := float64(time.Since(start) / time.Microsecond) | ||
status := rwd.Status() | ||
labels := map[string]string{handler: h.pattern, code: status, method: strings.ToLower(r.Method)} | ||
requestCounts.Increment(labels) | ||
requestDuration.IncrementBy(labels, duration) | ||
requestDurations.Add(labels, duration) | ||
requestBytes.IncrementBy(labels, float64(computeApproximateRequestSize(*r))) | ||
responseBytes.IncrementBy(labels, float64(rwd.BytesWritten)) | ||
}() | ||
|
||
h.delegate.ServeHTTP(rwd, r) | ||
} | ||
|
||
func (h handlerDelegator) String() string { | ||
return fmt.Sprintf("handlerDelegator wrapping %s for %s", h.delegate, h.pattern) | ||
} | ||
|
||
// Handle registers a http.Handler to this CoarseMux. See http.ServeMux.Handle. | ||
func (m *coarseMux) handle(pattern string, handler http.Handler) { | ||
m.ServeMux.Handle(pattern, handlerDelegator{ | ||
delegate: handler, | ||
pattern: pattern, | ||
}) | ||
} | ||
|
||
// Handle registers a handler to this CoarseMux. See http.ServeMux.HandleFunc. | ||
func (m *coarseMux) handleFunc(pattern string, handler http.HandlerFunc) { | ||
m.ServeMux.Handle(pattern, handlerDelegator{ | ||
delegate: handler, | ||
pattern: pattern, | ||
}) | ||
} | ||
|
||
func newCoarseMux() *coarseMux { | ||
return &coarseMux{ | ||
ServeMux: http.NewServeMux(), | ||
} | ||
} | ||
|
||
// Handle registers a http.Handler to DefaultCoarseMux. See http.Handle. | ||
func Handle(pattern string, handler http.Handler) { | ||
DefaultCoarseMux.handle(pattern, handler) | ||
} | ||
|
||
// HandleFunc registers a handler to DefaultCoarseMux. See http.HandleFunc. | ||
func HandleFunc(pattern string, handler http.HandlerFunc) { | ||
DefaultCoarseMux.handleFunc(pattern, handler) | ||
} | ||
|
||
func init() { | ||
registry.Register("http_requests_total", "A counter of the total number of HTTP requests made against the default multiplexor.", registry.NilLabels, requestCounts) | ||
registry.Register("http_request_durations_total_microseconds", "The total amount of time the default multiplexor has spent answering HTTP requests (microseconds).", registry.NilLabels, requestDuration) | ||
registry.Register("http_request_durations_microseconds", "The amounts of time the default multiplexor has spent answering HTTP requests (microseconds).", registry.NilLabels, requestDurations) | ||
registry.Register("http_request_bytes_total", "The total volume of content body sizes received (bytes).", registry.NilLabels, requestBytes) | ||
registry.Register("http_response_bytes_total", "The total volume of response payloads emitted (bytes).", registry.NilLabels, responseBytes) | ||
} |
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,11 @@ | ||
// Copyright (c) 2013, Matt T. Proud | ||
// All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
// A repository of various immature Prometheus client components that may | ||
// assist in your use of the library. Items contained herein are regarded as | ||
// especially interface unstable and may change without warning. Upon | ||
// maturation, they should be migrated into a formal package for users. | ||
package exp |
Oops, something went wrong.