| |
| |
| |
| package telemetry |
|
|
| import ( |
| "log" |
| "math" |
| "strings" |
|
|
| "github.com/prometheus/client_golang/prometheus" |
| "github.com/valyala/fasthttp" |
| ) |
|
|
| |
| |
| func getPrometheusLabelValues(expectedLabels []string, headerValues map[string]string) []string { |
| values := make([]string, len(expectedLabels)) |
| for i, label := range expectedLabels { |
| if value, exists := headerValues[label]; exists { |
| values[i] = value |
| } else { |
| values[i] = "" |
| } |
| } |
| return values |
| } |
|
|
| |
| |
| |
| |
| func collectPrometheusKeyValues(ctx *fasthttp.RequestCtx) map[string]string { |
| path := string(ctx.Path()) |
| method := string(ctx.Method()) |
|
|
| |
| labelValues := map[string]string{ |
| "path": path, |
| "method": method, |
| } |
|
|
| |
| ctx.Request.Header.All()(func(key, value []byte) bool { |
| keyStr := strings.ToLower(string(key)) |
| if strings.HasPrefix(keyStr, "x-bf-prom-") { |
| labelName := strings.TrimPrefix(keyStr, "x-bf-prom-") |
| labelValues[labelName] = string(value) |
| ctx.SetUserValue(keyStr, string(value)) |
| } |
| return true |
| }) |
|
|
| return labelValues |
| } |
|
|
| |
| |
| func safeObserve(histogram *prometheus.HistogramVec, value float64, labels ...string) { |
| if value > 0 && value < math.MaxFloat64 { |
| metric, err := histogram.GetMetricWithLabelValues(labels...) |
| if err != nil { |
| log.Printf("Error getting metric with label values: %v", err) |
| } else { |
| metric.Observe(value) |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func containsLabel(slice []string, label string) bool { |
| for _, s := range slice { |
| |
| if s == label { |
| return true |
| } |
| |
| if strings.ReplaceAll(s, "-", "_") == strings.ReplaceAll(label, "-", "_") { |
| return true |
| } |
| |
| if strings.ReplaceAll(s, "_", "-") == strings.ReplaceAll(label, "_", "-") { |
| return true |
| } |
| } |
| return false |
| } |
|
|