1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:24:41 -07:00

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 <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Nathan Dias 2020-03-23 22:14:59 -05:00 committed by Emmanuel Odeke
parent a576cf5246
commit dbf25ea225

View File

@ -36,25 +36,39 @@ import (
"net/http" "net/http"
"time" "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/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(), Start: time.Now(),
Address: "http://127.0.0.1:55678", Address: "http://127.0.0.1:55678",
Service: "go-tools-test", Service: "go-tools-test",
Rate: 5 * time.Second, Rate: 5 * time.Second,
Client: &http.Client{}, Client: &http.Client{},
}) })
export.SetExporter(exporter) event.SetExporter(exporter)
ctx := context.TODO() ctx := context.TODO()
mLatency := event.NewFloat64Key("latency", "the latency in milliseconds", "ms") mLatency := event.NewFloat64Key("latency", "the latency in milliseconds")
distribution := metric.HistogramFloat64Data{ distribution := metric.HistogramFloat64Data{
Info: &metric.HistogramFloat64{ Info: &metric.HistogramFloat64{
Name: "latencyDistribution", Name: "latencyDistribution",
@ -63,12 +77,12 @@ func main() {
}, },
} }
distribution.Info.Record(mLatency) distribution.Info.Record(&exporter.metrics, mLatency)
for { for {
sleep := randomSleep() sleep := randomSleep()
time.Sleep(time.Duration(sleep) * time.Millisecond) time.Sleep(time.Duration(sleep) * time.Millisecond)
mLatency.Record(ctx, float64(sleep)) event.Record(ctx, mLatency.Of(float64(sleep)))
fmt.Println("Latency: ", float64(sleep)) fmt.Println("Latency: ", float64(sleep))
} }