From dbf25ea225ceda21b057bd41c955a866646560ca Mon Sep 17 00:00:00 2001 From: Nathan Dias Date: Mon, 23 Mar 2020 22:14:59 -0500 Subject: [PATCH] internal/telemetry/export/ocagent: update metrics tutorial to use the event system This change updates the metrics tutorial to be compatible with the new event system. Change-Id: I8b75f6b02d241bc9dede01cfac167a982f1df67c Reviewed-on: https://go-review.googlesource.com/c/tools/+/225058 Reviewed-by: Emmanuel Odeke Run-TryBot: Emmanuel Odeke TryBot-Result: Gobot Gobot --- internal/telemetry/export/ocagent/README.md | 32 +++++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/internal/telemetry/export/ocagent/README.md b/internal/telemetry/export/ocagent/README.md index e81ee9faf7..67fa124937 100644 --- a/internal/telemetry/export/ocagent/README.md +++ b/internal/telemetry/export/ocagent/README.md @@ -36,25 +36,39 @@ import ( "net/http" "time" - "golang.org/x/tools/internal/telemetry/export" - "golang.org/x/tools/internal/telemetry/export/ocagent" - "golang.org/x/tools/internal/telemetry/export/metric" "golang.org/x/tools/internal/telemetry/event" + "golang.org/x/tools/internal/telemetry/export" + "golang.org/x/tools/internal/telemetry/export/metric" + "golang.org/x/tools/internal/telemetry/export/ocagent" ) -func main() { +type testExporter struct { + metrics metric.Exporter + ocagent *ocagent.Exporter +} - exporter := ocagent.Connect(&ocagent.Config{ +func (e *testExporter) ProcessEvent(ctx context.Context, ev event.Event) (context.Context, event.Event) { + ctx, ev = export.Tag(ctx, ev) + ctx, ev = export.ContextSpan(ctx, ev) + ctx, ev = e.metrics.ProcessEvent(ctx, ev) + ctx, ev = e.ocagent.ProcessEvent(ctx, ev) + return ctx, ev +} + +func main() { + exporter := &testExporter{} + + exporter.ocagent = ocagent.Connect(&ocagent.Config{ Start: time.Now(), Address: "http://127.0.0.1:55678", Service: "go-tools-test", Rate: 5 * time.Second, Client: &http.Client{}, }) - export.SetExporter(exporter) + event.SetExporter(exporter) ctx := context.TODO() - mLatency := event.NewFloat64Key("latency", "the latency in milliseconds", "ms") + mLatency := event.NewFloat64Key("latency", "the latency in milliseconds") distribution := metric.HistogramFloat64Data{ Info: &metric.HistogramFloat64{ Name: "latencyDistribution", @@ -63,12 +77,12 @@ func main() { }, } - distribution.Info.Record(mLatency) + distribution.Info.Record(&exporter.metrics, mLatency) for { sleep := randomSleep() time.Sleep(time.Duration(sleep) * time.Millisecond) - mLatency.Record(ctx, float64(sleep)) + event.Record(ctx, mLatency.Of(float64(sleep))) fmt.Println("Latency: ", float64(sleep)) }