Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #10 from jhoglin/master
Browse files Browse the repository at this point in the history
Ignore exposed ports during explicit discovery mode
  • Loading branch information
amouat committed Aug 31, 2020
2 parents 35e8852 + 4e85bad commit 35c3fe4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Usage:
Flags:
-c, --clean Disconnects unused networks from the Prometheus container, and deletes them. (default true)
-d, --discovery Discovery time: implicit, explicit. Implicit scans all the services found while explicit scans only labeled services. (default "implicit")
-i, --interval int The interval, in seconds, at which the discovery process is kicked off (default 30)
-l, --loglevel string Specify log level: debug, info, warn, error (default "info")
-o, --output string Output file that contains the Prometheus endpoints. (default "swarm-endpoints.json")
Expand Down
14 changes: 9 additions & 5 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ type scrapeTask struct {

// collectPorts builds a map of ports collected from container exposed ports and/or from ports defined
// as container labels
func collectPorts(task swarm.Task, serviceIDMap map[string]swarm.Service) map[int]struct{} {
func collectPorts(task swarm.Task, serviceIDMap map[string]swarm.Service, discoveryType string) map[int]struct{} {

ports := make(map[int]struct{})

Expand All @@ -180,9 +180,13 @@ func collectPorts(task swarm.Task, serviceIDMap map[string]swarm.Service) map[in
}
}

// collects exposed ports
for _, port := range serviceIDMap[task.ServiceID].Spec.EndpointSpec.Ports {
ports[int(port.TargetPort)] = struct{}{}
// collects exposed ports, but only if we use implicit discovery
if discoveryType == implicit {
for _, port := range serviceIDMap[task.ServiceID].Spec.EndpointSpec.Ports {
ports[int(port.TargetPort)] = struct{}{}
}
} else {
logger.Debugf("Exposed ports on Service %s ignored by Prometheus", task.ServiceID)
}

return ports
Expand Down Expand Up @@ -270,7 +274,7 @@ func discoverSwarm(prometheusContainerID string, outputFile string, discoveryTyp
}
}

ports := collectPorts(task, serviceIDMap)
ports := collectPorts(task, serviceIDMap, discoveryType)
containerIPs, taskNetworks := collectIPs(task)
var taskEndpoints []string

Expand Down

0 comments on commit 35c3fe4

Please sign in to comment.