2017-03-31 19:01:58 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
stdprometheus "github.com/prometheus/client_golang/prometheus"
|
2017-05-18 19:54:23 +03:00
|
|
|
"golang.org/x/net/context"
|
2017-03-31 19:01:58 +03:00
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
|
|
|
|
httptransport "github.com/go-kit/kit/transport/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-05-18 19:54:23 +03:00
|
|
|
ctx := context.Background()
|
2017-03-31 19:01:58 +03:00
|
|
|
logger := log.NewLogfmtLogger(os.Stderr)
|
|
|
|
|
|
|
|
fieldKeys := []string{"method", "error"}
|
|
|
|
requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
|
|
|
|
Namespace: "my_group",
|
|
|
|
Subsystem: "string_service",
|
|
|
|
Name: "request_count",
|
|
|
|
Help: "Number of requests received.",
|
|
|
|
}, fieldKeys)
|
|
|
|
requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
|
|
Namespace: "my_group",
|
|
|
|
Subsystem: "string_service",
|
|
|
|
Name: "request_latency_microseconds",
|
|
|
|
Help: "Total duration of requests in microseconds.",
|
|
|
|
}, fieldKeys)
|
|
|
|
countResult := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
|
|
Namespace: "my_group",
|
|
|
|
Subsystem: "string_service",
|
|
|
|
Name: "count_result",
|
|
|
|
Help: "The result of each count method.",
|
|
|
|
}, []string{}) // no fields here
|
|
|
|
|
|
|
|
var svc StringService
|
|
|
|
svc = stringService{}
|
|
|
|
svc = loggingMiddleware{logger, svc}
|
|
|
|
svc = instrumentingMiddleware{requestCount, requestLatency, countResult, svc}
|
|
|
|
|
|
|
|
uppercaseHandler := httptransport.NewServer(
|
2017-05-18 19:54:23 +03:00
|
|
|
ctx,
|
2017-03-31 19:01:58 +03:00
|
|
|
makeUppercaseEndpoint(svc),
|
|
|
|
decodeUppercaseRequest,
|
|
|
|
encodeResponse,
|
|
|
|
)
|
|
|
|
|
|
|
|
countHandler := httptransport.NewServer(
|
2017-05-18 19:54:23 +03:00
|
|
|
ctx,
|
2017-03-31 19:01:58 +03:00
|
|
|
makeCountEndpoint(svc),
|
|
|
|
decodeCountRequest,
|
|
|
|
encodeResponse,
|
|
|
|
)
|
|
|
|
|
|
|
|
http.Handle("/uppercase", uppercaseHandler)
|
|
|
|
http.Handle("/count", countHandler)
|
|
|
|
http.Handle("/metrics", stdprometheus.Handler())
|
|
|
|
logger.Log("msg", "HTTP", "addr", ":8080")
|
|
|
|
logger.Log("err", http.ListenAndServe(":8080", nil))
|
|
|
|
}
|