Skip to content

Commit

Permalink
Add container ID tags to prometheus metrics (dcos#34)
Browse files Browse the repository at this point in the history
* Update container IDs to memorable values

* Add method for extracting container id from task

* Update tests with container IDs

* Add container ID to metrics
  • Loading branch information
philipnrmn authored and branden committed Feb 11, 2019
1 parent 6190eda commit 6be15a2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
58 changes: 47 additions & 11 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,8 @@ func (p *Prometheus) GetAllURLs() (map[string]URLAndAddress, error) {
return allURLs, err
}

for _, service := range getMesosTaskPrometheusURLs(tasks) {
URL, err := url.Parse(service)
if err != nil {
log.Printf("E! %s", err)
continue
}
allURLs = append(allURLs, URLAndAddress{URL: URL, OriginalURL: URL})
for _, url := range getMesosTaskPrometheusURLs(tasks) {
allURLs[url.URL.String()] = url
}
}

Expand Down Expand Up @@ -452,19 +447,39 @@ func processResponse(resp mesos.Response, t agent.Response_Type) (agent.Response

// getMesosTaskPrometheusURLs converts a list of tasks to a list of Prometheus
// URLs to scrape
func getMesosTaskPrometheusURLs(tasks *agent.Response_GetTasks) []string {
results := []string{}
func getMesosTaskPrometheusURLs(tasks *agent.Response_GetTasks) []URLAndAddress {
results := []URLAndAddress{}
for _, t := range tasks.GetLaunchedTasks() {
for _, endpoint := range getEndpointsFromTaskPorts(&t) {
results = append(results, endpoint)
uat, err := makeURLAndAddress(t, endpoint)
if err != nil {
log.Printf("E! %s", err)
continue
}
results = append(results, uat)
}
if endpoint, ok := getEndpointFromTaskLabels(&t); ok {
results = append(results, endpoint)
uat, err := makeURLAndAddress(t, endpoint)
if err != nil {
log.Printf("E! %s", err)
continue
}
results = append(results, uat)
}
}
return results
}

func makeURLAndAddress(task mesos.Task, endpoint string) (URLAndAddress, error) {
URL, err := url.Parse(endpoint)
cid, _ := getContainerIDs(task.GetStatuses())
return URLAndAddress{
URL: URL,
OriginalURL: URL,
Tags: map[string]string{"container_id": cid},
}, err
}

// getEndpointsFromTaskPorts retrieves a map of ports end enpoints from which
// Prometheus metrics can be retrieved from a given task.
func getEndpointsFromTaskPorts(t *mesos.Task) []string {
Expand Down Expand Up @@ -519,6 +534,27 @@ func getPortsFromTask(t *mesos.Task) []mesos.Port {
return []mesos.Port{}
}

// getContainerIDs retrieves the container ID and the parent container ID of a
// task from its TaskStatus. The container ID corresponds to the task's
// container, the parent container ID corresponds to the task's executor's
// container.
func getContainerIDs(statuses []mesos.TaskStatus) (containerID string, parentContainerID string) {
// Container ID is held in task status
for _, s := range statuses {
if cs := s.GetContainerStatus(); cs != nil {
if cid := cs.GetContainerID(); cid != nil {
containerID = cid.GetValue()
if pcid := cid.GetParent(); pcid != nil {
parentContainerID = pcid.GetValue()
return
}
return
}
}
}
return
}

// simplifyLabels converts a Labels object to a hashmap
func simplifyLabels(ll *mesos.Labels) map[string]string {
results := map[string]string{}
Expand Down
26 changes: 19 additions & 7 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,26 @@ func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) {
func TestPrometheusGathersMesosMetrics(t *testing.T) {
metricsUrl, _ := url.Parse("http://localhost:12345/metrics")
federateUrl, _ := url.Parse("http://localhost:12345/federate")
testCases := map[string][]URLAndAddress{
"empty": []URLAndAddress{},
"portlabel": []URLAndAddress{
URLAndAddress{URL: metricsUrl, OriginalURL: metricsUrl},
URLAndAddress{URL: federateUrl, OriginalURL: federateUrl},
testCases := map[string]map[string]URLAndAddress{
"empty": map[string]URLAndAddress{},
"portlabel": map[string]URLAndAddress{
metricsUrl.String(): URLAndAddress{
URL: metricsUrl,
OriginalURL: metricsUrl,
Tags: map[string]string{"container_id": "abc-123"},
},
federateUrl.String(): URLAndAddress{
URL: federateUrl,
OriginalURL: federateUrl,
Tags: map[string]string{"container_id": "xyz-123"},
},
},
"tasklabel": []URLAndAddress{
URLAndAddress{URL: metricsUrl, OriginalURL: metricsUrl},
"tasklabel": map[string]URLAndAddress{
metricsUrl.String(): URLAndAddress{
URL: metricsUrl,
OriginalURL: metricsUrl,
Tags: map[string]string{"container_id": "abc-123"},
},
},
}
for scenario, expected := range testCases {
Expand Down
Binary file modified plugins/inputs/prometheus/testdata/portlabel/tasks.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions plugins/inputs/prometheus/testdata/portlabel/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
"healthy": true,
"container_status": {
"container_id": {
"value": "6c9624d4-c98a-48cd-8c69-fd4994058d95"
"value": "abc-123"
},
"network_infos": [
{
Expand Down Expand Up @@ -314,7 +314,7 @@
"healthy": true,
"container_status": {
"container_id": {
"value": "6c9624d4-c98a-48cd-8c69-fd4994058d95"
"value": "xyz-123"
},
"network_infos": [
{
Expand Down
Binary file modified plugins/inputs/prometheus/testdata/tasklabel/tasks.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion plugins/inputs/prometheus/testdata/tasklabel/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
"healthy": true,
"container_status": {
"container_id": {
"value": "6c9624d4-c98a-48cd-8c69-fd4994058d95"
"value": "abc-123"
},
"network_infos": [
{
Expand Down

0 comments on commit 6be15a2

Please sign in to comment.