// Stop implements the TargetProvider interface. func (dd *DNSDiscovery) Stop() { log.Debug("Stopping DNS discovery for %s...", dd.names) dd.ticker.Stop() dd.done <- struct{}{} log.Debug("DNS discovery for %s stopped.", dd.names) }
// Run implements the TargetProvider interface. func (md *MarathonDiscovery) Run(ch chan<- *config.TargetGroup) { defer close(ch) for { select { case <-md.done: log.Debug("Shutting down marathon discovery.") return case <-time.After(md.refreshInterval): err := md.updateServices(ch) if err != nil { log.Errorf("Error while updating services: %s", err) } } } }
func (e *periodicExporter) updateSlaves() { log.Debug("discovering slaves...") // This will redirect us to the elected mesos master redirectURL := fmt.Sprintf("%s://%s/master/redirect", e.queryURL.Scheme, e.queryURL.Host) rReq, err := http.NewRequest("GET", redirectURL, nil) if err != nil { panic(err) } tr := http.Transport{ DisableKeepAlives: true, } rresp, err := tr.RoundTrip(rReq) if err != nil { log.Warn(err) return } defer rresp.Body.Close() // This will/should return http://master.ip:5050 masterLoc := rresp.Header.Get("Location") if masterLoc == "" { log.Warnf("%d response missing Location header", rresp.StatusCode) return } log.Debugf("current elected master at: %s", masterLoc) // Starting from 0.23.0, a Mesos Master does not set the scheme in the "Location" header. // Use the scheme from the master URL in this case. var stateURL string if strings.HasPrefix(masterLoc, "http") { stateURL = fmt.Sprintf("%s/master/state.json", masterLoc) } else { stateURL = fmt.Sprintf("%s:%s/master/state.json", e.queryURL.Scheme, masterLoc) } var state masterState // Find all active slaves err = getJSON(&state, stateURL) if err != nil { log.Warn(err) return } var slaveURLs []string for _, slave := range state.Slaves { if slave.Active { // Extract slave port from pid _, port, err := net.SplitHostPort(slave.PID) if err != nil { port = "5051" } url := fmt.Sprintf("http://%s:%s", slave.Hostname, port) slaveURLs = append(slaveURLs, url) } } log.Debugf("%d slaves discovered", len(slaveURLs)) e.slaves.Lock() e.slaves.urls = slaveURLs e.slaves.Unlock() }
func (a *newRelicApi) req(path string, params string) ([]byte, error) { u := a.server u.Path = path u.RawQuery = params log.Debug("Making API call: ", u.String()) req := &http.Request{ Method: "GET", URL: &u, Header: http.Header{ "User-Agent": {UserAgent}, "X-Api-Key": {a.apiKey}, }, } client := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: TlsIgnore, }, }, } var data []byte pageCount := 1 for page := 1; page <= pageCount; page++ { resp, err := client.Do(req) if err != nil { return nil, err } if resp.StatusCode != 200 { return nil, fmt.Errorf("Bad response code: %s", resp.Status) } body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } resp.Body.Close() data = append(data, body...) link := resp.Header.Get("Link") vals := LinkRexp.FindStringSubmatch(link) if len(vals) == 3 { // Full string plus two sub-expressions u, err := url.Parse(vals[2]) // Parse the second URL if err != nil { return nil, err } pageCount, err = strconv.Atoi(u.Query().Get("page")) if err != nil { return nil, err } } qry := req.URL.Query() qry.Set("page", strconv.Itoa(page+1)) req.URL.RawQuery = qry.Encode() } return data, nil }
func (e *periodicExporter) updateSlaves() { log.Debug("discovering slaves...") // This will redirect us to the elected mesos master redirectURL := fmt.Sprintf("%s/master/redirect", e.opts.masterURL) rReq, err := http.NewRequest("GET", redirectURL, nil) if err != nil { panic(err) } tr := http.Transport{ DisableKeepAlives: true, } rresp, err := tr.RoundTrip(rReq) if err != nil { log.Warn(err) return } defer rresp.Body.Close() // This will/should return http://master.ip:5050 masterLoc := rresp.Header.Get("Location") if masterLoc == "" { log.Warnf("%d response missing Location header", rresp.StatusCode) return } log.Debugf("current elected master at: %s", masterLoc) // Find all active slaves stateURL := fmt.Sprintf("%s/master/state.json", masterLoc) resp, err := http.Get(stateURL) if err != nil { log.Warn(err) return } defer resp.Body.Close() type slave struct { Active bool `json:"active"` Hostname string `json:"hostname"` Pid string `json:"pid"` } var req struct { Slaves []*slave `json:"slaves"` } if err := json.NewDecoder(resp.Body).Decode(&req); err != nil { log.Warnf("failed to deserialize request: %s", err) return } var slaveURLs []string for _, slave := range req.Slaves { if slave.Active { // Extract slave port from pid _, port, err := net.SplitHostPort(slave.Pid) if err != nil { port = "5051" } url := fmt.Sprintf("http://%s:%s", slave.Hostname, port) slaveURLs = append(slaveURLs, url) } } log.Debugf("%d slaves discovered", len(slaveURLs)) e.slaves.Lock() e.slaves.urls = slaveURLs e.slaves.Unlock() }