@@ -59,47 +59,98 @@ func (m *Monitor) initGinMetrics() {
59
59
Type : Counter ,
60
60
Name : metricRequestTotal ,
61
61
Description : "all the server received request num." ,
62
- Labels : nil ,
62
+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestTotal ) ,
63
63
})
64
64
_ = monitor .AddMetric (& Metric {
65
65
Type : Counter ,
66
66
Name : metricRequestUVTotal ,
67
67
Description : "all the server received ip num." ,
68
- Labels : nil ,
68
+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestUVTotal ) ,
69
69
})
70
70
_ = monitor .AddMetric (& Metric {
71
71
Type : Counter ,
72
72
Name : metricURIRequestTotal ,
73
73
Description : "all the server received request num with every uri." ,
74
- Labels : [] string { "uri" , "method" , "code" } ,
74
+ Labels : m . getMetricLabelsIncludingMetadata ( metricURIRequestTotal ) ,
75
75
})
76
76
_ = monitor .AddMetric (& Metric {
77
77
Type : Counter ,
78
78
Name : metricRequestBody ,
79
79
Description : "the server received request body size, unit byte" ,
80
- Labels : nil ,
80
+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestBody ) ,
81
81
})
82
82
_ = monitor .AddMetric (& Metric {
83
83
Type : Counter ,
84
84
Name : metricResponseBody ,
85
85
Description : "the server send response body size, unit byte" ,
86
- Labels : nil ,
86
+ Labels : m . getMetricLabelsIncludingMetadata ( metricResponseBody ) ,
87
87
})
88
88
_ = monitor .AddMetric (& Metric {
89
89
Type : Histogram ,
90
90
Name : metricRequestDuration ,
91
91
Description : "the time server took to handle the request." ,
92
- Labels : [] string { "uri" } ,
92
+ Labels : m . getMetricLabelsIncludingMetadata ( metricRequestDuration ) ,
93
93
Buckets : m .reqDuration ,
94
94
})
95
95
_ = monitor .AddMetric (& Metric {
96
96
Type : Counter ,
97
97
Name : metricSlowRequest ,
98
98
Description : fmt .Sprintf ("the server handled slow requests counter, t=%d." , m .slowTime ),
99
- Labels : [] string { "uri" , "method" , "code" } ,
99
+ Labels : m . getMetricLabelsIncludingMetadata ( metricSlowRequest ) ,
100
100
})
101
101
}
102
102
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
+
103
154
// monitorInterceptor as gin monitor middleware.
104
155
func (m * Monitor ) monitorInterceptor (ctx * gin.Context ) {
105
156
// some paths should not be reported
@@ -122,34 +173,50 @@ func (m *Monitor) ginMetricHandle(ctx *gin.Context, start time.Time) {
122
173
w := ctx .Writer
123
174
124
175
// set request total
125
- _ = m .GetMetric (metricRequestTotal ).Inc (nil )
176
+ var metric_values []string = nil
177
+ _ = m .GetMetric (metricRequestTotal ).Inc (m .getMetricValues (metric_values ))
126
178
127
179
// set uv
128
180
if clientIP := ctx .ClientIP (); ! bloomFilter .Contains (clientIP ) {
129
181
bloomFilter .Add (clientIP )
130
- _ = m .GetMetric (metricRequestUVTotal ).Inc (nil )
182
+ metric_values = nil
183
+ _ = m .GetMetric (metricRequestUVTotal ).Inc (m .getMetricValues (metric_values ))
131
184
}
132
185
133
186
// 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 ))
135
189
136
190
// set request body size
137
191
// since r.ContentLength can be negative (in some occasions) guard the operation
138
192
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 ))
140
195
}
141
196
142
197
// set slow request
143
198
latency := time .Since (start )
144
199
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 ))
146
202
}
147
203
148
204
// 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 ())
150
207
151
208
// set response size
152
209
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 ... )
154
220
}
221
+ return metric_values
155
222
}
0 commit comments