1- package main
1+ package loganalytics
22
33import (
44 "context"
55 "encoding/json"
66 "fmt"
7- operationalinsightsProfile "github.com/Azure/azure-sdk-for-go/profiles/latest/operationalinsights/mgmt/operationalinsights"
87 "github.com/Azure/azure-sdk-for-go/services/operationalinsights/v1/operationalinsights"
98 "github.com/Azure/go-autorest/autorest"
9+ "github.com/Azure/go-autorest/autorest/azure"
1010 "github.com/Azure/go-autorest/autorest/to"
1111 "github.com/patrickmn/go-cache"
1212 "github.com/prometheus/client_golang/prometheus"
1313 "github.com/remeh/sizedwaitgroup"
1414 log "github.com/sirupsen/logrus"
15+ "github.com/webdevops/azure-loganalytics-exporter/config"
1516 "github.com/webdevops/azure-resourcegraph-exporter/kusto"
1617 "net/http"
1718 "strconv"
1819 "time"
1920)
2021
2122const (
22- OPINSIGHTS_URL_SUFFIX = "/v1"
23+ OperationInsightsWorkspaceUrlSuffix = "/v1"
2324)
2425
2526type (
2627 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+
2737 workspaceList []string
2838
2939 request * http.Request
@@ -48,11 +58,6 @@ type (
4858 ServiceDiscovery LogAnalyticsServiceDiscovery
4959 }
5060
51- LogAnalyticsServiceDiscovery struct {
52- enabled bool
53- prober * LogAnalyticsProber
54- }
55-
5661 LogAnalyticsProbeResult struct {
5762 Name string
5863 Metrics []kusto.MetricRow
@@ -126,8 +131,8 @@ func (p *LogAnalyticsProber) AddWorkspaces(workspace ...string) {
126131
127132func (p * LogAnalyticsProber ) LogAnalyticsQueryClient () operationalinsights.QueryClient {
128133 // 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
131136 client .ResponseInspector = p .respondDecorator (nil )
132137 return client
133138}
@@ -138,7 +143,7 @@ func (p *LogAnalyticsProber) Run() {
138143 // check if value is cached
139144 executeQuery := true
140145 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 {
142147 if cacheData , ok := v .([]byte ); ok {
143148 if err := json .Unmarshal (cacheData , & p .metricList ); err == nil {
144149 p .logger .Debug ("fetched from cache" )
@@ -165,7 +170,7 @@ func (p *LogAnalyticsProber) Run() {
165170 p .logger .Debug ("saving metrics to cache" )
166171 if cacheData , err := json .Marshal (p .metricList ); err == nil {
167172 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 )
169174 p .logger .Debugf ("saved metric to cache for %s" , p .config .cacheDuration .String ())
170175 }
171176 }
@@ -197,7 +202,7 @@ func (p *LogAnalyticsProber) Run() {
197202func (p * LogAnalyticsProber ) executeQueries () {
198203 queryClient := p .LogAnalyticsQueryClient ()
199204
200- for _ , queryRow := range Config .Queries {
205+ for _ , queryRow := range p . QueryConfig .Queries {
201206 queryConfig := queryRow
202207
203208 // check if query matches module name
@@ -342,7 +347,7 @@ func (p *LogAnalyticsProber) parseCacheTime(r *http.Request) (time.Duration, err
342347}
343348
344349func (p * LogAnalyticsProber ) NewSizedWaitGroup () sizedwaitgroup.SizedWaitGroup {
345- size := opts .Loganalytics .Parallel
350+ size := p . Conf .Loganalytics .Parallel
346351
347352 parallelString := p .request .URL .Query ().Get ("parallel" )
348353 if parallelString != "" {
@@ -353,46 +358,3 @@ func (p *LogAnalyticsProber) NewSizedWaitGroup() sizedwaitgroup.SizedWaitGroup {
353358
354359 return sizedwaitgroup .New (size )
355360}
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