Skip to content

Commit ddcb51f

Browse files
committed
add resource labels and custom labels for single workspace queries
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 799c760 commit ddcb51f

File tree

3 files changed

+76
-32
lines changed

3 files changed

+76
-32
lines changed

config/opts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type (
2020
ServiceDiscovery struct {
2121
CacheDuration *time.Duration `long:"azure.servicediscovery.cache" env:"AZURE_SERVICEDISCOVERY_CACHE" description:"Duration for caching Azure ServiceDiscovery of workspaces to reduce API calls (time.Duration)" default:"30m"`
2222
}
23+
ResourceTags []string `long:"azure.resource-tag" env:"AZURE_RESOURCE_TAG" env-delim:" " description:"Azure Resource tags (space delimiter)" default:"owner"`
2324
}
2425

2526
Loganalytics struct {

loganalytics/prober.go

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ type (
3838
Client *armclient.ArmClient
3939
}
4040

41-
workspaceList []string
41+
tagManagerConfig *armclient.ResourceTagManager
42+
43+
workspaceList []WorkspaceConfig
4244

4345
request *http.Request
4446
response http.ResponseWriter
@@ -65,6 +67,12 @@ type (
6567
concurrencyWaitGroup *sizedwaitgroup.SizedWaitGroup
6668
}
6769

70+
WorkspaceConfig struct {
71+
ResourceID string
72+
CustomerID string
73+
Labels map[string]string
74+
}
75+
6876
LogAnalyticsProbeResult struct {
6977
WorkspaceId string
7078
Name string
@@ -80,7 +88,7 @@ type (
8088
func NewLogAnalyticsProber(logger *zap.SugaredLogger, w http.ResponseWriter, r *http.Request, concurrencyWaitGroup *sizedwaitgroup.SizedWaitGroup) *LogAnalyticsProber {
8189
prober := LogAnalyticsProber{}
8290
prober.logger = logger
83-
prober.workspaceList = []string{}
91+
prober.workspaceList = []WorkspaceConfig{}
8492
prober.request = r
8593
prober.response = w
8694
prober.ctx = context.Background()
@@ -121,6 +129,13 @@ func (p *LogAnalyticsProber) Init() {
121129
),
122130
)
123131
}
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
124139
}
125140

126141
func (p *LogAnalyticsProber) EnableCache(cache *cache.Cache) {
@@ -135,21 +150,44 @@ func (p *LogAnalyticsProber) GetPrometheusRegistry() *prometheus.Registry {
135150
return p.registry
136151
}
137152

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+
}
146157

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)
148162
}
149163

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
151182
}
152183

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+
}
153191
}
154192

155193
func (p *LogAnalyticsProber) Run() {
@@ -239,7 +277,10 @@ func (p *LogAnalyticsProber) executeQueries() error {
239277

240278
workspaceList := p.workspaceList
241279
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+
}
243284
}
244285

245286
if len(workspaceList) == 0 {
@@ -279,9 +320,9 @@ func (p *LogAnalyticsProber) executeQueries() error {
279320
}()
280321
case "", "single":
281322
for _, row := range workspaceList {
282-
workspaceId := row
323+
workspaceConfig := row
283324
// 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()
285326

286327
wgProbes.Add(1)
287328
p.concurrencyWaitGroup.Add()
@@ -290,7 +331,7 @@ func (p *LogAnalyticsProber) executeQueries() error {
290331
defer p.concurrencyWaitGroup.Done()
291332
p.sendQueryToSingleWorkspace(
292333
contextLogger,
293-
workspaceId,
334+
workspaceConfig,
294335
queryConfig,
295336
resultChannel,
296337
)
@@ -344,7 +385,7 @@ func (p *LogAnalyticsProber) executeQueries() error {
344385
return nil
345386
}
346387

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) {
348389
clientOpts := azquery.LogsClientOptions{ClientOptions: *p.Azure.Client.NewAzCoreClientOptions()}
349390
logsClient, err := azquery.NewLogsClient(p.Azure.Client.GetCred(), &clientOpts)
350391
if err != nil {
@@ -359,8 +400,8 @@ func (p *LogAnalyticsProber) queryWorkspace(workspaces []string, queryConfig kus
359400

360401
additionalWorkspaces := []*string{}
361402
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))
364405
}
365406
}
366407

@@ -371,13 +412,13 @@ func (p *LogAnalyticsProber) queryWorkspace(workspaces []string, queryConfig kus
371412
AdditionalWorkspaces: additionalWorkspaces,
372413
}
373414

374-
return logsClient.QueryWorkspace(p.ctx, workspaces[0], queryBody, &opts)
415+
return logsClient.QueryWorkspace(p.ctx, workspaces[0].CustomerID, queryBody, &opts)
375416
}
376417

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) {
378419
workspaceLogger := logger.With(zap.Any("workspaceId", workspaces))
379420

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")
381422

382423
queryResults, queryErr := p.queryWorkspace(workspaces, queryConfig)
383424
if queryErr != nil {
@@ -424,12 +465,12 @@ func (p *LogAnalyticsProber) sendQueryToMultipleWorkspace(logger *zap.SugaredLog
424465
logger.Debug("metrics parsed")
425466
}
426467

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))
429470

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")
431472

432-
queryResults, queryErr := p.queryWorkspace([]string{workspaceId}, queryConfig)
473+
queryResults, queryErr := p.queryWorkspace([]WorkspaceConfig{workspaceConfig}, queryConfig)
433474
if queryErr != nil {
434475
workspaceLogger.Error(queryErr.Error())
435476
result <- LogAnalyticsProbeResult{
@@ -459,11 +500,16 @@ func (p *LogAnalyticsProber) sendQueryToSingleWorkspace(logger *zap.SugaredLogge
459500
// inject workspaceId
460501
for num := range metric {
461502
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+
}
463509
}
464510

465511
result <- LogAnalyticsProbeResult{
466-
WorkspaceId: workspaceId,
512+
WorkspaceId: workspaceConfig.CustomerID,
467513
Name: metricName,
468514
Metrics: metric,
469515
}

loganalytics/servicediscovery.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ func (sd *LogAnalyticsServiceDiscovery) findWorkspaces(logger *zap.SugaredLogger
158158
}
159159

160160
for _, row := range result {
161-
prober.workspaceList = append(
162-
prober.workspaceList,
163-
row["customerId"].(string),
164-
)
161+
prober.AddWorkspaces(row["id"].(string))
165162
}
166163
}

0 commit comments

Comments
 (0)