Skip to content

Commit 6a1ec81

Browse files
committed
add license metrics #33
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 3001d53 commit 6a1ec81

4 files changed

Lines changed: 141 additions & 22 deletions

File tree

README.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,31 @@ This will run the container locally, mapping container port 8080 to local port 8
102102

103103
## Metrics
104104

105-
| Metric | Scraper | Description |
106-
|-------------------------------------------------|--------------------|----------------------------------------------------------------------------------------------------------------------|
107-
| `pagerduty_stats` | Collector | Collector stats |
108-
| `pagerduty_api_counter` | Collector | PagerDuty api call counter |
109-
| `pagerduty_team_info` | Team | Team informations |
110-
| `pagerduty_user_info` | User | User informations |
111-
| `pagerduty_service_info` | Service | Service (per team) informations |
112-
| `pagerduty_maintenancewindow_info` | MaintanaceWindows | Maintenance window informations |
113-
| `pagerduty_maintenancewindow_status` | Maintenance window | status (start and endtime) |
114-
| `pagerduty_schedule_info` | Schedule | Schedule informations |
115-
| `pagerduty_schedule_layer_info` | Schedule | Schedule layer informations |
116-
| `pagerduty_schedule_layer_entry` | Schedule | Schedule layer schedule entries |
117-
| `pagerduty_schedule_layer_coverage` | Schedule | Schedule layer schedule coverage |
118-
| `pagerduty_schedule_final_entry` | Schedule | Schedule final (rendered) schedule entries |
119-
| `pagerduty_schedule_final_coverage` | Schedule | Schedule final (rendered) schedule coverage |
120-
| `pagerduty_schedule_override` | Schedule | Schedule override informations |
121-
| `pagerduty_schedule_oncall` | Oncall | Schedule oncall informations |
122-
| `pagerduty_incident_info` | Incident | Incident informations |
123-
| `pagerduty_incident_status` | Incident | Incident status informations (acknowledgement, assignment) |
124-
| `pagerduty_summary_incident_count` | Summary | Count of incidents splitted by status, service, urgency and priority |
125-
| `pagerduty_summary_incident_resolve_duration` | Summary | Histogram (buckets) for resolve duration splitted by service, urgency and priority |
126-
| `pagerduty_summary_incident_statuschange_count` | Summary | Counter for new or changed status (eg triggered -> acknowledged) incidents splitted by service, urgency and priority |
105+
| Metric | Scraper | Description |
106+
|--------------------------------------------------|-------------------|----------------------------------------------------------------------------------------------------------------------|
107+
| `pagerduty_stats` | Collector | Collector stats |
108+
| `pagerduty_api_counter` | Collector | PagerDuty api call counter |
109+
| `pagerduty_team_info` | Team | Team information |
110+
| `pagerduty_user_info` | User | User information |
111+
| `pagerduty_service_info` | Service | Service (per team) information |
112+
| `pagerduty_maintenancewindow_info` | MaintenanceWindow | Maintenance window information |
113+
| `pagerduty_maintenancewindow_status` | MaintenanceWindow | status (start and endtime) |
114+
| `pagerduty_schedule_info` | Schedule | Schedule information |
115+
| `pagerduty_schedule_layer_info` | Schedule | Schedule layer information |
116+
| `pagerduty_schedule_layer_entry` | Schedule | Schedule layer schedule entries |
117+
| `pagerduty_schedule_layer_coverage` | Schedule | Schedule layer schedule coverage |
118+
| `pagerduty_schedule_final_entry` | Schedule | Schedule final (rendered) schedule entries |
119+
| `pagerduty_schedule_final_coverage` | Schedule | Schedule final (rendered) schedule coverage |
120+
| `pagerduty_schedule_override` | Schedule | Schedule override information |
121+
| `pagerduty_schedule_oncall` | Oncall | Schedule oncall information |
122+
| `pagerduty_incident_info` | Incident | Incident information |
123+
| `pagerduty_incident_status` | Incident | Incident status information (acknowledgement, assignment) |
124+
| `pagerduty_summary_incident_count` | Summary | Count of incidents splitted by status, service, urgency and priority |
125+
| `pagerduty_summary_incident_resolve_duration` | Summary | Histogram (buckets) for resolve duration splitted by service, urgency and priority |
126+
| `pagerduty_summary_incident_statuschange_count` | Summary | Counter for new or changed status (eg triggered -> acknowledged) incidents splitted by service, urgency and priority |
127+
| `pagerduty_system_license_info` | System | License information |
128+
| `pagerduty_system_license_current` | System | Current value of license |
129+
| `pagerduty_system_license_allocations_available` | System | Allocations available (max value) of license |
127130

128131
Prometheus queries
129132
------------------

