-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a handler for the metrics endpoint (#35)
* add a handler for the metrics endpoint * remove references to un-used metric
- Loading branch information
Showing
5 changed files
with
83 additions
and
0 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
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,58 @@ | ||
package metrics | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
var ( | ||
apiLatencySeconds *prometheus.HistogramVec | ||
) | ||
|
||
func init() { | ||
apiLatencySeconds = promauto.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: "bomservice", | ||
Subsystem: "api", | ||
Name: "latency_seconds", | ||
Help: "api latency measurements in seconds", | ||
// XXX: will need to tune these buckets once we understand common behaviors better | ||
// buckets between 25ms to 10 s | ||
Buckets: []float64{0.025, 0.05, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0}, | ||
}, []string{ | ||
"endpoint", | ||
"response_code", | ||
}, | ||
) | ||
} | ||
|
||
// ListenAndServeMetrics exposes prometheus metrics as /metrics on port 9090 | ||
func ListenAndServe() { | ||
endpoint := "0.0.0.0:9090" | ||
|
||
go func() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
|
||
server := &http.Server{ | ||
Addr: endpoint, | ||
ReadHeaderTimeout: 2 * time.Second, | ||
} | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
} | ||
|
||
// APICallEpilog observes the results and latency of an API call | ||
func APICallEpilog(start time.Time, endpoint string, responseCode int) { | ||
code := strconv.Itoa(responseCode) | ||
elapsed := time.Since(start).Seconds() | ||
apiLatencySeconds.WithLabelValues(endpoint, code).Observe(elapsed) | ||
} |
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