# HG changeset patch # User Christophe de Vienne <christophe@cdevienne.info> # Date 1607080656 -3600 # Fri Dec 04 12:17:36 2020 +0100 # Node ID 3c9dc46a1c49abeec6f72591aa522a8656c6803c # Parent 90fb58063fed3800fe3efe3aa102100547c2be1d prometheus: build metric names from the project name diff --git a/templates/server/initPrometheus.gotmpl b/templates/server/initPrometheus.gotmpl --- a/templates/server/initPrometheus.gotmpl +++ b/templates/server/initPrometheus.gotmpl @@ -4,6 +4,7 @@ {{ $package := .Package }} import ( + "github.com/prometheus/client_golang/prometheus" {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }} {{ end }} ) @@ -15,3 +16,18 @@ {{.ReceiverName}}.handlers[{{ printf "%q" (upper .Method) }}][{{ if eq .Path "/" }}""{{ else }}{{ printf "%q" (cleanPath .Path) }}{{ end }}]) {{end}} } + +func init() { + reg := prometheus.WrapRegistererWith(prometheus.Labels{ + "api_name": "{{ .Name }}", + "api_title": "{{ .Info.Title }}", + "api_version": "{{ .Info.Version }}", + }, prometheus.DefaultRegisterer) + + {{range .Operations}} + reg.MustRegister( + {{if ne .Package $package}}{{.Package}}.{{end}}{{ camelize .Name }}HandlerDuration, + {{if ne .Package $package}}{{.Package}}.{{end}}{{ camelize .Name }}HandlerInFlight, + ) + {{end}} +} diff --git a/templates/server/prometheus.gotmpl b/templates/server/prometheus.gotmpl --- a/templates/server/prometheus.gotmpl +++ b/templates/server/prometheus.gotmpl @@ -1,4 +1,4 @@ -// Code generated by go-swagger with a alpe-api template; DO NOT EDIT. +// Code generated by go-swagger with a go-orusapi template; DO NOT EDIT. // Copyright Orus.io team @@ -12,11 +12,14 @@ var ( {{ camelize .Name }}HandlerDuration = prometheus.NewSummaryVec( prometheus.SummaryOpts{ - Name: "alpe_api_handler", + Name: "orus_api_handler", Help: "The handlers duration and count.", - Objectives: map[float64]float64{0.9: 0.01, 0.95: 0.01, 0.99: 0.001}, + Objectives: map[float64]float64{0.9: 0.01, 0.95: 0.01, 0.99: 0.001}, ConstLabels: map[string]string{ "service": "{{.Name}}", +{{- if .Tags }} + "tag": "{{ .Tags }}", +{{- end }} }, }, []string{"code"}, @@ -24,10 +27,13 @@ {{ camelize .Name }}HandlerInFlight = prometheus.NewGauge( prometheus.GaugeOpts{ - Name: "alpe_api_handler_in_flights", + Name: "orus_api_handler_in_flignts", Help: "The number of handlers currently running.", ConstLabels: map[string]string{ "service": "{{.Name}}", +{{- if .Tags }} + "tag": "{{ .Tags }}", +{{- end }} }, }, ) @@ -40,10 +46,3 @@ ), ) } - -func init() { - prometheus.MustRegister( - {{ camelize .Name }}HandlerDuration, - {{ camelize .Name }}HandlerInFlight, - ) -}