@@ -3,7 +3,7 @@ title: Instrumenting HTTP server written in Go
33sort_rank : 3
44---
55
6- In this tutorial we will create a simple Go HTTP server and instrumentation it by adding a counter
6+ In this tutorial we will create a simple Go HTTP server and instrument it by adding a counter
77metric to keep count of the total number of requests processed by the server.
88
99Here we have a simple HTTP server with ` /ping ` endpoint which returns ` pong ` as response.
@@ -16,7 +16,7 @@ import (
1616 " net/http"
1717)
1818
19- func ping (w http .ResponseWriter , req *http .Request ){
19+ func ping (w http .ResponseWriter , req *http .Request ) {
2020 fmt.Fprintf (w," pong" )
2121}
2222
@@ -39,7 +39,7 @@ Now open `http://localhost:8090/ping` in your browser and you must see `pong`.
3939[ ![ Server] ( /assets/docs/tutorial/server.png )] ( /assets/docs/tutorial/server.png )
4040
4141
42- Now lets add a metric to the server which will instrument the number of requests made to the ping endpoint, the counter metric type is suitable for this as we know the request count doesn’t go down and only increases.
42+ Now let's add a metric to the server which will instrument the number of requests made to the ping endpoint. The counter metric type is suitable for this as we know the request count doesn’t go down and only increases.
4343
4444Create a Prometheus counter
4545
@@ -51,16 +51,16 @@ type metrics struct {
5151func newMetrics (reg prometheus .Registerer ) *metrics {
5252 m := &metrics{
5353 pingCounter: promauto.With (reg).NewCounter (
54- prometheus.CounterOpts {
54+ prometheus.CounterOpts {
5555 Name: " ping_request_count" ,
56- Help: " No of request handled by Ping handler" ,
56+ Help: " No of requests handled by Ping handler" ,
5757 }),
5858 }
5959 return m
6060}
6161```
6262
63- Next lets update the ping Handler to increase the count of the counter using ` metrics.pingCounter.Inc() ` .
63+ Next, let's update the ping Handler to increase the count of the counter using ` metrics.pingCounter.Inc() ` .
6464
6565``` go
6666func ping (m *metrics ) func (w http.ResponseWriter, req *http.Request) {
@@ -71,7 +71,7 @@ func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) {
7171}
7272```
7373
74- Then register the metrics (in this case only one counter) to a Prometheus Register and expose the metrics.
74+ Then register the metrics (in this case, only one counter) with a Prometheus registry and expose the metrics.
7575
7676``` go
7777func main () {
@@ -84,12 +84,11 @@ func main() {
8484}
8585```
8686
87- The ` prometheus.MustRegister ` function registers the pingCounter to the default Register .
88- To expose the metrics the Go Prometheus client library provides the promhttp package.
89- ` promhttp.Handler() ` provides a ` http.Handler ` which exposes the metrics registered in the Default Register .
87+ The ` prometheus.MustRegister ` function registers the pingCounter with the default registry .
88+ To expose the metrics, the Go Prometheus client library provides the promhttp package.
89+ ` promhttp.Handler() ` provides an ` http.Handler ` which exposes the metrics registered in the default registry .
9090
91- The sample code depends on the
92- The sample code depends on the
91+ The sample code is now:
9392
9493``` go
9594package main
@@ -142,13 +141,13 @@ go mod tidy
142141go run server.go
143142```
144143
145- Now hit the localhost:8090/ping endpoint a couple of times and sending a request to localhost:8090 will provide the metrics.
144+ Now hit the localhost:8090/ping endpoint a couple of times and then send a request to localhost:8090/metrics to see the metrics.
146145
147146[ ![ Ping Metric] ( /assets/docs/tutorial/ping_metric.png )] ( /assets/docs/tutorial/ping_metric.png )
148147
149- Here the ` ping_request_count ` shows that ` /ping ` endpoint was called 3 times.
148+ Here, the ` ping_request_count ` shows that the ` /ping ` endpoint was called 3 times.
150149
151- The Default Register comes with a collector for go runtime metrics and that is why we see other metrics like ` go_threads ` , ` go_goroutines ` etc.
150+ The default registry comes with a collector for Go runtime metrics, and that is why we see other metrics like ` go_threads ` , ` go_goroutines ` , etc.
152151
153152We have built our first metric exporter. Let’s update our Prometheus config to scrape the metrics from our server.
154153
0 commit comments