forked from webdevops/azure-metrics-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_loganalytics_query.go
154 lines (122 loc) · 4.03 KB
/
probe_loganalytics_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/operationalinsights/v1/operationalinsights"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
prometheusCommon "github.com/webdevops/go-prometheus-common"
"net/http"
"time"
)
func probeLogAnalyticsQueryHandler(w http.ResponseWriter, r *http.Request) {
var err error
var timeoutSeconds float64
var workspace, query, timespan string
params := r.URL.Query()
startTime := time.Now()
contextLogger := buildContextLoggerFromRequest(r)
// If a timeout is configured via the Prometheus header, add it to the request.
timeoutSeconds, err = getPrometheusTimeout(r, ProbeLoganalyticsScrapeTimeoutDefault)
if err != nil {
contextLogger.Error(err)
http.Error(w, fmt.Sprintf("failed to parse timeout from Prometheus header: %s", err), http.StatusInternalServerError)
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds*float64(time.Second)))
defer cancel()
r = r.WithContext(ctx)
if workspace, err = paramsGetRequired(params, "workspace"); err != nil {
contextLogger.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if query, err = paramsGetRequired(params, "query"); err != nil {
contextLogger.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if timespan, err = paramsGetRequired(params, "timespan"); err != nil {
contextLogger.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
queryBody := operationalinsights.QueryBody{
Query: &query,
Timespan: ×pan,
}
result, err := azureLogAnalyticsMetrics.Query(ctx, workspace, queryBody)
if err != nil {
contextLogger.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
}
registry := prometheus.NewRegistry()
queryInfoGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "azurerm_loganalytics_query_result",
Help: "Azure LogAnalytics query result",
}, []string{})
registry.MustRegister(queryInfoGauge)
queryInfoGauge.With(prometheus.Labels{}).Set(boolToFloat64(result.Result != nil))
metricsList := prometheusCommon.NewMetricsList()
metricsList.SetCache(metricsCache)
if result.Result != nil {
metricLabels := []string{"table"}
if result.Result.Tables != nil {
for _, table := range *result.Result.Tables {
for _, column := range *table.Columns {
metricLabels = append(metricLabels, *column.Name)
}
}
}
logRowGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "azurerm_loganalytics_query_row",
Help: "Azure LogAnalytics query row",
}, metricLabels)
registry.MustRegister(logRowGauge)
if result.Result.Tables != nil {
for _, table := range *result.Result.Tables {
fmt.Println(*table.Name)
for _, row := range *table.Rows {
rowLabels := prometheus.Labels{}
for _, metricLabelName := range metricLabels {
rowLabels[metricLabelName] = ""
}
rowLabels["table"] = *table.Name
for colId, column := range *table.Columns {
labelValue := ""
cellValue := row[colId]
if val, ok := cellValue.(bool); ok {
labelValue = boolToString(val)
}
if val, ok := cellValue.(int); ok {
labelValue = intToString(val)
}
if val, ok := cellValue.(int64); ok {
labelValue = int64ToString(val)
}
if val, ok := cellValue.(float32); ok {
labelValue = float32ToString(val)
}
if val, ok := cellValue.(float64); ok {
labelValue = float64ToString(val)
}
if val, ok := cellValue.(string); ok {
labelValue = val
}
rowLabels[*column.Name] = labelValue
}
metricsList.AddInfo(rowLabels)
}
}
}
metricsList.GaugeSet(logRowGauge)
}
// global stats counter
prometheusCollectTime.With(prometheus.Labels{
"subscriptionID": "",
"handler": ProbeLoganalyticsScrapeUrl,
"filter": query,
}).Observe(time.Until(startTime).Seconds())
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}