Skip to content

Commit

Permalink
make AZURE_DEVOPS_AGENTPOOL optional
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
  • Loading branch information
mblaschke committed Jan 30, 2022
1 parent 4d3d469 commit 4f234ea
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
69 changes: 69 additions & 0 deletions azure-devops-client/agentpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion collector_agentpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type CollectorAgentPool struct {
CollectorBase

Processor CollectorProcessorAgentPoolInterface
AgentPoolIdList []int64
AgentPoolIdList *[]int64
}

func (c *CollectorAgentPool) Run() {
Expand Down
2 changes: 1 addition & 1 deletion config/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"`
Expand Down
17 changes: 16 additions & 1 deletion metrics_agentpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down

0 comments on commit 4f234ea

Please sign in to comment.