1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-16 10:19:23 +02:00
opentelemetry-go/exporter/metric/dogstatsd/example_test.go
Liz Fong-Jones 4c9bf78ab3 s/options/config/ (#410)
Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
2020-01-03 09:48:45 -08:00

74 lines
1.4 KiB
Go

package dogstatsd_test
import (
"context"
"fmt"
"io"
"log"
"sync"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/exporter/metric/dogstatsd"
)
func ExampleNew() {
// Create a "server"
wg := &sync.WaitGroup{}
wg.Add(1)
reader, writer := io.Pipe()
go func() {
defer wg.Done()
for {
var buf [4096]byte
n, err := reader.Read(buf[:])
if err == io.EOF {
return
} else if err != nil {
log.Fatal("Read err: ", err)
} else if n >= len(buf) {
log.Fatal("Read small buffer: ", n)
} else {
fmt.Print(string(buf[0:n]))
}
}
}()
// Create a meter
pusher, err := dogstatsd.NewExportPipeline(dogstatsd.Config{
// The Writer field provides test support.
Writer: writer,
// In real code, use the URL field:
//
// URL: fmt.Sprint("unix://", path),
})
if err != nil {
log.Fatal("Could not initialize dogstatsd exporter:", err)
}
ctx := context.Background()
key := key.New("key")
// pusher implements the metric.MeterProvider interface:
meter := pusher.Meter("example")
// Create and update a single counter:
counter := meter.NewInt64Counter("a.counter", metric.WithKeys(key))
labels := meter.Labels(key.String("value"))
counter.Add(ctx, 100, labels)
// Flush the exporter, close the pipe, and wait for the reader.
pusher.Stop()
writer.Close()
wg.Wait()
// Output:
// a.counter:100|c|#key:value
}