@@ -59,47 +59,98 @@ func (m *Monitor) initGinMetrics() {
5959 Type : Counter ,
6060 Name : metricRequestTotal ,
6161 Description : "all the server received request num." ,
62- Labels : nil ,
62+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestTotal ) ,
6363 })
6464 _ = monitor .AddMetric (& Metric {
6565 Type : Counter ,
6666 Name : metricRequestUVTotal ,
6767 Description : "all the server received ip num." ,
68- Labels : nil ,
68+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestUVTotal ) ,
6969 })
7070 _ = monitor .AddMetric (& Metric {
7171 Type : Counter ,
7272 Name : metricURIRequestTotal ,
7373 Description : "all the server received request num with every uri." ,
74- Labels : [] string { "uri" , "method" , "code" } ,
74+ Labels : m . getMetricLabelsIncludingMetadata ( metricURIRequestTotal ) ,
7575 })
7676 _ = monitor .AddMetric (& Metric {
7777 Type : Counter ,
7878 Name : metricRequestBody ,
7979 Description : "the server received request body size, unit byte" ,
80- Labels : nil ,
80+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestBody ) ,
8181 })
8282 _ = monitor .AddMetric (& Metric {
8383 Type : Counter ,
8484 Name : metricResponseBody ,
8585 Description : "the server send response body size, unit byte" ,
86- Labels : nil ,
86+ Labels : m . getMetricLabelsIncludingMetadata ( metricResponseBody ) ,
8787 })
8888 _ = monitor .AddMetric (& Metric {
8989 Type : Histogram ,
9090 Name : metricRequestDuration ,
9191 Description : "the time server took to handle the request." ,
92- Labels : [] string { "uri" } ,
92+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestDuration ) ,
9393 Buckets : m .reqDuration ,
9494 })
9595 _ = monitor .AddMetric (& Metric {
9696 Type : Counter ,
9797 Name : metricSlowRequest ,
9898 Description : fmt .Sprintf ("the server handled slow requests counter, t=%d." , m .slowTime ),
99- Labels : [] string { "uri" , "method" , "code" } ,
99+ Labels : m . getMetricLabelsIncludingMetadata ( metricSlowRequest ) ,
100100 })
101101}
102102
103+ func (m * Monitor ) includesMetadata () bool {
104+ return len (m .metadata ) > 0
105+ }
106+
107+ func (m * Monitor ) getMetadata () ([]string , []string ) {
108+ metadata_labels := []string {}
109+ metadata_values := []string {}
110+
111+ for v := range m .metadata {
112+ metadata_labels = append (metadata_labels , v )
113+ metadata_values = append (metadata_values , m .metadata [v ])
114+ }
115+
116+ return metadata_labels , metadata_values
117+ }
118+
119+ func (m * Monitor ) getMetricLabelsIncludingMetadata (metricName string ) []string {
120+ includes_metadata := m .includesMetadata ()
121+ metadata_labels , _ := m .getMetadata ()
122+
123+ switch metricName {
124+ case metricRequestDuration :
125+ metric_labels := []string {"uri" }
126+ if includes_metadata {
127+ metric_labels = append (metric_labels , metadata_labels ... )
128+ }
129+ return metric_labels
130+
131+ case metricURIRequestTotal :
132+ metric_labels := []string {"uri" , "method" , "code" }
133+ if includes_metadata {
134+ metric_labels = append (metric_labels , metadata_labels ... )
135+ }
136+ return metric_labels
137+
138+ case metricSlowRequest :
139+ metric_labels := []string {"uri" , "method" , "code" }
140+ if includes_metadata {
141+ metric_labels = append (metric_labels , metadata_labels ... )
142+ }
143+ return metric_labels
144+
145+ default :
146+ var metric_labels []string = nil
147+ if includes_metadata {
148+ metric_labels = metadata_labels
149+ }
150+ return metric_labels
151+ }
152+ }
153+
103154// monitorInterceptor as gin monitor middleware.
104155func (m * Monitor ) monitorInterceptor (ctx * gin.Context ) {
105156 // some paths should not be reported
@@ -122,34 +173,50 @@ func (m *Monitor) ginMetricHandle(ctx *gin.Context, start time.Time) {
122173 w := ctx .Writer
123174
124175 // set request total
125- _ = m .GetMetric (metricRequestTotal ).Inc (nil )
176+ var metric_values []string = nil
177+ _ = m .GetMetric (metricRequestTotal ).Inc (m .getMetricValues (metric_values ))
126178
127179 // set uv
128180 if clientIP := ctx .ClientIP (); ! bloomFilter .Contains (clientIP ) {
129181 bloomFilter .Add (clientIP )
130- _ = m .GetMetric (metricRequestUVTotal ).Inc (nil )
182+ metric_values = nil
183+ _ = m .GetMetric (metricRequestUVTotal ).Inc (m .getMetricValues (metric_values ))
131184 }
132185
133186 // set uri request total
134- _ = m .GetMetric (metricURIRequestTotal ).Inc ([]string {ctx .FullPath (), r .Method , strconv .Itoa (w .Status ())})
187+ metric_values = []string {ctx .FullPath (), r .Method , strconv .Itoa (w .Status ())}
188+ _ = m .GetMetric (metricURIRequestTotal ).Inc (m .getMetricValues (metric_values ))
135189
136190 // set request body size
137191 // since r.ContentLength can be negative (in some occasions) guard the operation
138192 if r .ContentLength >= 0 {
139- _ = m .GetMetric (metricRequestBody ).Add (nil , float64 (r .ContentLength ))
193+ metric_values = nil
194+ _ = m .GetMetric (metricRequestBody ).Add (m .getMetricValues (metric_values ), float64 (r .ContentLength ))
140195 }
141196
142197 // set slow request
143198 latency := time .Since (start )
144199 if int32 (latency .Seconds ()) > m .slowTime {
145- _ = m .GetMetric (metricSlowRequest ).Inc ([]string {ctx .FullPath (), r .Method , strconv .Itoa (w .Status ())})
200+ metric_values = []string {ctx .FullPath (), r .Method , strconv .Itoa (w .Status ())}
201+ _ = m .GetMetric (metricSlowRequest ).Inc (m .getMetricValues (metric_values ))
146202 }
147203
148204 // set request duration
149- _ = m .GetMetric (metricRequestDuration ).Observe ([]string {ctx .FullPath ()}, latency .Seconds ())
205+ metric_values = []string {ctx .FullPath ()}
206+ _ = m .GetMetric (metricRequestDuration ).Observe (m .getMetricValues (metric_values ), latency .Seconds ())
150207
151208 // set response size
152209 if w .Size () > 0 {
153- _ = m .GetMetric (metricResponseBody ).Add (nil , float64 (w .Size ()))
210+ metric_values = nil
211+ _ = m .GetMetric (metricResponseBody ).Add (m .getMetricValues (metric_values ), float64 (w .Size ()))
212+ }
213+ }
214+
215+ func (m * Monitor ) getMetricValues (metric_values []string ) []string {
216+ includes_metadata := m .includesMetadata ()
217+ _ , metadata_values := m .getMetadata ()
218+ if includes_metadata {
219+ metric_values = append (metric_values , metadata_values ... )
154220 }
221+ return metric_values
155222}
0 commit comments