diff --git a/server.yaml b/server.yaml index 5091da796767856577b5f919d404b9d183d98c80_c2VydmVyLnlhbWw=..7908dfbcac1f1960c82db6b7c8af2c72d2acab66_c2VydmVyLnlhbWw= 100644 --- a/server.yaml +++ b/server.yaml @@ -22,7 +22,11 @@ file_name: "doc.go" # custom templates + - name: logging + source: server/logging.gotmpl + target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" + file_name: "logging.go" - name: init_prometheus source: server/initPrometheus.gotmpl target: "{{ joinFilePath .Target .ServerPackage .Package }}" file_name: "prometheus.go" @@ -25,11 +29,7 @@ - name: init_prometheus source: server/initPrometheus.gotmpl target: "{{ joinFilePath .Target .ServerPackage .Package }}" file_name: "prometheus.go" - - name: logging - source: server/logging.gotmpl - target: "{{ if gt (len .Tags) 0 }}{{ joinFilePath .Target .ServerPackage .APIPackage .Package }}{{ else }}{{ joinFilePath .Target .ServerPackage .Package }}{{ end }}" - file_name: "logging.go" - name: main source: server/main.gotmpl @@ -103,3 +103,7 @@ file_name: "{{ (snakize (pascalize .Name)) }}_prometheus.go" operation_groups: + - name: operation_init_prometheus + source: server/operationInitPrometheus.gotmpl + target: "{{ joinFilePath .Target .ServerPackage .APIPackage (snakize (pascalize .Name)) }}" + file_name: "prometheus.go" diff --git a/templates/server/initPrometheus.gotmpl b/templates/server/initPrometheus.gotmpl index 5091da796767856577b5f919d404b9d183d98c80_dGVtcGxhdGVzL3NlcnZlci9pbml0UHJvbWV0aGV1cy5nb3RtcGw=..7908dfbcac1f1960c82db6b7c8af2c72d2acab66_dGVtcGxhdGVzL3NlcnZlci9pbml0UHJvbWV0aGV1cy5nb3RtcGw= 100644 --- a/templates/server/initPrometheus.gotmpl +++ b/templates/server/initPrometheus.gotmpl @@ -24,10 +24,7 @@ "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}} + {{range .OperationGroups}} + {{ .Name }}.RegisterPrometheus(reg) + {{- end}} } diff --git a/templates/server/operationInitPrometheus.gotmpl b/templates/server/operationInitPrometheus.gotmpl new file mode 100644 index 0000000000000000000000000000000000000000..7908dfbcac1f1960c82db6b7c8af2c72d2acab66_dGVtcGxhdGVzL3NlcnZlci9vcGVyYXRpb25Jbml0UHJvbWV0aGV1cy5nb3RtcGw= --- /dev/null +++ b/templates/server/operationInitPrometheus.gotmpl @@ -0,0 +1,23 @@ +// Code generated by go-swagger with a alpe-api template; DO NOT EDIT. + +package {{.Name}} +{{ $package := .Name }} + +import ( + "github.com/prometheus/client_golang/prometheus" + {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }} + {{ end }} +) + +func RegisterPrometheus(reg prometheus.Registerer) { + reg = prometheus.WrapRegistererWith(prometheus.Labels{ + "api_operation_group": "{{ .Name }}", + }, reg) + + {{ range .Operations}} + reg.MustRegister( + {{ camelize .Name }}HandlerDuration, + {{ camelize .Name }}HandlerInFlight, + ) + {{- end}} +}