config/opts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type (
6363
Team *time.Duration `long:"scrape.time.team" env:"SCRAPE_TIME_TEAM" description:"Scrape time for team metrics (time.duration; default is SCRAPE_TIME)"`
6464
User *time.Duration `long:"scrape.time.user" env:"SCRAPE_TIME_USER" description:"Scrape time for user metrics (time.duration; default is SCRAPE_TIME)"`
6565
Summary time.Duration `long:"scrape.time.summary" env:"SCRAPE_TIME_SUMMARY" description:"Scrape time for general summary metrics (time.duration)" default:"15m"`
66+
System time.Duration `long:"scrape.time.system" env:"SCRAPE_TIME_SYSTEM" description:"Scrape time for general system (time.duration)" default:"15m"`
6667
Live time.Duration `long:"scrape.time.live" env:"SCRAPE_TIME_LIVE" description:"Scrape time incidents and oncalls (time.duration)" default:"1m"`
6768
}
6869
}

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ func initMetricCollector() {
259259
} else {
260260
logger.With(zap.String("collector", collectorName)).Infof("collector disabled")
261261
}
262+
263+
collectorName = "System"
264+
if Opts.ScrapeTime.System.Seconds() > 0 {
265+
c := collector.New(collectorName, &MetricsCollectorSystem{}, logger)
266+
c.SetScapeTime(Opts.ScrapeTime.Summary)
267+
c.SetCache(Opts.GetCachePath("system.json"), cacheTag)
268+
if err := c.Start(); err != nil {
269+
logger.Panic(err.Error())
270+
}
271+
} else {
272+
logger.With(zap.String("collector", collectorName)).Infof("collector disabled")
273+
}
262274
}
263275

264276
// start and handle prometheus handler

metrics_system.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/PagerDuty/go-pagerduty"
8+
"github.com/prometheus/client_golang/prometheus"
9+
"github.com/webdevops/go-common/prometheus/collector"
10+
)
11+
12+
type MetricsCollectorSystem struct {
13+
collector.Processor
14+
15+
prometheus struct {
16+
license *prometheus.GaugeVec
17+
licenseCurrent *prometheus.GaugeVec
18+
licenseAllocationsAvailable *prometheus.GaugeVec
19+
}
20+
}
21+
22+
func (m *MetricsCollectorSystem) Setup(collector *collector.Collector) {
23+
m.Processor.Setup(collector)
24+
25+
m.prometheus.license = prometheus.NewGaugeVec(
26+
prometheus.GaugeOpts{
27+
Name: "pagerduty_system_license_info",
28+
Help: "PagerDuty license",
29+
},
30+
[]string{
31+
"licenseID",
32+
"licenseType",
33+
"licenseName",
34+
},
35+
)
36+
m.Collector.RegisterMetricList("pagerduty_system_license", m.prometheus.license, true)
37+
38+
m.prometheus.licenseCurrent = prometheus.NewGaugeVec(
39+
prometheus.GaugeOpts{
40+
Name: "pagerduty_system_license_current",
41+
Help: "PagerDuty license current value",
42+
},
43+
[]string{
44+
"licenseID",
45+
"licenseType",
46+
"licenseName",
47+
},
48+
)
49+
m.Collector.RegisterMetricList("pagerduty_system_licenses_current", m.prometheus.licenseCurrent, true)
50+
51+
m.prometheus.licenseAllocationsAvailable = prometheus.NewGaugeVec(
52+
prometheus.GaugeOpts{
53+
Name: "pagerduty_system_license_allocations_available",
54+
Help: "PagerDuty license allocations available",
55+
},
56+
[]string{
57+
"licenseID",
58+
"licenseType",
59+
"licenseName",
60+
},
61+
)
62+
m.Collector.RegisterMetricList("pagerduty_system_license_allocations_available", m.prometheus.licenseAllocationsAvailable, true)
63+
}
64+
65+
func (m *MetricsCollectorSystem) Reset() {
66+
}
67+
68+
func (m *MetricsCollectorSystem) Collect(callback chan<- func()) {
69+
listOpts := pagerduty.ListServiceOptions{}
70+
listOpts.Limit = PagerdutyListLimit
71+
listOpts.Offset = 0
72+
73+
licenseMetricList := m.Collector.GetMetricList("pagerduty_system_license")
74+
licenseCurrentMetricList := m.Collector.GetMetricList("pagerduty_system_licenses_current")
75+
licenseAllocationsAvailableMetricList := m.Collector.GetMetricList("pagerduty_system_license_allocations_available")
76+
77+
resp, err := PagerDutyClient.ListLicensesWithContext(m.Context())
78+
if err != nil {
79+
m.Logger().Panic(err)
80+
}
81+
82+
for _, license := range resp.Licenses {
83+
foo, _ := json.Marshal(license)
84+
fmt.Println(string(foo))
85+
licenseMetricList.AddInfo(prometheus.Labels{
86+
"licenseID": license.ID,
87+
"licenseType": license.Type,
88+
"licenseName": license.Name,
89+
})
90+
91+
licenseCurrentMetricList.Add(prometheus.Labels{
92+
"licenseID": license.ID,
93+
"licenseType": license.Type,
94+
"licenseName": license.Name,
95+
}, float64(license.CurrentValue))
96+
97+
licenseAllocationsAvailableMetricList.Add(prometheus.Labels{
98+
"licenseID": license.ID,
99+
"licenseType": license.Type,
100+
"licenseName": license.Name,
101+
}, float64(license.AllocationsAvailable))
102+
}
103+
}

0 commit comments

Comments
 (0)