Skip to content

Commit

Permalink
switch from WithSyncer to WithBatcher in examples (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole authored Jun 16, 2021
1 parent f1ff9d3 commit 7a135d3
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ var (
)

func main() {
example.InitTracer()
tp := example.InitTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
flag.Parse()

if *brokers == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ const (
KafkaTopic = "sarama-instrumentation-example"
)

func InitTracer() {
func InitTracer() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}))
return tp
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ var (
)

func main() {
example.InitTracer()
tp := example.InitTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
flag.Parse()

if *brokers == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"log"

"github.com/astaxie/beego"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (c *ExampleController) Template() {
}
}

func initTracer() {
func initTracer() *sdktrace.TracerProvider {
// Create stdout exporter to be able to retrieve
// the collected spans.
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
Expand All @@ -61,14 +62,20 @@ func initTracer() {
// In a production application, use sdktrace.ProbabilitySampler with a desired probability.
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resource.NewWithAttributes(semconv.ServiceNameKey.String("ExampleService"))))
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}

func main() {
initTracer()
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()

// To enable tracing on template rendering, disable autorender
beego.BConfig.WebConfig.AutoRender = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/bradfitz/gomemcache/memcache"

"go.opentelemetry.io/contrib/instrumentation/github.com/bradfitz/gomemcache/memcache/otelmemcache"
oteltrace "go.opentelemetry.io/otel/trace"

oteltracestdout "go.opentelemetry.io/otel/exporters/stdout"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
Expand All @@ -32,6 +31,11 @@ func main() {
var host, port = os.Getenv("HOST"), "11211"

tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
ctx := context.Background()

c := otelmemcache.NewClientWithTracing(
Expand Down Expand Up @@ -73,14 +77,14 @@ func doMemcacheOperations(ctx context.Context, c *otelmemcache.Client) {
}
}

func initTracer() oteltrace.TracerProvider {
func initTracer() *sdktrace.TracerProvider {
exporter, err := oteltracestdout.NewExporter(oteltracestdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)

return tp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ require (
go.opentelemetry.io/contrib/instrumentation/github.com/bradfitz/gomemcache/memcache/otelmemcache v0.20.0
go.opentelemetry.io/otel/exporters/stdout v0.20.0
go.opentelemetry.io/otel/sdk v0.20.0
go.opentelemetry.io/otel/trace v0.20.0
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"log"
"net/http"
"strconv"
Expand Down Expand Up @@ -49,7 +50,12 @@ func (u UserResource) WebService() *restful.WebService {
}

func main() {
initTracer()
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
u := UserResource{}
// create the Otel filter
filter := otelrestful.OTelFilter("my-service")
Expand All @@ -60,17 +66,18 @@ func main() {
_ = http.ListenAndServe(":8080", nil)
}

func initTracer() {
func initTracer() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
tracer = otel.GetTracerProvider().Tracer("go-restful-server", oteltrace.WithInstrumentationVersion("0.1"))
return tp
}

func (u UserResource) getUser(req *restful.Request, resp *restful.Response) {
Expand Down
13 changes: 10 additions & 3 deletions instrumentation/github.com/gin-gonic/gin/otelgin/example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"html/template"
"log"
"net/http"
Expand All @@ -33,7 +34,12 @@ import (
var tracer = otel.Tracer("gin-server")

func main() {
initTracer()
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
r := gin.New()
r.Use(otelgin.Middleware("my-server"))
tmplName := "user"
Expand All @@ -51,17 +57,18 @@ func main() {
_ = r.Run(":8080")
}

func initTracer() {
func initTracer() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}

func getUser(c *gin.Context, id string) string {
Expand Down
12 changes: 9 additions & 3 deletions instrumentation/github.com/go-kit/kit/otelkit/example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import (
var tracer = otel.Tracer("kit-server")

func main() {
initTracer()
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
ep := otelkit.EndpointMiddleware(
otelkit.WithOperation("getUser"),
)(func(ctx context.Context, request interface{}) (response interface{}, err error) {
Expand All @@ -56,17 +61,18 @@ func main() {
_ = http.ListenAndServe(":8080", nil)
}

func initTracer() {
func initTracer() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}

func getUser(ctx context.Context, id string) string {
Expand Down
12 changes: 9 additions & 3 deletions instrumentation/github.com/gorilla/mux/otelmux/example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ import (
var tracer = otel.Tracer("mux-server")

func main() {
initTracer()
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
r := mux.NewRouter()
r.Use(otelmux.Middleware("my-server"))
r.HandleFunc("/users/{id:[0-9]+}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -48,17 +53,18 @@ func main() {
_ = http.ListenAndServe(":8080", nil)
}

func initTracer() {
func initTracer() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}

func getUser(ctx context.Context, id string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import (
var tracer = otel.Tracer("gin-server")

func main() {
initTracer()
tp := initTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
r := echo.New()
r.Use(otelecho.Middleware("my-server"))

Expand All @@ -51,17 +56,18 @@ func main() {
_ = r.Start(":8080")
}

func initTracer() {
func initTracer() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}

func getUser(ctx context.Context, id string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ import (
)

func main() {
config.Init()
tp := config.Init()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()

var conn *grpc.ClientConn
conn, err := grpc.Dial(":7777", grpc.WithInsecure(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import (
)

// Init configures an OpenTelemetry exporter and trace provider
func Init() {
func Init() *sdktrace.TracerProvider {
exporter, err := stdout.NewExporter(stdout.WithPrettyPrint())
if err != nil {
log.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
sdktrace.WithBatcher(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return tp
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ func (s *server) SayHelloBidiStream(stream api.HelloService_SayHelloBidiStreamSe
}

func main() {
config.Init()
tp := config.Init()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()

lis, err := net.Listen("tcp", port)
if err != nil {
Expand Down
Loading

0 comments on commit 7a135d3

Please sign in to comment.