func scrapeMetrics(s *httptest.Server) ([]*prometheuspb.MetricFamily, error) { req, err := http.NewRequest("GET", s.URL+"/metrics", nil) if err != nil { return nil, fmt.Errorf("Unable to create http request: %v", err) } // Ask the prometheus exporter for its text protocol buffer format, since it's // much easier to parse than its plain-text format. Don't use the serialized // proto representation since it uses a non-standard varint delimiter between // metric families. req.Header.Add("Accept", scrapeRequestHeader) client := &http.Client{} resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("Unable to contact metrics endpoint of master: %v", err) } defer resp.Body.Close() if resp.StatusCode != 200 { return nil, fmt.Errorf("Non-200 response trying to scrape metrics from master: %v", resp) } // Each line in the response body should contain all the data for a single metric. var metrics []*prometheuspb.MetricFamily scanner := bufio.NewScanner(resp.Body) for scanner.Scan() { var metric prometheuspb.MetricFamily if err := proto.UnmarshalText(scanner.Text(), &metric); err != nil { return nil, fmt.Errorf("Failed to unmarshal line of metrics response: %v", err) } glog.Infof("Got metric %q", metric.GetName()) metrics = append(metrics, &metric) } return metrics, nil }
// Dial opens a connection to a remote server and attempts to negotiate a SPDY // connection. Upon success, it returns the connection and the protocol // selected by the server. func (e *streamExecutor) Dial(protocols ...string) (httpstream.Connection, string, error) { rt := transport.DebugWrappers(e.transport) // TODO the client probably shouldn't be created here, as it doesn't allow // flexibility to allow callers to configure it. client := &http.Client{Transport: rt} req, err := http.NewRequest(e.method, e.url.String(), nil) if err != nil { return nil, "", fmt.Errorf("error creating request: %v", err) } for i := range protocols { req.Header.Add(httpstream.HeaderProtocolVersion, protocols[i]) } resp, err := client.Do(req) if err != nil { return nil, "", fmt.Errorf("error sending request: %v", err) } defer resp.Body.Close() conn, err := e.upgrader.NewConnection(resp) if err != nil { return nil, "", err } return conn, resp.Header.Get(httpstream.HeaderProtocolVersion), nil }
func httpDelete(url string) (*http.Response, error) { req, err := http.NewRequest("DELETE", url, nil) if err != nil { return nil, err } client := &http.Client{} return client.Do(req) }
func doPut(url, content string) (string, error) { req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(content))) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return "", err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } strBody := string(body) return strBody, nil }
// Dial opens a connection to a remote server and attempts to negotiate a SPDY // connection. Upon success, it returns the connection and the protocol // selected by the server. func (e *streamExecutor) Dial(protocols ...string) (httpstream.Connection, string, error) { transport := e.transport // TODO consider removing this and reusing client.TransportFor above to get this for free switch { case bool(glog.V(9)): transport = client.NewDebuggingRoundTripper(transport, client.CurlCommand, client.URLTiming, client.ResponseHeaders) case bool(glog.V(8)): transport = client.NewDebuggingRoundTripper(transport, client.JustURL, client.RequestHeaders, client.ResponseStatus, client.ResponseHeaders) case bool(glog.V(7)): transport = client.NewDebuggingRoundTripper(transport, client.JustURL, client.RequestHeaders, client.ResponseStatus) case bool(glog.V(6)): transport = client.NewDebuggingRoundTripper(transport, client.URLTiming) } // TODO the client probably shouldn't be created here, as it doesn't allow // flexibility to allow callers to configure it. client := &http.Client{Transport: transport} req, err := http.NewRequest(e.method, e.url.String(), nil) if err != nil { return nil, "", fmt.Errorf("error creating request: %v", err) } for i := range protocols { req.Header.Add(httpstream.HeaderProtocolVersion, protocols[i]) } resp, err := client.Do(req) if err != nil { return nil, "", fmt.Errorf("error sending request: %v", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusSwitchingProtocols { return nil, "", fmt.Errorf("unexpected response status code %d (%s)", resp.StatusCode, http.StatusText(resp.StatusCode)) } conn, err := e.upgrader.NewConnection(resp) if err != nil { return nil, "", err } return conn, resp.Header.Get(httpstream.HeaderProtocolVersion), nil }