Skip to content
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
65 changes: 63 additions & 2 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,17 +608,78 @@ func getPortsFromTask(t *mesos.Task) []mesos.Port {
// and set to container, it returns the task's IP address. If absent, or set to
// host, it returns the node's hostname.
func getHostnameForPort(p *mesos.Port, t *mesos.Task, nodeHostname string) (string, error) {
portLabels := simplifyLabels(p.GetLabels())
if portLabels["network-scope"] == "container" {
if !isHostPort(p, t) {
taskIP, err := getTaskIP(t.GetStatuses())
if err != nil {
return nodeHostname, fmt.Errorf("could not retrieve IP address for %s: %s", t.GetTaskID(), err)
}
return taskIP, nil

}

return nodeHostname, nil
}

func isHostPort(p *mesos.Port, t *mesos.Task) bool {

// Handle network scope label
portLabels := simplifyLabels(p.GetLabels())
if s, ok := portLabels["network-scope"]; ok {
if s == "host" {
return true
}

return false
}

// Handle task resources
for _, r := range t.GetResources() {
if n := r.GetName(); n == "ports" {
for _, pr := range r.GetRanges().GetRange() {
if pr.GetBegin() <= uint64(p.Number) && pr.GetEnd() >= uint64(p.Number) {
return true
}
}

if r.GetScalar().GetValue() == float64(p.Number) {
return true
}
}
}

// Handle Pod portmappings
for _, s := range t.GetStatuses() {
for _, ni := range s.GetContainerStatus().GetNetworkInfos() {
for _, pm := range ni.GetPortMappings() {
if h := pm.GetHostPort(); h == p.Number {
return true
}
}
}
}

// Handle MESOS and Docker portmappings
if c := t.GetContainer(); c != nil {
for _, ni := range c.GetNetworkInfos() {
for _, pm := range ni.GetPortMappings() {
if h := pm.GetHostPort(); h == p.Number {
return true
}
}
}

if d := c.GetDocker(); d != nil {
for _, pm := range d.GetPortMappings() {
if h := pm.GetHostPort(); h == p.Number {
return true
}
}
}
}

return false
}

// 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
Expand Down
73 changes: 58 additions & 15 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,66 @@ func TestPrometheusGathersMesosMetrics(t *testing.T) {
},
},
"networkModes": {
// Host mode should use hostname:port
"http://127.0.0.1:7070/metrics": {
URL: unsafelyParse("http://127.0.0.1:7070/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:7070/metrics"),
Tags: map[string]string{"container_id": "host-123"},
"http://127.0.0.1:85254/metrics": {
URL: unsafelyParse("http://127.0.0.1:85254/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:85254/metrics"),
Tags: map[string]string{"container_id": "mapped-pod-labeled-container-id"},
},
"http://127.0.0.1:25854/metrics": {
URL: unsafelyParse("http://127.0.0.1:25854/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:25854/metrics"),
Tags: map[string]string{"container_id": "mapped-pod-container-id"},
},
// Bridge mode should use hostname:mapped-port (not hostname:container-port)
"http://127.0.0.1:8080/metrics": {
URL: unsafelyParse("http://127.0.0.1:8080/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:8080/metrics"),
Tags: map[string]string{"container_id": "bridge-123"},
},
// Container mode should use task-ip:container-port
"http://198.0.2.3:9090/metrics": {
URL: unsafelyParse("http://198.0.2.3:9090/metrics"),
OriginalURL: unsafelyParse("http://198.0.2.3:9090/metrics"),
Tags: map[string]string{"container_id": "container-123"},
"http://127.0.0.1:10388/metrics": {
URL: unsafelyParse("http://127.0.0.1:10388/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:10388/metrics"),
Tags: map[string]string{"container_id": "host-docker-container-id"},
},
"http://127.0.0.1:8486/metrics": {
URL: unsafelyParse("http://127.0.0.1:8486/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:8486/metrics"),
Tags: map[string]string{"container_id": "host-mesos-container-id"},
},
"http://9.0.4.4:85254/metrics": {
URL: unsafelyParse("http://9.0.4.4:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.4.4:85254/metrics"),
Tags: map[string]string{"container_id": "mixed-pod-labeled-container-id"},
},
"http://9.0.4.3:85254/metrics": {
URL: unsafelyParse("http://9.0.4.3:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.4.3:85254/metrics"),
Tags: map[string]string{"container_id": "mixed-pod-container-id"},
},
"http://9.0.3.9:85254/metrics": {
URL: unsafelyParse("http://9.0.3.9:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.3.9:85254/metrics"),
Tags: map[string]string{"container_id": "mixed-mesos-container-id"},
},
"http://9.0.4.9:85254/metrics": {
URL: unsafelyParse("http://9.0.4.9:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.4.9:85254/metrics"),
Tags: map[string]string{"container_id": "mixed-mesos-labeled-container-id"},
},
"http://9.0.2.3:85254/metrics": {
URL: unsafelyParse("http://9.0.2.3:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.2.3:85254/metrics"),
Tags: map[string]string{"container_id": "container-pod-container-id"},
},
"http://9.0.2.4:85254/metrics": {
URL: unsafelyParse("http://9.0.2.4:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.2.4:85254/metrics"),
Tags: map[string]string{"container_id": "container-pod-labeled-container-id"},
},
"http://9.0.0.0:85254/metrics": {
URL: unsafelyParse("http://9.0.0.0:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.0.0:85254/metrics"),
Tags: map[string]string{"container_id": "container-mesos-container-id"},
},
"http://9.0.0.1:85254/metrics": {
URL: unsafelyParse("http://9.0.0.1:85254/metrics"),
OriginalURL: unsafelyParse("http://9.0.0.1:85254/metrics"),
Tags: map[string]string{"container_id": "container-mesos-labeled-container-id"},
},
},
}
Expand Down
Loading