|
51 | 51 | spanKindFilter string
|
52 | 52 | serviceFilter string
|
53 | 53 | rate string
|
| 54 | + tagFilters []string |
54 | 55 | }
|
55 | 56 |
|
56 | 57 | metricsQueryParams struct {
|
@@ -134,13 +135,25 @@ func (m MetricsReader) GetLatencies(ctx context.Context, requestParams *metricst
|
134 | 135 | metricName: "service_latencies",
|
135 | 136 | metricDesc: fmt.Sprintf("%.2fth quantile latency, grouped by service", requestParams.Quantile),
|
136 | 137 | buildPromQuery: func(p promQueryParams) string {
|
| 138 | + // Build filter string including service_name, span_kind, and tags |
| 139 | + filters := []string{fmt.Sprintf(`service_name =~ %q`, p.serviceFilter)} |
| 140 | + |
| 141 | + if p.spanKindFilter != "" { |
| 142 | + filters = append(filters, p.spanKindFilter) |
| 143 | + } |
| 144 | + |
| 145 | + // Add tag filters if there are any |
| 146 | + if len(p.tagFilters) > 0 { |
| 147 | + filters = append(filters, p.tagFilters...) |
| 148 | + } |
| 149 | + |
| 150 | + filterStr := strings.Join(filters, ", ") |
| 151 | + |
137 | 152 | return fmt.Sprintf(
|
138 |
| - // Note: p.spanKindFilter can be ""; trailing commas are okay within a timeseries selection. |
139 |
| - `histogram_quantile(%.2f, sum(rate(%s_bucket{service_name =~ %q, %s}[%s])) by (%s))`, |
| 153 | + `histogram_quantile(%.2f, sum(rate(%s_bucket{%s}[%s])) by (%s))`, |
140 | 154 | requestParams.Quantile,
|
141 | 155 | m.latencyMetricName,
|
142 |
| - p.serviceFilter, |
143 |
| - p.spanKindFilter, |
| 156 | + filterStr, |
144 | 157 | p.rate,
|
145 | 158 | p.groupBy,
|
146 | 159 | )
|
@@ -177,12 +190,24 @@ func (m MetricsReader) GetCallRates(ctx context.Context, requestParams *metricst
|
177 | 190 | metricName: "service_call_rate",
|
178 | 191 | metricDesc: "calls/sec, grouped by service",
|
179 | 192 | buildPromQuery: func(p promQueryParams) string {
|
| 193 | + // Build filter string including service_name, span_kind, and tags |
| 194 | + filters := []string{fmt.Sprintf(`service_name =~ %q`, p.serviceFilter)} |
| 195 | + |
| 196 | + if p.spanKindFilter != "" { |
| 197 | + filters = append(filters, p.spanKindFilter) |
| 198 | + } |
| 199 | + |
| 200 | + // Add tag filters if there are any |
| 201 | + if len(p.tagFilters) > 0 { |
| 202 | + filters = append(filters, p.tagFilters...) |
| 203 | + } |
| 204 | + |
| 205 | + filterStr := strings.Join(filters, ", ") |
| 206 | + |
180 | 207 | return fmt.Sprintf(
|
181 |
| - // Note: p.spanKindFilter can be ""; trailing commas are okay within a timeseries selection. |
182 |
| - `sum(rate(%s{service_name =~ %q, %s}[%s])) by (%s)`, |
| 208 | + `sum(rate(%s{%s}[%s])) by (%s)`, |
183 | 209 | m.callsMetricName,
|
184 |
| - p.serviceFilter, |
185 |
| - p.spanKindFilter, |
| 210 | + filterStr, |
186 | 211 | p.rate,
|
187 | 212 | p.groupBy,
|
188 | 213 | )
|
@@ -211,11 +236,32 @@ func (m MetricsReader) GetErrorRates(ctx context.Context, requestParams *metrics
|
211 | 236 | metricName: "service_error_rate",
|
212 | 237 | metricDesc: "error rate, computed as a fraction of errors/sec over calls/sec, grouped by service",
|
213 | 238 | buildPromQuery: func(p promQueryParams) string {
|
| 239 | + // Build base filters for all queries (service_name) |
| 240 | + baseFilters := []string{fmt.Sprintf(`service_name =~ %q`, p.serviceFilter)} |
| 241 | + |
| 242 | + // Add status_code filter only for error rate numerator, must be right after service_name to match test expectations |
| 243 | + errorFilters := append([]string{}, baseFilters...) |
| 244 | + errorFilters = append(errorFilters, `status_code = "STATUS_CODE_ERROR"`) |
| 245 | + |
| 246 | + // Add span_kind filter |
| 247 | + if p.spanKindFilter != "" { |
| 248 | + baseFilters = append(baseFilters, p.spanKindFilter) |
| 249 | + errorFilters = append(errorFilters, p.spanKindFilter) |
| 250 | + } |
| 251 | + |
| 252 | + // Add tag filters if there are any |
| 253 | + if len(p.tagFilters) > 0 { |
| 254 | + baseFilters = append(baseFilters, p.tagFilters...) |
| 255 | + errorFilters = append(errorFilters, p.tagFilters...) |
| 256 | + } |
| 257 | + |
| 258 | + errorFilterStr := strings.Join(errorFilters, ", ") |
| 259 | + baseFilterStr := strings.Join(baseFilters, ", ") |
| 260 | + |
214 | 261 | return fmt.Sprintf(
|
215 |
| - // Note: p.spanKindFilter can be ""; trailing commas are okay within a timeseries selection. |
216 |
| - `sum(rate(%s{service_name =~ %q, status_code = "STATUS_CODE_ERROR", %s}[%s])) by (%s) / sum(rate(%s{service_name =~ %q, %s}[%s])) by (%s)`, |
217 |
| - m.callsMetricName, p.serviceFilter, p.spanKindFilter, p.rate, p.groupBy, |
218 |
| - m.callsMetricName, p.serviceFilter, p.spanKindFilter, p.rate, p.groupBy, |
| 262 | + `sum(rate(%s{%s}[%s])) by (%s) / sum(rate(%s{%s}[%s])) by (%s)`, |
| 263 | + m.callsMetricName, errorFilterStr, p.rate, p.groupBy, |
| 264 | + m.callsMetricName, baseFilterStr, p.rate, p.groupBy, |
219 | 265 | )
|
220 | 266 | },
|
221 | 267 | }
|
@@ -308,11 +354,24 @@ func (m MetricsReader) buildPromQuery(metricsParams metricsQueryParams) string {
|
308 | 354 | if len(metricsParams.SpanKinds) > 0 {
|
309 | 355 | spanKindFilter = fmt.Sprintf(`span_kind =~ %q`, strings.Join(metricsParams.SpanKinds, "|"))
|
310 | 356 | }
|
| 357 | + |
| 358 | + // Build tag filters |
| 359 | + var tagFilters []string |
| 360 | + if len(metricsParams.Tags) > 0 { |
| 361 | + for k, v := range metricsParams.Tags { |
| 362 | + // Escape dots in key names for Prometheus compatibility |
| 363 | + escapedKey := strings.ReplaceAll(k, ".", "_") |
| 364 | + tagFilters = append(tagFilters, fmt.Sprintf(`%s=%q`, escapedKey, v)) |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + fmt.Println(">>>>>>>>> tagfilters", tagFilters) |
311 | 369 | promParams := promQueryParams{
|
312 | 370 | serviceFilter: strings.Join(metricsParams.ServiceNames, "|"),
|
313 | 371 | spanKindFilter: spanKindFilter,
|
314 | 372 | rate: promqlDurationString(metricsParams.RatePer),
|
315 | 373 | groupBy: strings.Join(groupBy, ","),
|
| 374 | + tagFilters: tagFilters, |
316 | 375 | }
|
317 | 376 | return metricsParams.buildPromQuery(promParams)
|
318 | 377 | }
|
|
0 commit comments