66// types are implemented. The main motivation of creating a custom
77// package was a need to avoid usage of external unaudited dependencies.
88//
9- // Following specifications was used as reference:
9+ // Following specifications were used as reference:
1010// - https://prometheus.io/docs/instrumenting/writing_clientlibs/
1111// - https://prometheus.io/docs/instrumenting/exposition_formats/
1212package metrics
@@ -28,22 +28,52 @@ type metric interface {
2828 expose () string
2929}
3030
31+ // Label represents an arbitrary information which will be attached to all
32+ // metrics managed by the registry.
33+ type Label struct {
34+ name string
35+ value string
36+ }
37+
38+ // NewLabel creates a new label using the given name and value.
39+ func NewLabel (name , value string ) Label {
40+ return Label {name , value }
41+ }
42+
3143// Registry performs all management of metrics. Specifically, it allows
3244// to registering new metrics and exposing them through the metrics server.
3345type Registry struct {
34- application string
35- identifier string
46+ labels map [string ]string
3647
3748 metrics map [string ]metric
3849 metricsMutex sync.RWMutex
3950}
4051
4152// NewRegistry creates a new metrics registry.
42- func NewRegistry (application , identifier string ) * Registry {
53+ func NewRegistry (
54+ application , identifier string ,
55+ additionalLabels ... Label ,
56+ ) * Registry {
57+ labels := map [string ]string {
58+ "application" : application ,
59+ "identifier" : identifier ,
60+ }
61+
62+ for _ , additionalLabel := range additionalLabels {
63+ if additionalLabel .name == "" || additionalLabel .value == "" {
64+ continue
65+ }
66+
67+ if _ , exists := labels [additionalLabel .name ]; exists {
68+ continue
69+ }
70+
71+ labels [additionalLabel .name ] = additionalLabel .value
72+ }
73+
4374 return & Registry {
44- application : application ,
45- identifier : identifier ,
46- metrics : make (map [string ]metric ),
75+ labels : labels ,
76+ metrics : make (map [string ]metric ),
4777 }
4878}
4979
@@ -90,11 +120,8 @@ func (r *Registry) NewGauge(name string) (*Gauge, error) {
90120 }
91121
92122 gauge := & Gauge {
93- name : name ,
94- labels : map [string ]string {
95- "application" : r .application ,
96- "identifier" : r .identifier ,
97- },
123+ name : name ,
124+ labels : r .labels ,
98125 }
99126
100127 r .metrics [name ] = gauge
0 commit comments