shipping: Take metrics as dependencies
Marcus Olsson
7 years ago
2 | 2 | import ( |
3 | 3 | "time" |
4 | 4 | |
5 | stdprometheus "github.com/prometheus/client_golang/prometheus" | |
6 | ||
7 | 5 | "github.com/go-kit/kit/metrics" |
8 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" | |
9 | 6 | |
10 | 7 | "github.com/go-kit/kit/examples/shipping/cargo" |
11 | 8 | "github.com/go-kit/kit/examples/shipping/location" |
18 | 15 | } |
19 | 16 | |
20 | 17 | // NewInstrumentingService returns an instance of an instrumenting Service. |
21 | func NewInstrumentingService(s Service) Service { | |
22 | fieldKeys := []string{"method"} | |
23 | ||
24 | requestCount := kitprometheus.NewCounter(stdprometheus.CounterOpts{ | |
25 | Namespace: "api", | |
26 | Subsystem: "booking_service", | |
27 | Name: "request_count", | |
28 | Help: "Number of requests received.", | |
29 | }, fieldKeys) | |
30 | ||
31 | requestLatency := metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{ | |
32 | Namespace: "api", | |
33 | Subsystem: "booking_service", | |
34 | Name: "request_latency_microseconds", | |
35 | Help: "Total duration of requests in microseconds.", | |
36 | }, fieldKeys)) | |
37 | ||
18 | func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.TimeHistogram, s Service) Service { | |
38 | 19 | return &instrumentingService{ |
39 | 20 | requestCount: requestCount, |
40 | 21 | requestLatency: requestLatency, |
3 | 3 | "time" |
4 | 4 | |
5 | 5 | "github.com/go-kit/kit/metrics" |
6 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" | |
7 | stdprometheus "github.com/prometheus/client_golang/prometheus" | |
8 | 6 | |
9 | 7 | "github.com/go-kit/kit/examples/shipping/cargo" |
10 | 8 | "github.com/go-kit/kit/examples/shipping/location" |
18 | 16 | } |
19 | 17 | |
20 | 18 | // NewInstrumentingService returns an instance of an instrumenting Service. |
21 | func NewInstrumentingService(s Service) Service { | |
22 | fieldKeys := []string{"method"} | |
23 | ||
24 | requestCount := kitprometheus.NewCounter(stdprometheus.CounterOpts{ | |
25 | Namespace: "api", | |
26 | Subsystem: "handling_service", | |
27 | Name: "request_count", | |
28 | Help: "Number of requests received.", | |
29 | }, fieldKeys) | |
30 | ||
31 | requestLatency := metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{ | |
32 | Namespace: "api", | |
33 | Subsystem: "handling_service", | |
34 | Name: "request_latency_microseconds", | |
35 | Help: "Total duration of requests in microseconds.", | |
36 | }, fieldKeys)) | |
37 | ||
19 | func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.TimeHistogram, s Service) Service { | |
38 | 20 | return &instrumentingService{ |
39 | 21 | requestCount: requestCount, |
40 | 22 | requestLatency: requestLatency, |
9 | 9 | "syscall" |
10 | 10 | "time" |
11 | 11 | |
12 | "github.com/go-kit/kit/log" | |
13 | 12 | stdprometheus "github.com/prometheus/client_golang/prometheus" |
14 | 13 | "golang.org/x/net/context" |
14 | ||
15 | "github.com/go-kit/kit/log" | |
16 | "github.com/go-kit/kit/metrics" | |
17 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" | |
15 | 18 | |
16 | 19 | "github.com/go-kit/kit/examples/shipping/booking" |
17 | 20 | "github.com/go-kit/kit/examples/shipping/cargo" |
68 | 71 | // Facilitate testing by adding some cargos. |
69 | 72 | storeTestData(cargos) |
70 | 73 | |
74 | fieldKeys := []string{"method"} | |
75 | ||
71 | 76 | var rs routing.Service |
72 | 77 | rs = routing.NewProxyingMiddleware(*routingServiceURL, ctx)(rs) |
73 | 78 | |
74 | 79 | var bs booking.Service |
75 | 80 | bs = booking.NewService(cargos, locations, handlingEvents, rs) |
76 | 81 | bs = booking.NewLoggingService(log.NewContext(logger).With("component", "booking"), bs) |
77 | bs = booking.NewInstrumentingService(bs) | |
82 | bs = booking.NewInstrumentingService( | |
83 | kitprometheus.NewCounter(stdprometheus.CounterOpts{ | |
84 | Namespace: "api", | |
85 | Subsystem: "booking_service", | |
86 | Name: "request_count", | |
87 | Help: "Number of requests received.", | |
88 | }, fieldKeys), | |
89 | metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{ | |
90 | Namespace: "api", | |
91 | Subsystem: "booking_service", | |
92 | Name: "request_latency_microseconds", | |
93 | Help: "Total duration of requests in microseconds.", | |
94 | }, fieldKeys)), bs) | |
78 | 95 | |
79 | 96 | var ts tracking.Service |
80 | 97 | ts = tracking.NewService(cargos, handlingEvents) |
81 | 98 | ts = tracking.NewLoggingService(log.NewContext(logger).With("component", "tracking"), ts) |
82 | ts = tracking.NewInstrumentingService(ts) | |
99 | ts = tracking.NewInstrumentingService( | |
100 | kitprometheus.NewCounter(stdprometheus.CounterOpts{ | |
101 | Namespace: "api", | |
102 | Subsystem: "tracking_service", | |
103 | Name: "request_count", | |
104 | Help: "Number of requests received.", | |
105 | }, fieldKeys), | |
106 | metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{ | |
107 | Namespace: "api", | |
108 | Subsystem: "tracking_service", | |
109 | Name: "request_latency_microseconds", | |
110 | Help: "Total duration of requests in microseconds.", | |
111 | }, fieldKeys)), ts) | |
83 | 112 | |
84 | 113 | var hs handling.Service |
85 | 114 | hs = handling.NewService(handlingEvents, handlingEventFactory, handlingEventHandler) |
86 | 115 | hs = handling.NewLoggingService(log.NewContext(logger).With("component", "handling"), hs) |
87 | hs = handling.NewInstrumentingService(hs) | |
116 | hs = handling.NewInstrumentingService( | |
117 | kitprometheus.NewCounter(stdprometheus.CounterOpts{ | |
118 | Namespace: "api", | |
119 | Subsystem: "handling_service", | |
120 | Name: "request_count", | |
121 | Help: "Number of requests received.", | |
122 | }, fieldKeys), | |
123 | metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{ | |
124 | Namespace: "api", | |
125 | Subsystem: "handling_service", | |
126 | Name: "request_latency_microseconds", | |
127 | Help: "Total duration of requests in microseconds.", | |
128 | }, fieldKeys)), hs) | |
88 | 129 | |
89 | 130 | httpLogger := log.NewContext(logger).With("component", "http") |
90 | 131 |
2 | 2 | import ( |
3 | 3 | "time" |
4 | 4 | |
5 | stdprometheus "github.com/prometheus/client_golang/prometheus" | |
6 | ||
7 | 5 | "github.com/go-kit/kit/metrics" |
8 | kitprometheus "github.com/go-kit/kit/metrics/prometheus" | |
9 | 6 | ) |
10 | 7 | |
11 | 8 | type instrumentingService struct { |
15 | 12 | } |
16 | 13 | |
17 | 14 | // NewInstrumentingService returns an instance of an instrumenting Service. |
18 | func NewInstrumentingService(s Service) Service { | |
19 | fieldKeys := []string{"method"} | |
20 | ||
21 | requestCount := kitprometheus.NewCounter(stdprometheus.CounterOpts{ | |
22 | Namespace: "api", | |
23 | Subsystem: "tracking_service", | |
24 | Name: "request_count", | |
25 | Help: "Number of requests received.", | |
26 | }, fieldKeys) | |
27 | ||
28 | requestLatency := metrics.NewTimeHistogram(time.Microsecond, kitprometheus.NewSummary(stdprometheus.SummaryOpts{ | |
29 | Namespace: "api", | |
30 | Subsystem: "tracking_service", | |
31 | Name: "request_latency_microseconds", | |
32 | Help: "Total duration of requests in microseconds.", | |
33 | }, fieldKeys)) | |
34 | ||
15 | func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.TimeHistogram, s Service) Service { | |
35 | 16 | return &instrumentingService{ |
36 | 17 | requestCount: requestCount, |
37 | 18 | requestLatency: requestLatency, |