-
Notifications
You must be signed in to change notification settings - Fork 17
/
metrics_graph_apps.go
168 lines (148 loc) · 4.91 KB
/
metrics_graph_apps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"strings"
abstractions "github.com/microsoft/kiota-abstractions-go"
msgraphcore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/applications"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/prometheus/client_golang/prometheus"
"github.com/webdevops/go-common/prometheus/collector"
"github.com/webdevops/go-common/utils/to"
)
type MetricsCollectorGraphApps struct {
collector.Processor
prometheus struct {
app *prometheus.GaugeVec
appTags *prometheus.GaugeVec
appCredential *prometheus.GaugeVec
}
}
func (m *MetricsCollectorGraphApps) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
m.prometheus.app = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_graph_app_info",
Help: "Azure GraphQL applications information",
},
[]string{
"appAppID",
"appObjectID",
"appDisplayName",
},
)
m.Collector.RegisterMetricList("app", m.prometheus.app, true)
m.prometheus.appTags = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_graph_app_tag",
Help: "Azure GraphQL applications tag",
},
[]string{
"appAppID",
"appObjectID",
"appTag",
},
)
m.Collector.RegisterMetricList("appTag", m.prometheus.appTags, true)
m.prometheus.appCredential = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_graph_app_credential",
Help: "Azure GraphQL application credentials status",
},
[]string{
"appAppID",
"credentialName",
"credentialID",
"credentialType",
"type",
},
)
m.Collector.RegisterMetricList("appCredential", m.prometheus.appCredential, true)
}
func (m *MetricsCollectorGraphApps) Reset() {}
func (m *MetricsCollectorGraphApps) Collect(callback chan<- func()) {
headers := abstractions.NewRequestHeaders()
headers.Add("ConsistencyLevel", "eventual")
const requestCount = true
rcount := requestCount
opts := applications.ApplicationsRequestBuilderGetRequestConfiguration{
Headers: headers,
Options: nil,
QueryParameters: &applications.ApplicationsRequestBuilderGetQueryParameters{
Filter: Config.Collectors.Graph.Filter.Application,
Count: &rcount,
},
}
result, err := MsGraphClient.ServiceClient().Applications().Get(m.Context(), &opts)
if err != nil {
m.Logger().Panic(err)
}
appMetrics := m.Collector.GetMetricList("app")
appTagMetrics := m.Collector.GetMetricList("appTag")
appCredentialMetrics := m.Collector.GetMetricList("appCredential")
i, err := msgraphcore.NewPageIterator[models.Applicationable](result, MsGraphClient.RequestAdapter(), models.CreateApplicationCollectionResponseFromDiscriminatorValue)
if err != nil {
m.Logger().Panic(err)
}
err = i.Iterate(m.Context(), func(application models.Applicationable) bool {
appId := to.StringLower(application.GetAppId())
objId := to.StringLower(application.GetId())
appMetrics.AddInfo(prometheus.Labels{
"appAppID": appId,
"appObjectID": objId,
"appDisplayName": to.String(application.GetDisplayName()),
})
for _, tagValue := range application.GetTags() {
appTagMetrics.AddInfo(prometheus.Labels{
"appAppID": appId,
"appObjectID": objId,
"appTag": tagValue,
})
}
for _, credential := range application.GetPasswordCredentials() {
credential.GetDisplayName()
if credential.GetStartDateTime() != nil {
appCredentialMetrics.AddTime(prometheus.Labels{
"appAppID": appId,
"credentialName": to.String(credential.GetDisplayName()),
"credentialID": strings.ToLower(credential.GetKeyId().String()),
"credentialType": "password",
"type": "startDate",
}, credential.GetStartDateTime().UTC())
}
if credential.GetEndDateTime() != nil {
appCredentialMetrics.AddTime(prometheus.Labels{
"appAppID": appId,
"credentialName": to.String(credential.GetDisplayName()),
"credentialID": strings.ToLower(credential.GetKeyId().String()),
"credentialType": "password",
"type": "endDate",
}, credential.GetEndDateTime().UTC())
}
}
for _, credential := range application.GetKeyCredentials() {
credential.GetDisplayName()
if credential.GetStartDateTime() != nil {
appCredentialMetrics.AddTime(prometheus.Labels{
"appAppID": appId,
"credentialName": to.String(credential.GetDisplayName()),
"credentialID": strings.ToLower(credential.GetKeyId().String()),
"credentialType": "key",
"type": "startDate",
}, credential.GetStartDateTime().UTC())
}
if credential.GetEndDateTime() != nil {
appCredentialMetrics.AddTime(prometheus.Labels{
"appAppID": appId,
"credentialName": to.String(credential.GetDisplayName()),
"credentialID": strings.ToLower(credential.GetKeyId().String()),
"credentialType": "key",
"type": "endDate",
}, credential.GetEndDateTime().UTC())
}
}
return true
})
if err != nil {
m.Logger().Panic(err)
}
}