Skip to content

Commit

Permalink
Update grpc example to allow long runs (#835)
Browse files Browse the repository at this point in the history
* Update grpc example to allow long runs

* Trigger Build

* Update sample to allow indefinite span generation
  • Loading branch information
psx95 committed Apr 19, 2024
1 parent e60bca9 commit 4fe07d3
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions example/trace/otlpgrpc/example.go
Expand Up @@ -17,6 +17,7 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"time"
Expand All @@ -31,6 +32,8 @@ import (
"google.golang.org/grpc/credentials/oauth"
)

var keepRunning = flag.Bool("keepRunning", false, "Set to true for generating spans at a fixed rate indefinitely. Default is false.")

func initTracer() (func(), error) {
ctx := context.Background()

Expand Down Expand Up @@ -62,7 +65,28 @@ func initTracer() (func(), error) {
}, nil
}

func generateTestSpan(ctx context.Context, tr trace.Tracer, description string) {
fmt.Println("starting span...")
_, span := tr.Start(ctx, description, trace.WithAttributes(semconv.PeerServiceKey.String("ExampleService")))
defer span.End()
defer fmt.Println("ending span.")

time.Sleep(3 * time.Second)
}

func generateSpansAtFixedRate(ctx context.Context, tr trace.Tracer) {
fmt.Println("Generating 1 test span every 10 seconds indefinitely")
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()

for tick := range ticker.C {
go generateTestSpan(ctx, tr, fmt.Sprintf("span-%s", tick))
}
fmt.Println("Span generation complete.")
}

func main() {
flag.Parse()
shutdown, err := initTracer()
if err != nil {
log.Fatal(err)
Expand All @@ -71,10 +95,9 @@ func main() {
tr := otel.Tracer("cloudtrace/example/client")

ctx := context.Background()
fmt.Println("starting span...")
_, span := tr.Start(ctx, "test span", trace.WithAttributes(semconv.PeerServiceKey.String("ExampleService")))
defer span.End()
defer fmt.Println("ending span.")

time.Sleep(3 * time.Second)
if *keepRunning {
generateSpansAtFixedRate(ctx, tr)
} else {
generateTestSpan(ctx, tr, "test span")
}
}

0 comments on commit 4fe07d3

Please sign in to comment.