Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds task event counter #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
| nomad_task_cpu_total_ticks | Task CPU total ticks | job, group, alloc, task, region, datacenter, node |
| nomad_task_cpu_percent | Task CPU usage, percent | job, group, alloc, task, region, datacenter, node |
| nomad_task_memory_rss_bytes | Task memory RSS usage, bytes | job, group, alloc, task, region, datacenter, node |
| nomad_task_events | Task events counts | job, group, name, alloc, task, region, datacenter, node, event |
| nomad_node_resource_memory_megabytes | Amount of allocatable memory the node has in MB | node, datacenter |
| nomad_node_allocated_memory_megabytes | Amount of memory allocated to tasks on the node in MB | node, datacenter |
| nomad_node_used_memory_megabytes | Amount of memory used on the node in MB | node, datacenter |
Expand Down
44 changes: 42 additions & 2 deletions nomad-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ var (
"Task memory RSS usage, bytes",
[]string{"job", "group", "alloc", "task", "region", "datacenter", "node"}, nil,
)
taskEvents = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "nomad_task_events",
Help: "Task events count",
ConstLabels: prometheus.Labels{},
},
[]string{"job", "group", "name", "alloc", "task", "region", "datacenter", "node", "event"},
)
nodeResourceMemory = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "node_resource_memory_megabytes"),
"Amount of allocatable memory the node has in MB",
Expand Down Expand Up @@ -125,7 +133,8 @@ func AllocationsByStatus(allocs []*api.AllocationListStub, status string) []*api
}

type Exporter struct {
client *api.Client
client *api.Client
lastScrapeTime time.Time
}

func NewExporter(cfg *api.Config) (*Exporter, error) {
Expand All @@ -134,7 +143,8 @@ func NewExporter(cfg *api.Config) (*Exporter, error) {
return nil, err
}
return &Exporter{
client: client,
client: client,
lastScrapeTime: time.Now(),
}, nil
}

Expand All @@ -158,10 +168,14 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- nodeResourceCPU
ch <- nodeAllocatedCPU
ch <- nodeUsedCPU

taskEvents.Describe(ch)
}

// Collect collects nomad metrics
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
scrapeTime := time.Now()

peers, err := e.client.Status().Peers()
if err != nil {
ch <- prometheus.MustNewConstMetric(
Expand Down Expand Up @@ -205,6 +219,30 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
)

var w sync.WaitGroup
for _, a := range allocs {
w.Add(1)
go func(a *api.AllocationListStub) {
defer w.Done()
alloc, _, err := e.client.Allocations().Info(a.ID, &api.QueryOptions{})
if err != nil {
logError(err)
return
}
node, _, err := e.client.Nodes().Info(alloc.NodeID, &api.QueryOptions{})
if err != nil {
logError(err)
return
}
for taskName, taskState := range alloc.TaskStates {
for _, taskEvent := range taskState.Events {
if taskEvent.Time > e.lastScrapeTime.UnixNano() && taskEvent.Time <= scrapeTime.UnixNano() {
taskEvents.WithLabelValues(alloc.Job.Name, alloc.TaskGroup, alloc.Name, alloc.ID, taskName, alloc.Job.Region, node.Datacenter, node.Name, taskEvent.Type).Inc()
}
}
}
}(a)
}

for _, a := range runningAllocs {
w.Add(1)
go func(a *api.AllocationListStub) {
Expand Down Expand Up @@ -300,6 +338,8 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
}(a)
}
w.Wait()
taskEvents.Collect(ch)
e.lastScrapeTime = scrapeTime
}

func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, error) {
Expand Down