From 4f234ead574a16235156b1370779e97bad201d65 Mon Sep 17 00:00:00 2001 From: Markus Blaschke Date: Sun, 30 Jan 2022 16:08:14 +0100 Subject: [PATCH] make AZURE_DEVOPS_AGENTPOOL optional Signed-off-by: Markus Blaschke --- azure-devops-client/agentpool.go | 69 ++++++++++++++++++++++++++++++++ collector_agentpool.go | 2 +- config/opts.go | 2 +- metrics_agentpool.go | 17 +++++++- 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/azure-devops-client/agentpool.go b/azure-devops-client/agentpool.go index 5a946df..5edaa44 100644 --- a/azure-devops-client/agentpool.go +++ b/azure-devops-client/agentpool.go @@ -48,6 +48,52 @@ func (c *AzureDevopsClient) ListAgentQueues(project string) (list AgentQueueList return } +type AgentPoolList struct { + Count int `json:"count"` + Value []AgentPoolEntry `json:"value"` +} + +type AgentPoolEntry struct { + CreatedOn time.Time `json:"createdOn"` + AutoProvision bool `json:"autoProvision"` + AutoUpdate bool `json:"autoUpdate"` + AutoSize bool `json:"autoSize"` + CreatedBy struct { + DisplayName string `json:"displayName"` + URL string `json:"url"` + Links struct { + Avatar struct { + Href string `json:"href"` + } `json:"avatar"` + } `json:"_links"` + ID string `json:"id"` + UniqueName string `json:"uniqueName"` + ImageURL string `json:"imageUrl"` + Descriptor string `json:"descriptor"` + } `json:"createdBy"` + Owner struct { + DisplayName string `json:"displayName"` + URL string `json:"url"` + Links struct { + Avatar struct { + Href string `json:"href"` + } `json:"avatar"` + } `json:"_links"` + ID string `json:"id"` + UniqueName string `json:"uniqueName"` + ImageURL string `json:"imageUrl"` + Descriptor string `json:"descriptor"` + } `json:"owner"` + ID int64 `json:"id"` + Scope string `json:"scope"` + Name string `json:"name"` + IsHosted bool `json:"isHosted"` + PoolType string `json:"poolType"` + Size int `json:"size"` + IsLegacy bool `json:"isLegacy"` + Options string `json:"options"` +} + type AgentPoolAgentList struct { Count int `json:"count"` List []AgentPoolAgent `json:"value"` @@ -86,6 +132,29 @@ type JobRequest struct { } } +func (c *AzureDevopsClient) ListAgentPools() (list AgentPoolList, error error) { + defer c.concurrencyUnlock() + c.concurrencyLock() + + url := fmt.Sprintf( + "/_apis/distributedtask/pools?api-version=%s", + url.QueryEscape(c.ApiVersion), + ) + response, err := c.rest().R().Get(url) + if err := c.checkResponse(response, err); err != nil { + error = err + return + } + + err = json.Unmarshal(response.Body(), &list) + if err != nil { + error = err + return + } + + return +} + func (c *AzureDevopsClient) ListAgentPoolAgents(agentPoolId int64) (list AgentPoolAgentList, error error) { defer c.concurrencyUnlock() c.concurrencyLock() diff --git a/collector_agentpool.go b/collector_agentpool.go index e342ca3..95f8ca0 100644 --- a/collector_agentpool.go +++ b/collector_agentpool.go @@ -9,7 +9,7 @@ type CollectorAgentPool struct { CollectorBase Processor CollectorProcessorAgentPoolInterface - AgentPoolIdList []int64 + AgentPoolIdList *[]int64 } func (c *CollectorAgentPool) Run() { diff --git a/config/opts.go b/config/opts.go index 04f4ce3..6135408 100644 --- a/config/opts.go +++ b/config/opts.go @@ -44,7 +44,7 @@ type ( ApiVersion string `long:"azuredevops.apiversion" env:"AZURE_DEVOPS_APIVERSION" description:"Azure DevOps API version" default:"5.1"` // agentpool - AgentPoolIdList []int64 `long:"azuredevops.agentpool" env:"AZURE_DEVOPS_AGENTPOOL" env-delim:" " description:"Enable scrape metrics for agent pool (IDs)"` + AgentPoolIdList *[]int64 `long:"azuredevops.agentpool" env:"AZURE_DEVOPS_AGENTPOOL" env-delim:" " description:"Enable scrape metrics for agent pool (IDs)"` // ignore settings FilterProjects []string `long:"whitelist.project" env:"AZURE_DEVOPS_FILTER_PROJECT" env-delim:" " description:"Filter projects (UUIDs)"` diff --git a/metrics_agentpool.go b/metrics_agentpool.go index c5395a7..e59075c 100644 --- a/metrics_agentpool.go +++ b/metrics_agentpool.go @@ -138,7 +138,22 @@ func (m *MetricsCollectorAgentPool) Collect(ctx context.Context, logger *log.Ent m.collectAgentInfo(ctx, contextLogger, callback, project) } - for _, agentPoolId := range m.CollectorReference.AgentPoolIdList { + agentPoolList := []int64{} + if m.CollectorReference.AgentPoolIdList != nil { + agentPoolList = *m.CollectorReference.AgentPoolIdList + } else { + result, err := AzureDevopsClient.ListAgentPools() + if err != nil { + logger.Error(err) + return + } + + for _, agentPool := range result.Value { + agentPoolList = append(agentPoolList, agentPool.ID) + } + } + + for _, agentPoolId := range agentPoolList { contextLogger := logger.WithFields(log.Fields{ "agentPoolId": agentPoolId, })