Skip to content

Commit 4f234ea

Browse files
committed
make AZURE_DEVOPS_AGENTPOOL optional
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 4d3d469 commit 4f234ea

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

azure-devops-client/agentpool.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,52 @@ func (c *AzureDevopsClient) ListAgentQueues(project string) (list AgentQueueList
4848
return
4949
}
5050

51+
type AgentPoolList struct {
52+
Count int `json:"count"`
53+
Value []AgentPoolEntry `json:"value"`
54+
}
55+
56+
type AgentPoolEntry struct {
57+
CreatedOn time.Time `json:"createdOn"`
58+
AutoProvision bool `json:"autoProvision"`
59+
AutoUpdate bool `json:"autoUpdate"`
60+
AutoSize bool `json:"autoSize"`
61+
CreatedBy struct {
62+
DisplayName string `json:"displayName"`
63+
URL string `json:"url"`
64+
Links struct {
65+
Avatar struct {
66+
Href string `json:"href"`
67+
} `json:"avatar"`
68+
} `json:"_links"`
69+
ID string `json:"id"`
70+
UniqueName string `json:"uniqueName"`
71+
ImageURL string `json:"imageUrl"`
72+
Descriptor string `json:"descriptor"`
73+
} `json:"createdBy"`
74+
Owner struct {
75+
DisplayName string `json:"displayName"`
76+
URL string `json:"url"`
77+
Links struct {
78+
Avatar struct {
79+
Href string `json:"href"`
80+
} `json:"avatar"`
81+
} `json:"_links"`
82+
ID string `json:"id"`
83+
UniqueName string `json:"uniqueName"`
84+
ImageURL string `json:"imageUrl"`
85+
Descriptor string `json:"descriptor"`
86+
} `json:"owner"`
87+
ID int64 `json:"id"`
88+
Scope string `json:"scope"`
89+
Name string `json:"name"`
90+
IsHosted bool `json:"isHosted"`
91+
PoolType string `json:"poolType"`
92+
Size int `json:"size"`
93+
IsLegacy bool `json:"isLegacy"`
94+
Options string `json:"options"`
95+
}
96+
5197
type AgentPoolAgentList struct {
5298
Count int `json:"count"`
5399
List []AgentPoolAgent `json:"value"`
@@ -86,6 +132,29 @@ type JobRequest struct {
86132
}
87133
}
88134

135+
func (c *AzureDevopsClient) ListAgentPools() (list AgentPoolList, error error) {
136+
defer c.concurrencyUnlock()
137+
c.concurrencyLock()
138+
139+
url := fmt.Sprintf(
140+
"/_apis/distributedtask/pools?api-version=%s",
141+
url.QueryEscape(c.ApiVersion),
142+
)
143+
response, err := c.rest().R().Get(url)
144+
if err := c.checkResponse(response, err); err != nil {
145+
error = err
146+
return
147+
}
148+
149+
err = json.Unmarshal(response.Body(), &list)
150+
if err != nil {
151+
error = err
152+
return
153+
}
154+
155+
return
156+
}
157+
89158
func (c *AzureDevopsClient) ListAgentPoolAgents(agentPoolId int64) (list AgentPoolAgentList, error error) {
90159
defer c.concurrencyUnlock()
91160
c.concurrencyLock()

collector_agentpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type CollectorAgentPool struct {
99
CollectorBase
1010

1111
Processor CollectorProcessorAgentPoolInterface
12-
AgentPoolIdList []int64
12+
AgentPoolIdList *[]int64
1313
}
1414

1515
func (c *CollectorAgentPool) Run() {

config/opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type (
4444
ApiVersion string `long:"azuredevops.apiversion" env:"AZURE_DEVOPS_APIVERSION" description:"Azure DevOps API version" default:"5.1"`
4545

4646
// agentpool
47-
AgentPoolIdList []int64 `long:"azuredevops.agentpool" env:"AZURE_DEVOPS_AGENTPOOL" env-delim:" " description:"Enable scrape metrics for agent pool (IDs)"`
47+
AgentPoolIdList *[]int64 `long:"azuredevops.agentpool" env:"AZURE_DEVOPS_AGENTPOOL" env-delim:" " description:"Enable scrape metrics for agent pool (IDs)"`
4848

4949
// ignore settings
5050
FilterProjects []string `long:"whitelist.project" env:"AZURE_DEVOPS_FILTER_PROJECT" env-delim:" " description:"Filter projects (UUIDs)"`

metrics_agentpool.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,22 @@ func (m *MetricsCollectorAgentPool) Collect(ctx context.Context, logger *log.Ent
138138
m.collectAgentInfo(ctx, contextLogger, callback, project)
139139
}
140140

141-
for _, agentPoolId := range m.CollectorReference.AgentPoolIdList {
141+
agentPoolList := []int64{}
142+
if m.CollectorReference.AgentPoolIdList != nil {
143+
agentPoolList = *m.CollectorReference.AgentPoolIdList
144+
} else {
145+
result, err := AzureDevopsClient.ListAgentPools()
146+
if err != nil {
147+
logger.Error(err)
148+
return
149+
}
150+
151+
for _, agentPool := range result.Value {
152+
agentPoolList = append(agentPoolList, agentPool.ID)
153+
}
154+
}
155+
156+
for _, agentPoolId := range agentPoolList {
142157
contextLogger := logger.WithFields(log.Fields{
143158
"agentPoolId": agentPoolId,
144159
})

0 commit comments

Comments
 (0)