1
- package main
1
+ package loganalytics
2
2
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- operationalinsightsProfile "github.com/Azure/azure-sdk-for-go/profiles/latest/operationalinsights/mgmt/operationalinsights"
8
7
"github.com/Azure/azure-sdk-for-go/services/operationalinsights/v1/operationalinsights"
9
8
"github.com/Azure/go-autorest/autorest"
9
+ "github.com/Azure/go-autorest/autorest/azure"
10
10
"github.com/Azure/go-autorest/autorest/to"
11
11
"github.com/patrickmn/go-cache"
12
12
"github.com/prometheus/client_golang/prometheus"
13
13
"github.com/remeh/sizedwaitgroup"
14
14
log "github.com/sirupsen/logrus"
15
+ "github.com/webdevops/azure-loganalytics-exporter/config"
15
16
"github.com/webdevops/azure-resourcegraph-exporter/kusto"
16
17
"net/http"
17
18
"strconv"
18
19
"time"
19
20
)
20
21
21
22
const (
22
- OPINSIGHTS_URL_SUFFIX = "/v1"
23
+ OperationInsightsWorkspaceUrlSuffix = "/v1"
23
24
)
24
25
25
26
type (
26
27
LogAnalyticsProber struct {
28
+ QueryConfig kusto.Config
29
+ Conf config.Opts
30
+
31
+ Azure struct {
32
+ Environment azure.Environment
33
+ OpInsightsAuthorizer autorest.Authorizer
34
+ AzureAuthorizer autorest.Authorizer
35
+ }
36
+
27
37
workspaceList []string
28
38
29
39
request * http.Request
@@ -48,11 +58,6 @@ type (
48
58
ServiceDiscovery LogAnalyticsServiceDiscovery
49
59
}
50
60
51
- LogAnalyticsServiceDiscovery struct {
52
- enabled bool
53
- prober * LogAnalyticsProber
54
- }
55
-
56
61
LogAnalyticsProbeResult struct {
57
62
Name string
58
63
Metrics []kusto.MetricRow
@@ -126,8 +131,8 @@ func (p *LogAnalyticsProber) AddWorkspaces(workspace ...string) {
126
131
127
132
func (p * LogAnalyticsProber ) LogAnalyticsQueryClient () operationalinsights.QueryClient {
128
133
// Create and authorize a operationalinsights client
129
- client := operationalinsights .NewQueryClientWithBaseURI (AzureEnvironment . ResourceIdentifiers .OperationalInsights + OPINSIGHTS_URL_SUFFIX )
130
- client .Authorizer = OpInsightsAuthorizer
134
+ client := operationalinsights .NewQueryClientWithBaseURI (p . Azure . Environment . ResourceIdentifiers .OperationalInsights + OperationInsightsWorkspaceUrlSuffix )
135
+ client .Authorizer = p . Azure . OpInsightsAuthorizer
131
136
client .ResponseInspector = p .respondDecorator (nil )
132
137
return client
133
138
}
@@ -138,7 +143,7 @@ func (p *LogAnalyticsProber) Run() {
138
143
// check if value is cached
139
144
executeQuery := true
140
145
if p .cache != nil && p .config .cacheEnabled {
141
- if v , ok := metricCache .Get (* p .config .cacheKey ); ok {
146
+ if v , ok := p . cache .Get (* p .config .cacheKey ); ok {
142
147
if cacheData , ok := v .([]byte ); ok {
143
148
if err := json .Unmarshal (cacheData , & p .metricList ); err == nil {
144
149
p .logger .Debug ("fetched from cache" )
@@ -165,7 +170,7 @@ func (p *LogAnalyticsProber) Run() {
165
170
p .logger .Debug ("saving metrics to cache" )
166
171
if cacheData , err := json .Marshal (p .metricList ); err == nil {
167
172
p .response .Header ().Add ("X-metrics-cached-until" , time .Now ().Add (* p .config .cacheDuration ).Format (time .RFC3339 ))
168
- metricCache .Set (* p .config .cacheKey , cacheData , * p .config .cacheDuration )
173
+ p . cache .Set (* p .config .cacheKey , cacheData , * p .config .cacheDuration )
169
174
p .logger .Debugf ("saved metric to cache for %s" , p .config .cacheDuration .String ())
170
175
}
171
176
}
@@ -197,7 +202,7 @@ func (p *LogAnalyticsProber) Run() {
197
202
func (p * LogAnalyticsProber ) executeQueries () {
198
203
queryClient := p .LogAnalyticsQueryClient ()
199
204
200
- for _ , queryRow := range Config .Queries {
205
+ for _ , queryRow := range p . QueryConfig .Queries {
201
206
queryConfig := queryRow
202
207
203
208
// check if query matches module name
@@ -342,7 +347,7 @@ func (p *LogAnalyticsProber) parseCacheTime(r *http.Request) (time.Duration, err
342
347
}
343
348
344
349
func (p * LogAnalyticsProber ) NewSizedWaitGroup () sizedwaitgroup.SizedWaitGroup {
345
- size := opts .Loganalytics .Parallel
350
+ size := p . Conf .Loganalytics .Parallel
346
351
347
352
parallelString := p .request .URL .Query ().Get ("parallel" )
348
353
if parallelString != "" {
@@ -353,46 +358,3 @@ func (p *LogAnalyticsProber) NewSizedWaitGroup() sizedwaitgroup.SizedWaitGroup {
353
358
354
359
return sizedwaitgroup .New (size )
355
360
}
356
-
357
- func (sd * LogAnalyticsServiceDiscovery ) ResourcesClient (subscriptionId string ) * operationalinsightsProfile.WorkspacesClient {
358
- client := operationalinsightsProfile .NewWorkspacesClientWithBaseURI (AzureEnvironment .ResourceManagerEndpoint , subscriptionId )
359
- client .Authorizer = AzureAuthorizer
360
- client .ResponseInspector = sd .prober .respondDecorator (& subscriptionId )
361
-
362
- return & client
363
- }
364
-
365
- func (sd * LogAnalyticsServiceDiscovery ) Use () {
366
- sd .enabled = true
367
- }
368
- func (sd * LogAnalyticsServiceDiscovery ) Find () {
369
- contextLogger := sd .prober .logger .WithFields (log.Fields {
370
- "type" : "servicediscovery" ,
371
- })
372
-
373
- contextLogger .Debug ("requesting list for workspaces via Azure API" )
374
-
375
- params := sd .prober .request .URL .Query ()
376
-
377
- subscriptionList , _ := paramsGetList (params , "subscription" )
378
- for _ , subscriptionId := range subscriptionList {
379
- subscriptionLogger := contextLogger .WithFields (log.Fields {
380
- "subscription" : subscriptionId ,
381
- })
382
-
383
- list , err := sd .ResourcesClient (subscriptionId ).List (sd .prober .ctx )
384
- if err != nil {
385
- subscriptionLogger .Error (err )
386
- panic (LogAnalyticsPanicStop {Message : err .Error ()})
387
- }
388
-
389
- for _ , val := range * list .Value {
390
- if val .CustomerID != nil {
391
- sd .prober .workspaceList = append (
392
- sd .prober .workspaceList ,
393
- to .String (val .CustomerID ),
394
- )
395
- }
396
- }
397
- }
398
- }
0 commit comments