38
38
Client * armclient.ArmClient
39
39
}
40
40
41
- workspaceList []string
41
+ tagManagerConfig * armclient.ResourceTagManager
42
+
43
+ workspaceList []WorkspaceConfig
42
44
43
45
request * http.Request
44
46
response http.ResponseWriter
@@ -65,6 +67,12 @@ type (
65
67
concurrencyWaitGroup * sizedwaitgroup.SizedWaitGroup
66
68
}
67
69
70
+ WorkspaceConfig struct {
71
+ ResourceID string
72
+ CustomerID string
73
+ Labels map [string ]string
74
+ }
75
+
68
76
LogAnalyticsProbeResult struct {
69
77
WorkspaceId string
70
78
Name string
80
88
func NewLogAnalyticsProber (logger * zap.SugaredLogger , w http.ResponseWriter , r * http.Request , concurrencyWaitGroup * sizedwaitgroup.SizedWaitGroup ) * LogAnalyticsProber {
81
89
prober := LogAnalyticsProber {}
82
90
prober .logger = logger
83
- prober .workspaceList = []string {}
91
+ prober .workspaceList = []WorkspaceConfig {}
84
92
prober .request = r
85
93
prober .response = w
86
94
prober .ctx = context .Background ()
@@ -121,6 +129,13 @@ func (p *LogAnalyticsProber) Init() {
121
129
),
122
130
)
123
131
}
132
+
133
+ tagManagerConfig , err := p .Azure .Client .TagManager .ParseTagConfig (p .Conf .Azure .ResourceTags )
134
+ if err != nil {
135
+ p .logger .Fatal (err )
136
+ }
137
+
138
+ p .tagManagerConfig = tagManagerConfig
124
139
}
125
140
126
141
func (p * LogAnalyticsProber ) EnableCache (cache * cache.Cache ) {
@@ -135,21 +150,44 @@ func (p *LogAnalyticsProber) GetPrometheusRegistry() *prometheus.Registry {
135
150
return p .registry
136
151
}
137
152
138
- func (p * LogAnalyticsProber ) AddWorkspaces (workspaces ... string ) {
139
- for _ , workspace := range workspaces {
140
-
141
- if strings .HasPrefix (workspace , "/subscriptions/" ) {
142
- workspaceResource , err := p .ServiceDiscovery .GetWorkspace (p .ctx , workspace )
143
- if err != nil {
144
- p .logger .Panic (err )
145
- }
153
+ func (p * LogAnalyticsProber ) translateWorkspaceIntoConfig (val string ) WorkspaceConfig {
154
+ workspaceConfig := WorkspaceConfig {
155
+ Labels : map [string ]string {},
156
+ }
146
157
147
- workspace = to .String (workspaceResource .Properties .CustomerID )
158
+ if strings .HasPrefix (val , "/subscriptions/" ) {
159
+ workspaceResource , err := p .ServiceDiscovery .GetWorkspace (p .ctx , val )
160
+ if err != nil {
161
+ p .logger .Panic (err )
148
162
}
149
163
150
- p .workspaceList = append (p .workspaceList , workspace )
164
+ workspaceConfig .ResourceID = to .String (workspaceResource .ID )
165
+ workspaceConfig .CustomerID = to .String (workspaceResource .Properties .CustomerID )
166
+
167
+ if resourceInfo , err := armclient .ParseResourceId (workspaceConfig .ResourceID ); err == nil {
168
+ workspaceConfig .Labels ["resourceID" ] = workspaceConfig .ResourceID
169
+ workspaceConfig .Labels ["resourceGroup" ] = resourceInfo .ResourceGroup
170
+ workspaceConfig .Labels ["resourceName" ] = resourceInfo .ResourceName
171
+
172
+ // add custom labels
173
+ workspaceConfig .Labels = p .tagManagerConfig .AddResourceTagsToPrometheusLabels (
174
+ p .ctx ,
175
+ workspaceConfig .Labels ,
176
+ workspaceConfig .ResourceID ,
177
+ )
178
+ }
179
+ } else {
180
+ // no resource id, must be a customer id
181
+ workspaceConfig .CustomerID = val
151
182
}
152
183
184
+ return workspaceConfig
185
+ }
186
+
187
+ func (p * LogAnalyticsProber ) AddWorkspaces (workspaces ... string ) {
188
+ for _ , item := range workspaces {
189
+ p .workspaceList = append (p .workspaceList , p .translateWorkspaceIntoConfig (item ))
190
+ }
153
191
}
154
192
155
193
func (p * LogAnalyticsProber ) Run () {
@@ -239,7 +277,10 @@ func (p *LogAnalyticsProber) executeQueries() error {
239
277
240
278
workspaceList := p .workspaceList
241
279
if queryRow .Workspaces != nil && len (* queryRow .Workspaces ) >= 1 {
242
- workspaceList = * queryRow .Workspaces
280
+ workspaceList = []WorkspaceConfig {}
281
+ for _ , workspace := range * queryRow .Workspaces {
282
+ workspaceList = append (workspaceList , p .translateWorkspaceIntoConfig (workspace ))
283
+ }
243
284
}
244
285
245
286
if len (workspaceList ) == 0 {
@@ -279,9 +320,9 @@ func (p *LogAnalyticsProber) executeQueries() error {
279
320
}()
280
321
case "" , "single" :
281
322
for _ , row := range workspaceList {
282
- workspaceId := row
323
+ workspaceConfig := row
283
324
// Run the query and get the results
284
- prometheusQueryRequests .With (prometheus.Labels {"workspaceID" : workspaceId , "module" : p .config .moduleName , "metric" : queryConfig .Metric }).Inc ()
325
+ prometheusQueryRequests .With (prometheus.Labels {"workspaceID" : workspaceConfig . CustomerID , "module" : p .config .moduleName , "metric" : queryConfig .Metric }).Inc ()
285
326
286
327
wgProbes .Add (1 )
287
328
p .concurrencyWaitGroup .Add ()
@@ -290,7 +331,7 @@ func (p *LogAnalyticsProber) executeQueries() error {
290
331
defer p .concurrencyWaitGroup .Done ()
291
332
p .sendQueryToSingleWorkspace (
292
333
contextLogger ,
293
- workspaceId ,
334
+ workspaceConfig ,
294
335
queryConfig ,
295
336
resultChannel ,
296
337
)
@@ -344,7 +385,7 @@ func (p *LogAnalyticsProber) executeQueries() error {
344
385
return nil
345
386
}
346
387
347
- func (p * LogAnalyticsProber ) queryWorkspace (workspaces []string , queryConfig kusto.ConfigQuery ) (azquery.LogsClientQueryWorkspaceResponse , error ) {
388
+ func (p * LogAnalyticsProber ) queryWorkspace (workspaces []WorkspaceConfig , queryConfig kusto.ConfigQuery ) (azquery.LogsClientQueryWorkspaceResponse , error ) {
348
389
clientOpts := azquery.LogsClientOptions {ClientOptions : * p .Azure .Client .NewAzCoreClientOptions ()}
349
390
logsClient , err := azquery .NewLogsClient (p .Azure .Client .GetCred (), & clientOpts )
350
391
if err != nil {
@@ -359,8 +400,8 @@ func (p *LogAnalyticsProber) queryWorkspace(workspaces []string, queryConfig kus
359
400
360
401
additionalWorkspaces := []* string {}
361
402
if len (workspaces ) > 1 {
362
- for _ , workspaceId := range workspaces [1 :] {
363
- additionalWorkspaces = append (additionalWorkspaces , to .StringPtr (workspaceId ))
403
+ for _ , workspaceConfig := range workspaces [1 :] {
404
+ additionalWorkspaces = append (additionalWorkspaces , to .StringPtr (workspaceConfig . CustomerID ))
364
405
}
365
406
}
366
407
@@ -371,13 +412,13 @@ func (p *LogAnalyticsProber) queryWorkspace(workspaces []string, queryConfig kus
371
412
AdditionalWorkspaces : additionalWorkspaces ,
372
413
}
373
414
374
- return logsClient .QueryWorkspace (p .ctx , workspaces [0 ], queryBody , & opts )
415
+ return logsClient .QueryWorkspace (p .ctx , workspaces [0 ]. CustomerID , queryBody , & opts )
375
416
}
376
417
377
- func (p * LogAnalyticsProber ) sendQueryToMultipleWorkspace (logger * zap.SugaredLogger , workspaces []string , queryConfig kusto.ConfigQuery , result chan <- LogAnalyticsProbeResult ) {
418
+ func (p * LogAnalyticsProber ) sendQueryToMultipleWorkspace (logger * zap.SugaredLogger , workspaces []WorkspaceConfig , queryConfig kusto.ConfigQuery , result chan <- LogAnalyticsProbeResult ) {
378
419
workspaceLogger := logger .With (zap .Any ("workspaceId" , workspaces ))
379
420
380
- workspaceLogger .With (zap .String ("query" , queryConfig .Query )).Debug ("send query to loganaltyics workspaces" )
421
+ workspaceLogger .With (zap .String ("query" , queryConfig .Query )).Debug ("send query to logAnalytics workspaces" )
381
422
382
423
queryResults , queryErr := p .queryWorkspace (workspaces , queryConfig )
383
424
if queryErr != nil {
@@ -424,12 +465,12 @@ func (p *LogAnalyticsProber) sendQueryToMultipleWorkspace(logger *zap.SugaredLog
424
465
logger .Debug ("metrics parsed" )
425
466
}
426
467
427
- func (p * LogAnalyticsProber ) sendQueryToSingleWorkspace (logger * zap.SugaredLogger , workspaceId string , queryConfig kusto.ConfigQuery , result chan <- LogAnalyticsProbeResult ) {
428
- workspaceLogger := logger .With (zap .String ("workspaceId" , workspaceId ))
468
+ func (p * LogAnalyticsProber ) sendQueryToSingleWorkspace (logger * zap.SugaredLogger , workspaceConfig WorkspaceConfig , queryConfig kusto.ConfigQuery , result chan <- LogAnalyticsProbeResult ) {
469
+ workspaceLogger := logger .With (zap .String ("workspaceId" , workspaceConfig . CustomerID ))
429
470
430
- workspaceLogger .With (zap .String ("query" , queryConfig .Query )).Debug ("send query to loganaltyics workspace" )
471
+ workspaceLogger .With (zap .String ("query" , queryConfig .Query )).Debug ("send query to logAnalytics workspace" )
431
472
432
- queryResults , queryErr := p .queryWorkspace ([]string { workspaceId }, queryConfig )
473
+ queryResults , queryErr := p .queryWorkspace ([]WorkspaceConfig { workspaceConfig }, queryConfig )
433
474
if queryErr != nil {
434
475
workspaceLogger .Error (queryErr .Error ())
435
476
result <- LogAnalyticsProbeResult {
@@ -459,11 +500,16 @@ func (p *LogAnalyticsProber) sendQueryToSingleWorkspace(logger *zap.SugaredLogge
459
500
// inject workspaceId
460
501
for num := range metric {
461
502
metric [num ].Labels ["workspaceTable" ] = to .String (table .Name )
462
- metric [num ].Labels ["workspaceID" ] = workspaceId
503
+ metric [num ].Labels ["workspaceID" ] = workspaceConfig .CustomerID
504
+
505
+ // add labels from resource config
506
+ for labelName , labelValue := range workspaceConfig .Labels {
507
+ metric [num ].Labels [labelName ] = labelValue
508
+ }
463
509
}
464
510
465
511
result <- LogAnalyticsProbeResult {
466
- WorkspaceId : workspaceId ,
512
+ WorkspaceId : workspaceConfig . CustomerID ,
467
513
Name : metricName ,
468
514
Metrics : metric ,
469
515
}
0 commit comments