Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
EventMetrics per response body
Browse files Browse the repository at this point in the history
  • Loading branch information
mfboulos committed Nov 25, 2019
1 parent 71efa3f commit 54cd061
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 65 deletions.
14 changes: 8 additions & 6 deletions probes/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ type Probe struct {
// (runCnt % statsExportFrequency) == 0
statsExportFrequency int64

// default payload metrics that we clone from to build per-target payload
// metrics.
// parser for reading metrics from response body
payloadParser *payload.Parser
}

Expand Down Expand Up @@ -168,7 +167,8 @@ func (p *Probe) Init(name string, opts *options.Options) error {
p.targetsUpdateFrequency = 1
}

parser, err := payload.NewParser(/*OutputMetricsOptions?*/, "http", p.name, metrics.Kind(defaultKind), p.l)
defaultKind := metrics.CUMULATIVE
parser, err := payload.NewParser(p.c.GetOutputMetricsOptions(), "http", p.name, metrics.Kind(defaultKind), p.l)
if err != nil {
return fmt.Errorf("error initializing payload metrics: %v", err)
}
Expand Down Expand Up @@ -235,11 +235,15 @@ func (p *Probe) doHTTPRequest(req *http.Request, result *probeResult, resultMu *

result.success++
result.latency.AddFloat64(latency.Seconds() / p.opts.LatencyUnit.Seconds())
if p.c.GetExportResponseAsMetrics() {
if p.c.GetExportResponseCountAsMetric() {
if len(respBody) <= maxResponseSizeForMetrics {
result.respBodies.IncKey(string(respBody))
}
}
if p.c.GetExportResponseAsMetrics() {
// Parse response body as metrics
result.responseMetrics = p.payloadParser.PayloadMetrics(result.responseMetrics, string(respBody), req.Host)
}
}

func (p *Probe) updateTargets() {
Expand Down Expand Up @@ -346,8 +350,6 @@ func (p *Probe) Start(ctx context.Context, dataChan chan *metrics.EventMetrics)
p.dataChan <- em

if p.c.GetExportResponseAsMetrics() {
result.responseMetrics = p.payloadParser.PayloadMetrics(result.responseMetrics, "" /*response?*/, target.Name)
p.opts.LogMetrics(result.responseMetrics)
p.dataChan <- result.responseMetrics
}
}
Expand Down
41 changes: 36 additions & 5 deletions probes/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,30 @@ func TestProbeVariousMethods(t *testing.T) {
}

func TestProbeWithBody(t *testing.T) {

testBody := "TestHTTPBody"
testBody := `TestHTTPBody "val123"`
testTarget := "test.com"
// Build the expected response code map
expectedMap := metrics.NewMap("resp", metrics.NewInt(0))
expectedMap.IncKey(testBody)
expected := expectedMap.String()
// Build the expected metrics
testBodyLines := strings.Split(testBody, "\n")
expectedMetrics := make(map[string]metrics.Value)
for _, line := range testBodyLines {
lineSplit := strings.Fields(line)
if len(lineSplit) == 2 {
expectedMetrics[lineSplit[0]], _ = metrics.ParseValueFromString(lineSplit[1])
}
}

p := &Probe{}
err := p.Init("http_test", &options.Options{
Targets: targets.StaticTargets(testTarget),
Interval: 2 * time.Second,
ProbeConf: &configpb.ProbeConf{
Body: &testBody,
ExportResponseAsMetrics: proto.Bool(true),
Body: &testBody,
ExportResponseCountAsMetric: proto.Bool(true),
ExportResponseAsMetrics: proto.Bool(true),
},
})

Expand All @@ -182,15 +191,37 @@ func TestProbeWithBody(t *testing.T) {
if got != expected {
t.Errorf("response map: got=%s, expected=%s", got, expected)
}
for key, val := range expectedMetrics {
metrics := p.results[testTarget].responseMetrics
if metrics != nil {
actualMetric := metrics.Metric(key)
if actualMetric != val {
t.Errorf("metric %s: got=%s, expected=%s", key, actualMetric, val)
}
} else {
t.Errorf("metric %s: not found, expected=%s", key, val)
}
}

// Probe 2nd run (we should get the same request body).
// Probe 2nd run (we should get the same request body)
p.runProbe(context.Background())
expectedMap.IncKey(testBody)
expected = expectedMap.String()
got = p.results[testTarget].respBodies.String()
if got != expected {
t.Errorf("response map: got=%s, expected=%s", got, expected)
}
for key, val := range expectedMetrics {
metrics := p.results[testTarget].responseMetrics
if metrics != nil {
actualMetric := metrics.Metric(key)
if actualMetric != val {
t.Errorf("metric %s: got=%s, expected=%s", key, actualMetric, val)
}
} else {
t.Errorf("metric %s: not found, expected=%s", key, val)
}
}
}

func TestMultipleTargetsMultipleRequests(t *testing.T) {
Expand Down
123 changes: 75 additions & 48 deletions probes/http/proto/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions probes/http/proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package cloudprober.probes.http;

import "github.com/google/cloudprober/metrics/payload/proto/config.proto";

// Next tag: 17
// Next tag: 18
message ProbeConf {
enum ProtocolType {
HTTP = 0;
Expand Down Expand Up @@ -44,7 +44,7 @@ message ProbeConf {
optional bool resolve_first = 4 [default = false];

// Export response (body) count as a metric
optional bool export_response_as_metrics = 5 [default = false];
optional bool export_response_count_as_metric = 5 [default = false];

// HTTP request method
optional Method method = 7 [default = GET];
Expand Down Expand Up @@ -80,9 +80,9 @@ message ProbeConf {
optional int32 requests_interval_msec = 99 [default = 25];

// Output metrics options, where output is the response body returned by the
// http probe process, over stdout for ONCE probes, and through ProbeReply
// for SERVER probes. Cloudprober expects variables to be in the following
// format in the output:
// HTTP probe process. Cloudprober expects variables to be in the following
// format in the response body:
// var1 value1 (for example: total_errors 589)
optional metrics.payload.OutputMetricsOptions output_metrics_options = 16;
optional bool export_response_as_metrics = 16 [default = false];
optional metrics.payload.OutputMetricsOptions output_metrics_options = 17;
}

0 comments on commit 54cd061

Please sign in to comment.