From 7d64328cbb91ec33d14a5a8b77492ea98049c509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 27 Sep 2024 09:44:44 +0200 Subject: [PATCH 1/5] Move examples from core repository --- .gitignore | 10 +- examples/dice/doc.go | 9 ++ examples/dice/go.mod | 27 ++++ examples/dice/go.sum | 41 +++++ examples/dice/main.go | 87 ++++++++++ examples/dice/otel.go | 124 +++++++++++++++ examples/dice/rolldice.go | 63 ++++++++ examples/namedtracer/foo/foo.go | 30 ++++ examples/namedtracer/go.mod | 18 +++ examples/namedtracer/go.sum | 29 ++++ examples/namedtracer/main.go | 72 +++++++++ examples/opencensus/go.mod | 23 +++ examples/opencensus/go.sum | 126 +++++++++++++++ examples/opencensus/main.go | 133 ++++++++++++++++ examples/otel-collector/README.md | 63 ++++++++ examples/otel-collector/docker-compose.yaml | 23 +++ examples/otel-collector/go.mod | 30 ++++ examples/otel-collector/go.sum | 55 +++++++ examples/otel-collector/main.go | 166 ++++++++++++++++++++ examples/otel-collector/otel-collector.yaml | 33 ++++ examples/otel-collector/prometheus.yaml | 8 + examples/passthrough/README.md | 45 ++++++ examples/passthrough/go.mod | 18 +++ examples/passthrough/go.sum | 29 ++++ examples/passthrough/handler/handler.go | 66 ++++++++ examples/passthrough/main.go | 89 +++++++++++ examples/prometheus/doc.go | 5 + examples/prometheus/go.mod | 28 ++++ examples/prometheus/go.sum | 51 ++++++ examples/prometheus/main.go | 94 +++++++++++ examples/zipkin/Dockerfile | 7 + examples/zipkin/README.md | 24 +++ examples/zipkin/docker-compose.yml | 24 +++ examples/zipkin/go.mod | 19 +++ examples/zipkin/go.sum | 31 ++++ examples/zipkin/main.go | 87 ++++++++++ versions.yaml | 7 + 37 files changed, 1793 insertions(+), 1 deletion(-) create mode 100644 examples/dice/doc.go create mode 100644 examples/dice/go.mod create mode 100644 examples/dice/go.sum create mode 100644 examples/dice/main.go create mode 100644 examples/dice/otel.go create mode 100644 examples/dice/rolldice.go create mode 100644 examples/namedtracer/foo/foo.go create mode 100644 examples/namedtracer/go.mod create mode 100644 examples/namedtracer/go.sum create mode 100644 examples/namedtracer/main.go create mode 100644 examples/opencensus/go.mod create mode 100644 examples/opencensus/go.sum create mode 100644 examples/opencensus/main.go create mode 100644 examples/otel-collector/README.md create mode 100644 examples/otel-collector/docker-compose.yaml create mode 100644 examples/otel-collector/go.mod create mode 100644 examples/otel-collector/go.sum create mode 100644 examples/otel-collector/main.go create mode 100644 examples/otel-collector/otel-collector.yaml create mode 100644 examples/otel-collector/prometheus.yaml create mode 100644 examples/passthrough/README.md create mode 100644 examples/passthrough/go.mod create mode 100644 examples/passthrough/go.sum create mode 100644 examples/passthrough/handler/handler.go create mode 100644 examples/passthrough/main.go create mode 100644 examples/prometheus/doc.go create mode 100644 examples/prometheus/go.mod create mode 100644 examples/prometheus/go.sum create mode 100644 examples/prometheus/main.go create mode 100644 examples/zipkin/Dockerfile create mode 100644 examples/zipkin/README.md create mode 100644 examples/zipkin/docker-compose.yml create mode 100644 examples/zipkin/go.mod create mode 100644 examples/zipkin/go.sum create mode 100644 examples/zipkin/main.go diff --git a/.gitignore b/.gitignore index fcbbf1441d5..9eb2e347269 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,18 @@ venv/ .vscode/ *.iml *.so +*.exe coverage.* go.work go.work.sum -example + +examples/dice/dice +examples/namedtracer/namedtracer +examples/otel-collector/otel-collector +examples/opencensus/opencensus +examples/passthrough/passthrough +examples/prometheus/prometheus +examples/zipkin/zipkin instrumentation/google.golang.org/grpc/otelgrpc/example/server/server instrumentation/google.golang.org/grpc/otelgrpc/example/client/client diff --git a/examples/dice/doc.go b/examples/dice/doc.go new file mode 100644 index 00000000000..237a50ea65f --- /dev/null +++ b/examples/dice/doc.go @@ -0,0 +1,9 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Dice is the "Roll the dice" application. +// +// [Getting Started] uses this example to demonstrate OpenTelemetry Go. +// +// [Getting Started]: https://opentelemetry.io/docs/languages/net/automatic/getting-started/ +package main diff --git a/examples/dice/go.mod b/examples/dice/go.mod new file mode 100644 index 00000000000..64fa5713ddf --- /dev/null +++ b/examples/dice/go.mod @@ -0,0 +1,27 @@ +module go.opentelemetry.io/contrib/examples/dice + +go 1.22 + +require ( + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.6.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.30.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 + go.opentelemetry.io/otel/log v0.6.1-0.20240921150428-063239fa3703 + go.opentelemetry.io/otel/metric v1.30.0 + go.opentelemetry.io/otel/sdk v1.30.0 + go.opentelemetry.io/otel/sdk/log v0.6.1-0.20240921150428-063239fa3703 + go.opentelemetry.io/otel/sdk/metric v1.30.0 +) + +require ( + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + go.opentelemetry.io/otel/trace v1.30.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) + +replace go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp => ../../instrumentation/net/http/otelhttp diff --git a/examples/dice/go.sum b/examples/dice/go.sum new file mode 100644 index 00000000000..d16a5cb6512 --- /dev/null +++ b/examples/dice/go.sum @@ -0,0 +1,41 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.6.0 h1:bZHOb8k/CwwSt0DgvgaoOhBXWNdWqFWaIsGTtg1H3KE= +go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.6.0/go.mod h1:XlV163j81kDdIt5b5BXCjdqVfqJFy/LJrHA697SorvQ= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.30.0 h1:IyFlqNsi8VT/nwYlLJfdM0y1gavxGpEvnf6FtVfZ6X4= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.30.0/go.mod h1:bxiX8eUeKoAEQmbq/ecUT8UqZwCjZW52yJrXJUSozsk= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 h1:kn1BudCgwtE7PxLqcZkErpD8GKqLZ6BSzeW9QihQJeM= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0/go.mod h1:ljkUDtAMdleoi9tIG1R6dJUpVwDcYjw3J2Q6Q/SuiC0= +go.opentelemetry.io/otel/log v0.6.1-0.20240921150428-063239fa3703 h1:Fu/+6sUbEeIQEDrEAj19PIwL2fisvk64isyAc2UjDxM= +go.opentelemetry.io/otel/log v0.6.1-0.20240921150428-063239fa3703/go.mod h1:KdySypjQHhP069JX0z/t26VHwa8vSwzgaKmXtIB3fJM= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/sdk/log v0.6.1-0.20240921150428-063239fa3703 h1:65cOSgW0mkMGi8fwm+OV9UM7ThmcZtYwZAukUe2IQLE= +go.opentelemetry.io/otel/sdk/log v0.6.1-0.20240921150428-063239fa3703/go.mod h1:L1DN8RMAduKkrwRAFDEX3E3TLOq46+XMGSbUfHU/+vE= +go.opentelemetry.io/otel/sdk/metric v1.30.0 h1:QJLT8Pe11jyHBHfSAgYH7kEmT24eX792jZO1bo4BXkM= +go.opentelemetry.io/otel/sdk/metric v1.30.0/go.mod h1:waS6P3YqFNzeP01kuo/MBBYqaoBJl7efRQHOaydhy1Y= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/dice/main.go b/examples/dice/main.go new file mode 100644 index 00000000000..2a4c2453ec0 --- /dev/null +++ b/examples/dice/main.go @@ -0,0 +1,87 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "errors" + "log" + "net" + "net/http" + "os" + "os/signal" + "time" + + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" +) + +func main() { + if err := run(); err != nil { + log.Fatalln(err) + } +} + +func run() (err error) { + // Handle SIGINT (CTRL+C) gracefully. + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + + // Set up OpenTelemetry. + otelShutdown, err := setupOTelSDK(ctx) + if err != nil { + return + } + // Handle shutdown properly so nothing leaks. + defer func() { + err = errors.Join(err, otelShutdown(context.Background())) + }() + + // Start HTTP server. + srv := &http.Server{ + Addr: ":8080", + BaseContext: func(_ net.Listener) context.Context { return ctx }, + ReadTimeout: time.Second, + WriteTimeout: 10 * time.Second, + Handler: newHTTPHandler(), + } + srvErr := make(chan error, 1) + go func() { + srvErr <- srv.ListenAndServe() + }() + + // Wait for interruption. + select { + case err = <-srvErr: + // Error when starting HTTP server. + return + case <-ctx.Done(): + // Wait for first CTRL+C. + // Stop receiving signal notifications as soon as possible. + stop() + } + + // When Shutdown is called, ListenAndServe immediately returns ErrServerClosed. + err = srv.Shutdown(context.Background()) + return +} + +func newHTTPHandler() http.Handler { + mux := http.NewServeMux() + + // handleFunc is a replacement for mux.HandleFunc + // which enriches the handler's HTTP instrumentation with the pattern as the http.route. + handleFunc := func(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) { + // Configure the "http.route" for the HTTP instrumentation. + handler := otelhttp.WithRouteTag(pattern, http.HandlerFunc(handlerFunc)) + mux.Handle(pattern, handler) + } + + // Register handlers. + handleFunc("/rolldice/", rolldice) + handleFunc("/rolldice/{player}", rolldice) + + // Add HTTP instrumentation for the whole server. + handler := otelhttp.NewHandler(mux, "/") + return handler +} diff --git a/examples/dice/otel.go b/examples/dice/otel.go new file mode 100644 index 00000000000..88ff7decff1 --- /dev/null +++ b/examples/dice/otel.go @@ -0,0 +1,124 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "errors" + "time" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" + "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + "go.opentelemetry.io/otel/log/global" + "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/sdk/log" + "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/trace" +) + +// setupOTelSDK bootstraps the OpenTelemetry pipeline. +// If it does not return an error, make sure to call shutdown for proper cleanup. +func setupOTelSDK(ctx context.Context) (shutdown func(context.Context) error, err error) { + var shutdownFuncs []func(context.Context) error + + // shutdown calls cleanup functions registered via shutdownFuncs. + // The errors from the calls are joined. + // Each registered cleanup will be invoked once. + shutdown = func(ctx context.Context) error { + var err error + for _, fn := range shutdownFuncs { + err = errors.Join(err, fn(ctx)) + } + shutdownFuncs = nil + return err + } + + // handleErr calls shutdown for cleanup and makes sure that all errors are returned. + handleErr := func(inErr error) { + err = errors.Join(inErr, shutdown(ctx)) + } + + // Set up propagator. + prop := newPropagator() + otel.SetTextMapPropagator(prop) + + // Set up trace provider. + tracerProvider, err := newTraceProvider() + if err != nil { + handleErr(err) + return + } + shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown) + otel.SetTracerProvider(tracerProvider) + + // Set up meter provider. + meterProvider, err := newMeterProvider() + if err != nil { + handleErr(err) + return + } + shutdownFuncs = append(shutdownFuncs, meterProvider.Shutdown) + otel.SetMeterProvider(meterProvider) + + // Set up logger provider. + loggerProvider, err := newLoggerProvider() + if err != nil { + handleErr(err) + return + } + shutdownFuncs = append(shutdownFuncs, loggerProvider.Shutdown) + global.SetLoggerProvider(loggerProvider) + + return +} + +func newPropagator() propagation.TextMapPropagator { + return propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, + propagation.Baggage{}, + ) +} + +func newTraceProvider() (*trace.TracerProvider, error) { + traceExporter, err := stdouttrace.New( + stdouttrace.WithPrettyPrint()) + if err != nil { + return nil, err + } + + traceProvider := trace.NewTracerProvider( + trace.WithBatcher(traceExporter, + // Default is 5s. Set to 1s for demonstrative purposes. + trace.WithBatchTimeout(time.Second)), + ) + return traceProvider, nil +} + +func newMeterProvider() (*metric.MeterProvider, error) { + metricExporter, err := stdoutmetric.New() + if err != nil { + return nil, err + } + + meterProvider := metric.NewMeterProvider( + metric.WithReader(metric.NewPeriodicReader(metricExporter, + // Default is 1m. Set to 3s for demonstrative purposes. + metric.WithInterval(3*time.Second))), + ) + return meterProvider, nil +} + +func newLoggerProvider() (*log.LoggerProvider, error) { + logExporter, err := stdoutlog.New() + if err != nil { + return nil, err + } + + loggerProvider := log.NewLoggerProvider( + log.WithProcessor(log.NewBatchProcessor(logExporter)), + ) + return loggerProvider, nil +} diff --git a/examples/dice/rolldice.go b/examples/dice/rolldice.go new file mode 100644 index 00000000000..4adf9acb87d --- /dev/null +++ b/examples/dice/rolldice.go @@ -0,0 +1,63 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "fmt" + "io" + "log/slog" + "math/rand" + "net/http" + "os" + "strconv" + + // TODO: https://github.com/open-telemetry/opentelemetry-go/issues/5801 + // "go.opentelemetry.io/contrib/bridges/otelslog". + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" +) + +const name = "go.opentelemetry.io/contrib/examples/dice" + +var ( + tracer = otel.Tracer(name) + meter = otel.Meter(name) + logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) // TODO: logger = otelslog.NewLogger(name). + rollCnt metric.Int64Counter +) + +func init() { + var err error + rollCnt, err = meter.Int64Counter("dice.rolls", + metric.WithDescription("The number of rolls by roll value"), + metric.WithUnit("{roll}")) + if err != nil { + panic(err) + } +} + +func rolldice(w http.ResponseWriter, r *http.Request) { + ctx, span := tracer.Start(r.Context(), "roll") + defer span.End() + + roll := 1 + rand.Intn(6) + + var msg string + if player := r.PathValue("player"); player != "" { + msg = fmt.Sprintf("%s is rolling the dice", player) + } else { + msg = "Anonymous player is rolling the dice" + } + logger.InfoContext(ctx, msg, "result", roll) + + rollValueAttr := attribute.Int("roll.value", roll) + span.SetAttributes(rollValueAttr) + rollCnt.Add(ctx, 1, metric.WithAttributes(rollValueAttr)) + + resp := strconv.Itoa(roll) + "\n" + if _, err := io.WriteString(w, resp); err != nil { + logger.ErrorContext(ctx, "Write failed", "error", err) + } +} diff --git a/examples/namedtracer/foo/foo.go b/examples/namedtracer/foo/foo.go new file mode 100644 index 00000000000..eec9c31505d --- /dev/null +++ b/examples/namedtracer/foo/foo.go @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package foo // import "go.opentelemetry.io/contrib/examples/namedtracer/foo" + +import ( + "context" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" +) + +var lemonsKey = attribute.Key("ex.com/lemons") + +// SubOperation is an example to demonstrate the use of named tracer. +// It creates a named tracer with its package path. +func SubOperation(ctx context.Context) error { + // Using global provider. Alternative is to have application provide a getter + // for its component to get the instance of the provider. + tr := otel.Tracer("go.opentelemetry.io/contrib/examples/namedtracer/foo") + + var span trace.Span + _, span = tr.Start(ctx, "Sub operation...") + defer span.End() + span.SetAttributes(lemonsKey.String("five")) + span.AddEvent("Sub span event") + + return nil +} diff --git a/examples/namedtracer/go.mod b/examples/namedtracer/go.mod new file mode 100644 index 00000000000..8d9077a8e9c --- /dev/null +++ b/examples/namedtracer/go.mod @@ -0,0 +1,18 @@ +module go.opentelemetry.io/contrib/examples/namedtracer + +go 1.22 + +require ( + github.com/go-logr/stdr v1.2.2 + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 + go.opentelemetry.io/otel/sdk v1.30.0 + go.opentelemetry.io/otel/trace v1.30.0 +) + +require ( + github.com/go-logr/logr v1.4.2 // indirect + github.com/google/uuid v1.6.0 // indirect + go.opentelemetry.io/otel/metric v1.30.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/examples/namedtracer/go.sum b/examples/namedtracer/go.sum new file mode 100644 index 00000000000..1666cc4c382 --- /dev/null +++ b/examples/namedtracer/go.sum @@ -0,0 +1,29 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 h1:kn1BudCgwtE7PxLqcZkErpD8GKqLZ6BSzeW9QihQJeM= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0/go.mod h1:ljkUDtAMdleoi9tIG1R6dJUpVwDcYjw3J2Q6Q/SuiC0= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/namedtracer/main.go b/examples/namedtracer/main.go new file mode 100644 index 00000000000..fa48085d78c --- /dev/null +++ b/examples/namedtracer/main.go @@ -0,0 +1,72 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "fmt" + "log" + + "github.com/go-logr/stdr" + + "go.opentelemetry.io/contrib/examples/namedtracer/foo" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/baggage" + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/trace" +) + +var ( + fooKey = attribute.Key("ex.com/foo") + barKey = attribute.Key("ex.com/bar") + anotherKey = attribute.Key("ex.com/another") +) + +var tp *sdktrace.TracerProvider + +// initTracer creates and registers trace provider instance. +func initTracer() error { + exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint()) + if err != nil { + return fmt.Errorf("failed to initialize stdouttrace exporter: %w", err) + } + bsp := sdktrace.NewBatchSpanProcessor(exp) + tp = sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + sdktrace.WithSpanProcessor(bsp), + ) + otel.SetTracerProvider(tp) + return nil +} + +func main() { + // Set logging level to info to see SDK status messages + stdr.SetVerbosity(5) + + // initialize trace provider. + if err := initTracer(); err != nil { + log.Panic(err) + } + + // Create a named tracer with package path as its name. + tracer := tp.Tracer("go.opentelemetry.io/contrib/examples/namedtracer") + ctx := context.Background() + defer func() { _ = tp.Shutdown(ctx) }() + + m0, _ := baggage.NewMemberRaw(string(fooKey), "foo1") + m1, _ := baggage.NewMemberRaw(string(barKey), "bar1") + b, _ := baggage.New(m0, m1) + ctx = baggage.ContextWithBaggage(ctx, b) + + var span trace.Span + ctx, span = tracer.Start(ctx, "operation") + defer span.End() + span.AddEvent("Nice operation!", trace.WithAttributes(attribute.Int("bogons", 100))) + span.SetAttributes(anotherKey.String("yes")) + if err := foo.SubOperation(ctx); err != nil { + panic(err) + } +} diff --git a/examples/opencensus/go.mod b/examples/opencensus/go.mod new file mode 100644 index 00000000000..f84b4e84f18 --- /dev/null +++ b/examples/opencensus/go.mod @@ -0,0 +1,23 @@ +module go.opentelemetry.io/contrib/examples/opencensus + +go 1.22 + +require ( + go.opencensus.io v0.24.0 + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/bridge/opencensus v1.30.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.30.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 + go.opentelemetry.io/otel/sdk v1.30.0 + go.opentelemetry.io/otel/sdk/metric v1.30.0 +) + +require ( + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/google/uuid v1.6.0 // indirect + go.opentelemetry.io/otel/metric v1.30.0 // indirect + go.opentelemetry.io/otel/trace v1.30.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/examples/opencensus/go.sum b/examples/opencensus/go.sum new file mode 100644 index 00000000000..26c4b1151ec --- /dev/null +++ b/examples/opencensus/go.sum @@ -0,0 +1,126 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/bridge/opencensus v1.30.0 h1:F6WsF4aSV6g6IBiK4NZ8gWNHqFjtwxrEsUnrYEGyBSw= +go.opentelemetry.io/otel/bridge/opencensus v1.30.0/go.mod h1:3FOD7DbLQhWLxwnzbYoT1oHucvoQIqLXHez1u2KIxMc= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.30.0 h1:IyFlqNsi8VT/nwYlLJfdM0y1gavxGpEvnf6FtVfZ6X4= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.30.0/go.mod h1:bxiX8eUeKoAEQmbq/ecUT8UqZwCjZW52yJrXJUSozsk= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 h1:kn1BudCgwtE7PxLqcZkErpD8GKqLZ6BSzeW9QihQJeM= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0/go.mod h1:ljkUDtAMdleoi9tIG1R6dJUpVwDcYjw3J2Q6Q/SuiC0= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/sdk/metric v1.30.0 h1:QJLT8Pe11jyHBHfSAgYH7kEmT24eX792jZO1bo4BXkM= +go.opentelemetry.io/otel/sdk/metric v1.30.0/go.mod h1:waS6P3YqFNzeP01kuo/MBBYqaoBJl7efRQHOaydhy1Y= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/examples/opencensus/main.go b/examples/opencensus/main.go new file mode 100644 index 00000000000..3901b48b70c --- /dev/null +++ b/examples/opencensus/main.go @@ -0,0 +1,133 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "fmt" + "log" + "time" + + ocmetric "go.opencensus.io/metric" + "go.opencensus.io/metric/metricdata" + "go.opencensus.io/metric/metricproducer" + "go.opencensus.io/stats" + "go.opencensus.io/stats/view" + "go.opencensus.io/tag" + octrace "go.opencensus.io/trace" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/bridge/opencensus" + "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + "go.opentelemetry.io/otel/sdk/metric" + sdktrace "go.opentelemetry.io/otel/sdk/trace" +) + +var ( + // instrumenttype differentiates between our gauge and view metrics. + keyType = tag.MustNewKey("instrumenttype") + // Counts the number of lines read in from standard input. + countMeasure = stats.Int64("test_count", "A count of something", stats.UnitDimensionless) + countView = &view.View{ + Name: "test_count", + Measure: countMeasure, + Description: "A count of something", + Aggregation: view.Count(), + TagKeys: []tag.Key{keyType}, + } +) + +func main() { + log.Println("Using OpenTelemetry stdout exporters.") + traceExporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint()) + if err != nil { + log.Fatal(fmt.Errorf("error creating trace exporter: %w", err)) + } + metricsExporter, err := stdoutmetric.New() + if err != nil { + log.Fatal(fmt.Errorf("error creating metric exporter: %w", err)) + } + tracing(traceExporter) + if err := monitoring(metricsExporter); err != nil { + log.Fatal(err) + } +} + +// tracing demonstrates overriding the OpenCensus DefaultTracer to send spans +// to the OpenTelemetry exporter by calling OpenCensus APIs. +func tracing(otExporter sdktrace.SpanExporter) { + ctx := context.Background() + + log.Println("Configuring OpenCensus. Not Registering any OpenCensus exporters.") + octrace.ApplyConfig(octrace.Config{DefaultSampler: octrace.AlwaysSample()}) + + tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(otExporter)) + otel.SetTracerProvider(tp) + + log.Println("Installing the OpenCensus bridge to make OpenCensus libraries write spans using OpenTelemetry.") + opencensus.InstallTraceBridge() + tp.ForceFlush(ctx) + + log.Println("Creating OpenCensus span, which should be printed out using the OpenTelemetry stdouttrace exporter.\n-- It should have no parent, since it is the first span.") + ctx, outerOCSpan := octrace.StartSpan(ctx, "OpenCensusOuterSpan") + outerOCSpan.End() + tp.ForceFlush(ctx) + + log.Println("Creating OpenTelemetry span\n-- It should have the OpenCensus span as a parent, since the OpenCensus span was written with using OpenTelemetry APIs.") + tracer := tp.Tracer("go.opentelemetry.io/contrib/examples/opencensus") + ctx, otspan := tracer.Start(ctx, "OpenTelemetrySpan") + otspan.End() + tp.ForceFlush(ctx) + + log.Println("Creating OpenCensus span, which should be printed out using the OpenTelemetry stdouttrace exporter.\n-- It should have the OpenTelemetry span as a parent, since it was written using OpenTelemetry APIs") + _, innerOCSpan := octrace.StartSpan(ctx, "OpenCensusInnerSpan") + innerOCSpan.End() + tp.ForceFlush(ctx) +} + +// monitoring demonstrates creating an IntervalReader using the OpenTelemetry +// exporter to send metrics to the exporter by using either an OpenCensus +// registry or an OpenCensus view. +func monitoring(exporter metric.Exporter) error { + log.Println("Adding the OpenCensus metric Producer to an OpenTelemetry Reader to export OpenCensus metrics using the OpenTelemetry stdout exporter.") + // Register the OpenCensus metric Producer to add metrics from OpenCensus to the output. + reader := metric.NewPeriodicReader(exporter, metric.WithProducer(opencensus.NewMetricProducer())) + metric.NewMeterProvider(metric.WithReader(reader)) + + log.Println("Registering a gauge metric using an OpenCensus registry.") + r := ocmetric.NewRegistry() + metricproducer.GlobalManager().AddProducer(r) + gauge, err := r.AddInt64Gauge( + "test_gauge", + ocmetric.WithDescription("A gauge for testing"), + ocmetric.WithConstLabel(map[metricdata.LabelKey]metricdata.LabelValue{ + {Key: keyType.Name()}: metricdata.NewLabelValue("gauge"), + }), + ) + if err != nil { + return fmt.Errorf("failed to add gauge: %w", err) + } + entry, err := gauge.GetEntry() + if err != nil { + return fmt.Errorf("failed to get gauge entry: %w", err) + } + + log.Println("Registering a cumulative metric using an OpenCensus view.") + if err := view.Register(countView); err != nil { + return fmt.Errorf("failed to register views: %w", err) + } + ctx, err := tag.New(context.Background(), tag.Insert(keyType, "view")) + if err != nil { + return fmt.Errorf("failed to set tag: %w", err) + } + for i := int64(1); true; i++ { + // update stats for our gauge + entry.Set(i) + // update stats for our view + stats.Record(ctx, countMeasure.M(1)) + time.Sleep(time.Second) + } + return nil +} diff --git a/examples/otel-collector/README.md b/examples/otel-collector/README.md new file mode 100644 index 00000000000..b5cf7be5f9f --- /dev/null +++ b/examples/otel-collector/README.md @@ -0,0 +1,63 @@ +# OpenTelemetry Collector Example + +This example illustrates how to export trace and metric data from the +OpenTelemetry-Go SDK to the OpenTelemetry Collector. From there, we bring the +trace data to Jaeger and the metric data to Prometheus +The complete flow is: + +``` + -----> Jaeger (trace) +App + SDK ---> OpenTelemetry Collector ---| + -----> Prometheus (metrics) +``` + +# Prerequisites + +You will need [Docker Compose V2](https://docs.docker.com/compose/) installed for this demo. + +# Deploying to docker compose + +This command will bring up the OpenTelemetry Collector, Jaeger, and Prometheus, and +expose the necessary ports for you to view the data. + +```bash +docker compose up -d +``` + +# Running the code + +You can find the complete code for this example in the [main.go](./main.go) +file. To run it, ensure you have a somewhat recent version of Go (preferably >= +1.13) and do + +```bash +go run main.go +``` + +The example simulates an application, hard at work, computing for ten seconds +then finishing. + +# Viewing instrumentation data + +Now the exciting part! Let's check out the telemetry data generated by our +sample application + +## Jaeger UI + +The Jaeger UI is available at +[http://localhost:16686](http://localhost:16686). Navigate there in your favorite +web-browser to view the generated traces. + +## Prometheus + +The Prometheus UI is available at +[http://localhost:9090](http://localhost:9090). Navigate there in your favorite +web-browser to view the generated metrics, for instance, `testapp_run_total`. + +# Shutting down + +To shut down and clean the example, run + +```bash +docker compose down +``` diff --git a/examples/otel-collector/docker-compose.yaml b/examples/otel-collector/docker-compose.yaml new file mode 100644 index 00000000000..358ca086a24 --- /dev/null +++ b/examples/otel-collector/docker-compose.yaml @@ -0,0 +1,23 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +services: + otel-collector: + image: otel/opentelemetry-collector-contrib:0.110.0 + command: ["--config=/etc/otel-collector.yaml"] + volumes: + - ./otel-collector.yaml:/etc/otel-collector.yaml + ports: + - 4317:4317 + + prometheus: + image: prom/prometheus:v2.54.1 + volumes: + - ./prometheus.yaml:/etc/prometheus/prometheus.yml + ports: + - 9090:9090 + + jaeger: + image: jaegertracing/all-in-one:1.60 + ports: + - 16686:16686 diff --git a/examples/otel-collector/go.mod b/examples/otel-collector/go.mod new file mode 100644 index 00000000000..fd95f5f44df --- /dev/null +++ b/examples/otel-collector/go.mod @@ -0,0 +1,30 @@ +module go.opentelemetry.io/contrib/examples/otel-collector + +go 1.22 + +require ( + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.30.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0 + go.opentelemetry.io/otel/metric v1.30.0 + go.opentelemetry.io/otel/sdk v1.30.0 + go.opentelemetry.io/otel/sdk/metric v1.30.0 + go.opentelemetry.io/otel/trace v1.30.0 + google.golang.org/grpc v1.67.0 +) + +require ( + github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect + go.opentelemetry.io/proto/otlp v1.3.1 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/text v0.18.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 // indirect + google.golang.org/protobuf v1.34.2 // indirect +) diff --git a/examples/otel-collector/go.sum b/examples/otel-collector/go.sum new file mode 100644 index 00000000000..276e90eef40 --- /dev/null +++ b/examples/otel-collector/go.sum @@ -0,0 +1,55 @@ +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.30.0 h1:WypxHH02KX2poqqbaadmkMYalGyy/vil4HE4PM4nRJc= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.30.0/go.mod h1:U79SV99vtvGSEBeeHnpgGJfTsnsdkWLpPN/CcHAzBSI= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 h1:lsInsfvhVIfOI6qHVyysXMNDnjO9Npvl7tlDPJFBVd4= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0/go.mod h1:KQsVNh4OjgjTG0G6EiNi1jVpnaeeKsKMRwbLN+f1+8M= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0 h1:m0yTiGDLUvVYaTFbAvCkVYIYcvwKt3G7OLoN77NUs/8= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0/go.mod h1:wBQbT4UekBfegL2nx0Xk1vBcnzyBPsIVm9hRG4fYcr4= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/sdk/metric v1.30.0 h1:QJLT8Pe11jyHBHfSAgYH7kEmT24eX792jZO1bo4BXkM= +go.opentelemetry.io/otel/sdk/metric v1.30.0/go.mod h1:waS6P3YqFNzeP01kuo/MBBYqaoBJl7efRQHOaydhy1Y= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0= +go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61 h1:pAjq8XSSzXoP9ya73v/w+9QEAAJNluLrpmMq5qFJQNY= +google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61/go.mod h1:O6rP0uBq4k0mdi/b4ZEMAZjkhYWhS815kCvaMha4VN8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 h1:N9BgCIAUvn/M+p4NJccWPWb3BWh88+zyL0ll9HgbEeM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw= +google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/otel-collector/main.go b/examples/otel-collector/main.go new file mode 100644 index 00000000000..97776c9fe14 --- /dev/null +++ b/examples/otel-collector/main.go @@ -0,0 +1,166 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Example using OTLP exporters + collector + third-party backends. For +// information about using the exporter, see: +// https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp?tab=doc#example-package-Insecure +package main + +import ( + "context" + "fmt" + "log" + "os" + "os/signal" + "time" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" + "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/propagation" + sdkmetric "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/resource" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" + "go.opentelemetry.io/otel/trace" +) + +var serviceName = semconv.ServiceNameKey.String("test-service") + +// Initialize a gRPC connection to be used by both the tracer and meter +// providers. +func initConn() (*grpc.ClientConn, error) { + // It connects the OpenTelemetry Collector through local gRPC connection. + // You may replace `localhost:4317` with your endpoint. + conn, err := grpc.NewClient("localhost:4317", + // Note the use of insecure transport here. TLS is recommended in production. + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + if err != nil { + return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err) + } + + return conn, err +} + +// Initializes an OTLP exporter, and configures the corresponding trace provider. +func initTracerProvider(ctx context.Context, res *resource.Resource, conn *grpc.ClientConn) (func(context.Context) error, error) { + // Set up a trace exporter + traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn)) + if err != nil { + return nil, fmt.Errorf("failed to create trace exporter: %w", err) + } + + // Register the trace exporter with a TracerProvider, using a batch + // span processor to aggregate spans before export. + bsp := sdktrace.NewBatchSpanProcessor(traceExporter) + tracerProvider := sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + sdktrace.WithResource(res), + sdktrace.WithSpanProcessor(bsp), + ) + otel.SetTracerProvider(tracerProvider) + + // Set global propagator to tracecontext (the default is no-op). + otel.SetTextMapPropagator(propagation.TraceContext{}) + + // Shutdown will flush any remaining spans and shut down the exporter. + return tracerProvider.Shutdown, nil +} + +// Initializes an OTLP exporter, and configures the corresponding meter provider. +func initMeterProvider(ctx context.Context, res *resource.Resource, conn *grpc.ClientConn) (func(context.Context) error, error) { + metricExporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(conn)) + if err != nil { + return nil, fmt.Errorf("failed to create metrics exporter: %w", err) + } + + meterProvider := sdkmetric.NewMeterProvider( + sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metricExporter)), + sdkmetric.WithResource(res), + ) + otel.SetMeterProvider(meterProvider) + + return meterProvider.Shutdown, nil +} + +func main() { + log.Printf("Waiting for connection...") + + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + + conn, err := initConn() + if err != nil { + log.Fatal(err) + } + + res, err := resource.New(ctx, + resource.WithAttributes( + // The service name used to display traces in backends + serviceName, + ), + ) + if err != nil { + log.Fatal(err) + } + + shutdownTracerProvider, err := initTracerProvider(ctx, res, conn) + if err != nil { + log.Fatal(err) + } + defer func() { + if err := shutdownTracerProvider(ctx); err != nil { + log.Fatalf("failed to shutdown TracerProvider: %s", err) + } + }() + + shutdownMeterProvider, err := initMeterProvider(ctx, res, conn) + if err != nil { + log.Fatal(err) + } + defer func() { + if err := shutdownMeterProvider(ctx); err != nil { + log.Fatalf("failed to shutdown MeterProvider: %s", err) + } + }() + + name := "go.opentelemetry.io/contrib/examples/otel-collector" + tracer := otel.Tracer(name) + meter := otel.Meter(name) + + // Attributes represent additional key-value descriptors that can be bound + // to a metric observer or recorder. + commonAttrs := []attribute.KeyValue{ + attribute.String("attrA", "chocolate"), + attribute.String("attrB", "raspberry"), + attribute.String("attrC", "vanilla"), + } + + runCount, err := meter.Int64Counter("run", metric.WithDescription("The number of times the iteration ran")) + if err != nil { + log.Fatal(err) + } + + // Work begins + ctx, span := tracer.Start( + ctx, + "CollectorExporter-Example", + trace.WithAttributes(commonAttrs...)) + defer span.End() + for i := 0; i < 10; i++ { + _, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i)) + runCount.Add(ctx, 1, metric.WithAttributes(commonAttrs...)) + log.Printf("Doing really hard work (%d / 10)\n", i+1) + + <-time.After(time.Second) + iSpan.End() + } + + log.Printf("Done!") +} diff --git a/examples/otel-collector/otel-collector.yaml b/examples/otel-collector/otel-collector.yaml new file mode 100644 index 00000000000..dc6b7286b44 --- /dev/null +++ b/examples/otel-collector/otel-collector.yaml @@ -0,0 +1,33 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 +processors: +extensions: + health_check: {} +exporters: + otlp: + endpoint: jaeger:4317 + tls: + insecure: true + prometheus: + endpoint: 0.0.0.0:9090 + namespace: testapp + debug: + +service: + extensions: [health_check] + pipelines: + traces: + receivers: [otlp] + processors: [] + exporters: [otlp, debug] + + metrics: + receivers: [otlp] + processors: [] + exporters: [prometheus, debug] diff --git a/examples/otel-collector/prometheus.yaml b/examples/otel-collector/prometheus.yaml new file mode 100644 index 00000000000..8e920be9b5f --- /dev/null +++ b/examples/otel-collector/prometheus.yaml @@ -0,0 +1,8 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +scrape_configs: + - job_name: 'otel-collector' + scrape_interval: 5s + static_configs: + - targets: ['otel-collector:9090'] diff --git a/examples/passthrough/README.md b/examples/passthrough/README.md new file mode 100644 index 00000000000..9fd54cd4927 --- /dev/null +++ b/examples/passthrough/README.md @@ -0,0 +1,45 @@ +# "Passthrough" setup for OpenTelemetry + +Some Go programs may wish to propagate context without recording spans. To do this in OpenTelemetry, simply install `TextMapPropagators`, but do not install a TracerProvider using the SDK. This works because the default TracerProvider implementation returns a "Non-Recording" span that keeps the context of the caller but does not record spans. + +For example, when you initialize your global settings, the following will propagate context without recording spans: + +```golang +// Setup Propagators only +otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) +``` + +But the following will propagate context _and_ create new, potentially recorded spans: + +```golang +// Setup SDK +exp, _ := stdout.New(stdout.WithPrettyPrint()) +tp = sdktrace.NewTracerProvider( + sdktrace.WithBatcher(exp), +) +otel.SetTracerProvider(tp) +// Setup Propagators +otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) +``` + +## The Demo + +The demo has the following call structure: + +`Outer -> Passthrough -> Inner` + +If all components had both an SDK and propagators registered, we would expect the trace to look like: + +``` +|-------outer---------| + |-Passthrough recv-| + |Passthrough send| + |---inner---| +``` + +However, in this demo, only the outer and inner have TracerProvider backed by the SDK. All components have Propagators set. In this case, we expect to see: + +``` +|-------outer---------| + |---inner---| +``` diff --git a/examples/passthrough/go.mod b/examples/passthrough/go.mod new file mode 100644 index 00000000000..ccd5083027c --- /dev/null +++ b/examples/passthrough/go.mod @@ -0,0 +1,18 @@ +module go.opentelemetry.io/contrib/examples/passthrough + +go 1.22 + +require ( + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 + go.opentelemetry.io/otel/sdk v1.30.0 + go.opentelemetry.io/otel/trace v1.30.0 +) + +require ( + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + go.opentelemetry.io/otel/metric v1.30.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/examples/passthrough/go.sum b/examples/passthrough/go.sum new file mode 100644 index 00000000000..1666cc4c382 --- /dev/null +++ b/examples/passthrough/go.sum @@ -0,0 +1,29 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0 h1:kn1BudCgwtE7PxLqcZkErpD8GKqLZ6BSzeW9QihQJeM= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.30.0/go.mod h1:ljkUDtAMdleoi9tIG1R6dJUpVwDcYjw3J2Q6Q/SuiC0= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/passthrough/handler/handler.go b/examples/passthrough/handler/handler.go new file mode 100644 index 00000000000..4d93986b55c --- /dev/null +++ b/examples/passthrough/handler/handler.go @@ -0,0 +1,66 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package handler // import "go.opentelemetry.io/contrib/examples/passthrough/handler" + +import ( + "context" + "log" + "net/http" + "time" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/trace" +) + +// Handler is a minimal implementation of the handler and client from +// go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp for demonstration purposes. +// It handles an incoming http request, and makes an outgoing http request. +type Handler struct { + propagators propagation.TextMapPropagator + tracer trace.Tracer + next func(r *http.Request) +} + +// New returns a new Handler that will trace requests before handing them off +// to next. +func New(next func(r *http.Request)) *Handler { + // Like most instrumentation packages, this handler defaults to using the + // global propagators and tracer providers. + return &Handler{ + propagators: otel.GetTextMapPropagator(), + tracer: otel.Tracer("go.opentelemetry.io/contrib/examples/passthrough/handler"), + next: next, + } +} + +// HandleHTTPReq mimics what an instrumented http server does. +func (h *Handler) HandleHTTPReq(r *http.Request) { + ctx := h.propagators.Extract(r.Context(), propagation.HeaderCarrier(r.Header)) + var span trace.Span + log.Println("The \"handle passthrough request\" span should NOT be recorded, because it is recorded by a TracerProvider not backed by the SDK.") + ctx, span = h.tracer.Start(ctx, "handle passthrough request") + defer span.End() + + // Pretend to do work + time.Sleep(time.Second) + + h.makeOutgoingRequest(ctx) +} + +// makeOutgoingRequest mimics what an instrumented http client does. +func (h *Handler) makeOutgoingRequest(ctx context.Context) { + // make a new http request + r, err := http.NewRequest("", "", nil) + if err != nil { + panic(err) + } + + log.Println("The \"make outgoing request from passthrough\" span should NOT be recorded, because it is recorded by a TracerProvider not backed by the SDK.") + ctx, span := h.tracer.Start(ctx, "make outgoing request from passthrough") + defer span.End() + r = r.WithContext(ctx) + h.propagators.Inject(ctx, propagation.HeaderCarrier(r.Header)) + h.next(r) +} diff --git a/examples/passthrough/main.go b/examples/passthrough/main.go new file mode 100644 index 00000000000..7ff730e3a4a --- /dev/null +++ b/examples/passthrough/main.go @@ -0,0 +1,89 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "fmt" + "log" + "net/http" + "time" + + "go.opentelemetry.io/contrib/examples/passthrough/handler" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + "go.opentelemetry.io/otel/propagation" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/trace" +) + +const name = "go.opentelemetry.io/contrib/examples/passthrough" + +func main() { + ctx := context.Background() + + initPassthroughGlobals() + tp, err := nonGlobalTracer() + if err != nil { + log.Fatal(err) + } + defer func() { _ = tp.Shutdown(ctx) }() + + // make an initial http request + r, err := http.NewRequest("", "", nil) + if err != nil { + panic(err) + } + + // This is roughly what an instrumented http client does. + log.Println("The \"make outer request\" span should be recorded, because it is recorded with a Tracer from the SDK TracerProvider") + var span trace.Span + tracer := tp.Tracer(name) + ctx, span = tracer.Start(ctx, "make outer request") + defer span.End() + r = r.WithContext(ctx) + otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) + + backendFunc := func(r *http.Request) { + // This is roughly what an instrumented http server does. + ctx := r.Context() + + tp := trace.SpanFromContext(ctx).TracerProvider() + tracer := tp.Tracer(name) + + ctx = otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier(r.Header)) + log.Println("The \"handle inner request\" span should be recorded, because it is recorded with a Tracer from the SDK TracerProvider") + _, span := tracer.Start(ctx, "handle inner request") + defer span.End() + + // Do "backend work" + time.Sleep(time.Second) + } + // This handler will be a passthrough, since we didn't set a global TracerProvider + passthroughHandler := handler.New(backendFunc) + passthroughHandler.HandleHTTPReq(r) +} + +func initPassthroughGlobals() { + // We explicitly DO NOT set the global TracerProvider using otel.SetTracerProvider(). + // The unset TracerProvider returns a "non-recording" span, but still passes through context. + log.Println("Register a global TextMapPropagator, but do not register a global TracerProvider to be in \"passthrough\" mode.") + log.Println("The \"passthrough\" mode propagates the TraceContext and Baggage, but does not record spans.") + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) +} + +// nonGlobalTracer creates a trace provider instance for testing, but doesn't +// set it as the global tracer provider. +func nonGlobalTracer() (*sdktrace.TracerProvider, error) { + exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint()) + if err != nil { + return nil, fmt.Errorf("failed to initialize stdouttrace exporter: %w", err) + } + bsp := sdktrace.NewBatchSpanProcessor(exp) + tp := sdktrace.NewTracerProvider( + sdktrace.WithSampler(sdktrace.AlwaysSample()), + sdktrace.WithSpanProcessor(bsp), + ) + return tp, nil +} diff --git a/examples/prometheus/doc.go b/examples/prometheus/doc.go new file mode 100644 index 00000000000..7e07c5888da --- /dev/null +++ b/examples/prometheus/doc.go @@ -0,0 +1,5 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Package main provides a code sample of the Prometheus exporter. +package main diff --git a/examples/prometheus/go.mod b/examples/prometheus/go.mod new file mode 100644 index 00000000000..34db9d3862a --- /dev/null +++ b/examples/prometheus/go.mod @@ -0,0 +1,28 @@ +module go.opentelemetry.io/contrib/examples/prometheus + +go 1.22 + +require ( + github.com/prometheus/client_golang v1.20.4 + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/exporters/prometheus v0.52.0 + go.opentelemetry.io/otel/metric v1.30.0 + go.opentelemetry.io/otel/sdk/metric v1.30.0 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/klauspost/compress v1.17.10 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.59.1 // indirect + github.com/prometheus/procfs v0.15.1 // indirect + go.opentelemetry.io/otel/sdk v1.30.0 // indirect + go.opentelemetry.io/otel/trace v1.30.0 // indirect + golang.org/x/sys v0.25.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect +) diff --git a/examples/prometheus/go.sum b/examples/prometheus/go.sum new file mode 100644 index 00000000000..c8323643a2d --- /dev/null +++ b/examples/prometheus/go.sum @@ -0,0 +1,51 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0= +github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= +github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= +github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= +github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0= +github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= +github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= +github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/exporters/prometheus v0.52.0 h1:kmU3H0b9ufFSi8IQCcxack+sWUblKkFbqWYs6YiACGQ= +go.opentelemetry.io/otel/exporters/prometheus v0.52.0/go.mod h1:+wsAp2+JhuGXX7YRkjlkx6hyWY3ogFPfNA4x3nyiAh0= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/sdk/metric v1.30.0 h1:QJLT8Pe11jyHBHfSAgYH7kEmT24eX792jZO1bo4BXkM= +go.opentelemetry.io/otel/sdk/metric v1.30.0/go.mod h1:waS6P3YqFNzeP01kuo/MBBYqaoBJl7efRQHOaydhy1Y= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/prometheus/main.go b/examples/prometheus/main.go new file mode 100644 index 00000000000..0f909864ecd --- /dev/null +++ b/examples/prometheus/main.go @@ -0,0 +1,94 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "fmt" + "log" + "math/rand" + "net/http" + "os" + "os/signal" + "time" + + "github.com/prometheus/client_golang/prometheus/promhttp" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/exporters/prometheus" + api "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/sdk/metric" +) + +const meterName = "go.opentelemetry.io/contrib/examples/prometheus" + +func main() { + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + ctx := context.Background() + + // The exporter embeds a default OpenTelemetry Reader and + // implements prometheus.Collector, allowing it to be used as + // both a Reader and Collector. + exporter, err := prometheus.New() + if err != nil { + log.Fatal(err) + } + provider := metric.NewMeterProvider(metric.WithReader(exporter)) + meter := provider.Meter(meterName) + + // Start the prometheus HTTP server and pass the exporter Collector to it + go serveMetrics() + + opt := api.WithAttributes( + attribute.Key("A").String("B"), + attribute.Key("C").String("D"), + ) + + // This is the equivalent of prometheus.NewCounterVec + counter, err := meter.Float64Counter("foo", api.WithDescription("a simple counter")) + if err != nil { + log.Fatal(err) + } + counter.Add(ctx, 5, opt) + + gauge, err := meter.Float64ObservableGauge("bar", api.WithDescription("a fun little gauge")) + if err != nil { + log.Fatal(err) + } + _, err = meter.RegisterCallback(func(_ context.Context, o api.Observer) error { + n := -10. + rng.Float64()*(90.) // [-10, 100) + o.ObserveFloat64(gauge, n, opt) + return nil + }, gauge) + if err != nil { + log.Fatal(err) + } + + // This is the equivalent of prometheus.NewHistogramVec + histogram, err := meter.Float64Histogram( + "baz", + api.WithDescription("a histogram with custom buckets and rename"), + api.WithExplicitBucketBoundaries(64, 128, 256, 512, 1024, 2048, 4096), + ) + if err != nil { + log.Fatal(err) + } + histogram.Record(ctx, 136, opt) + histogram.Record(ctx, 64, opt) + histogram.Record(ctx, 701, opt) + histogram.Record(ctx, 830, opt) + + ctx, _ = signal.NotifyContext(ctx, os.Interrupt) + <-ctx.Done() +} + +func serveMetrics() { + log.Printf("serving metrics at localhost:2223/metrics") + http.Handle("/metrics", promhttp.Handler()) + err := http.ListenAndServe(":2223", nil) //nolint:gosec // Ignoring G114: Use of net/http serve function that has no support for setting timeouts. + if err != nil { + fmt.Printf("error serving http: %v", err) + return + } +} diff --git a/examples/zipkin/Dockerfile b/examples/zipkin/Dockerfile new file mode 100644 index 00000000000..029ae04cce7 --- /dev/null +++ b/examples/zipkin/Dockerfile @@ -0,0 +1,7 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +FROM golang:1.23-alpine +COPY . /go/src/github.com/open-telemetry/opentelemetry-go/ +WORKDIR /go/src/github.com/open-telemetry/opentelemetry-go/example/zipkin/ +RUN go install ./main.go +CMD ["/go/bin/main"] diff --git a/examples/zipkin/README.md b/examples/zipkin/README.md new file mode 100644 index 00000000000..462f96e8fa2 --- /dev/null +++ b/examples/zipkin/README.md @@ -0,0 +1,24 @@ +# Zipkin Exporter Example + +Send an example span to a [Zipkin](https://zipkin.io/) service. +These instructions expect you have [docker-compose](https://docs.docker.com/compose/) installed. + +Bring up the `zipkin-collector` service and example `zipkin-client` service to send an example trace: + +```sh +docker-compose up --detach zipkin-collector zipkin-client +``` + +The `zipkin-client` service sends just one trace and exits. Retrieve the `traceId` generated by the `zipkin-client` service; should be the last line in the logs: + +```sh +docker-compose logs --tail=1 zipkin-client +``` + +With the `traceId` you can view the trace from the `zipkin-collector` service UI hosted on port `9411`, e.g. with `traceId` of `f5695ba3b2ed00ea583fa4fa0badbeef`: [http://localhost:9411/zipkin/traces/f5695ba3b2ed00ea583fa4fa0badbeef](http://localhost:9411/zipkin/traces/f5695ba3b2ed00ea583fa4fa0badbeef) + +Shut down the services when you are finished with the example: + +```sh +docker-compose down +``` diff --git a/examples/zipkin/docker-compose.yml b/examples/zipkin/docker-compose.yml new file mode 100644 index 00000000000..dab1a1cd7e6 --- /dev/null +++ b/examples/zipkin/docker-compose.yml @@ -0,0 +1,24 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 +version: "3.7" +services: + zipkin-collector: + image: openzipkin/zipkin-slim:latest + ports: + - "9411:9411" + networks: + - example + zipkin-client: + build: + dockerfile: $PWD/Dockerfile + context: ../.. + command: + - "/bin/sh" + - "-c" + - "while ! nc -w 1 -z zipkin-collector 9411; do echo sleep for 1s waiting for zipkin-collector to become available; sleep 1; done && /go/bin/main -zipkin http://zipkin-collector:9411/api/v2/spans" + networks: + - example + depends_on: + - zipkin-collector +networks: + example: diff --git a/examples/zipkin/go.mod b/examples/zipkin/go.mod new file mode 100644 index 00000000000..44741349ef6 --- /dev/null +++ b/examples/zipkin/go.mod @@ -0,0 +1,19 @@ +module go.opentelemetry.io/contrib/examples/zipkin + +go 1.22 + +require ( + go.opentelemetry.io/otel v1.30.0 + go.opentelemetry.io/otel/exporters/zipkin v1.30.0 + go.opentelemetry.io/otel/sdk v1.30.0 + go.opentelemetry.io/otel/trace v1.30.0 +) + +require ( + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/openzipkin/zipkin-go v0.4.3 // indirect + go.opentelemetry.io/otel/metric v1.30.0 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/examples/zipkin/go.sum b/examples/zipkin/go.sum new file mode 100644 index 00000000000..e25f27ea238 --- /dev/null +++ b/examples/zipkin/go.sum @@ -0,0 +1,31 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/openzipkin/zipkin-go v0.4.3 h1:9EGwpqkgnwdEIJ+Od7QVSEIH+ocmm5nPat0G7sjsSdg= +github.com/openzipkin/zipkin-go v0.4.3/go.mod h1:M9wCJZFWCo2RiY+o1eBCEMe0Dp2S5LDHcMZmk3RmK7c= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts= +go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc= +go.opentelemetry.io/otel/exporters/zipkin v1.30.0 h1:1uYaSfxiCLdJATlGEtYjQe4jZYfqCjVwxeSTMXe8VF4= +go.opentelemetry.io/otel/exporters/zipkin v1.30.0/go.mod h1:r/4BhMc3kiKxD61wGh9J3NVQ3/cZ45F2NHkQgVnql48= +go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w= +go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ= +go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE= +go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg= +go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc= +go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/zipkin/main.go b/examples/zipkin/main.go new file mode 100644 index 00000000000..82dc5f32f6f --- /dev/null +++ b/examples/zipkin/main.go @@ -0,0 +1,87 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Command zipkin is an example program that creates spans +// and uploads to openzipkin collector. +package main + +import ( + "context" + "flag" + "log" + "os" + "os/signal" + "time" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/exporters/zipkin" + "go.opentelemetry.io/otel/sdk/resource" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" + "go.opentelemetry.io/otel/trace" +) + +const name = "go.opentelemetry.io/contrib/examples/zipkin" + +var logger = log.New(os.Stderr, "zipkin-example", log.Ldate|log.Ltime|log.Llongfile) + +// initTracer creates a new trace provider instance and registers it as global trace provider. +func initTracer(url string) (func(context.Context) error, error) { + // Create Zipkin Exporter and install it as a global tracer. + // + // For demoing purposes, always sample. In a production application, you should + // configure the sampler to a trace.ParentBased(trace.TraceIDRatioBased) set at the desired + // ratio. + exporter, err := zipkin.New( + url, + zipkin.WithLogger(logger), + ) + if err != nil { + return nil, err + } + + batcher := sdktrace.NewBatchSpanProcessor(exporter) + + tp := sdktrace.NewTracerProvider( + sdktrace.WithSpanProcessor(batcher), + sdktrace.WithResource(resource.NewWithAttributes( + semconv.SchemaURL, + semconv.ServiceName("zipkin-test"), + )), + ) + otel.SetTracerProvider(tp) + + return tp.Shutdown, nil +} + +func main() { + url := flag.String("zipkin", "http://localhost:9411/api/v2/spans", "zipkin url") + flag.Parse() + + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + + shutdown, err := initTracer(*url) + if err != nil { + log.Fatal(err) + } + defer func() { + if err := shutdown(ctx); err != nil { + log.Fatal("failed to shutdown TracerProvider: %w", err) + } + }() + + tr := otel.GetTracerProvider().Tracer(name) + ctx, span := tr.Start(ctx, "foo", trace.WithSpanKind(trace.SpanKindServer)) + <-time.After(6 * time.Millisecond) + bar(ctx) + <-time.After(6 * time.Millisecond) + span.End() +} + +func bar(ctx context.Context) { + tr := trace.SpanFromContext(ctx).TracerProvider().Tracer(name) + _, span := tr.Start(ctx, "bar") + <-time.After(6 * time.Millisecond) + span.End() +} diff --git a/versions.yaml b/versions.yaml index 8d1b9eca4dc..4295aaddd7f 100644 --- a/versions.yaml +++ b/versions.yaml @@ -89,6 +89,13 @@ module-sets: - go.opentelemetry.io/contrib/detectors/azure/azurevm excluded-modules: - go.opentelemetry.io/contrib/bridges/otellogr + - go.opentelemetry.io/contrib/examples/dice + - go.opentelemetry.io/contrib/examples/namedtracer + - go.opentelemetry.io/contrib/examples/opencensus + - go.opentelemetry.io/contrib/examples/otel-collector + - go.opentelemetry.io/contrib/examples/passthrough + - go.opentelemetry.io/contrib/examples/prometheus + - go.opentelemetry.io/contrib/examples/zipkin - go.opentelemetry.io/contrib/instrgen - go.opentelemetry.io/contrib/instrgen/driver - go.opentelemetry.io/contrib/instrgen/testdata/interface From 93f4748faa4dab82920eabd3a4578aec2f973564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 27 Sep 2024 09:46:43 +0200 Subject: [PATCH 2/5] Update docs --- CHANGELOG.md | 1 + README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d9be18acb7..c282dbff84f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - The `Severitier` and `SeverityVar` types are added to `go.opentelemetry.io/contrib/processors/minsev` allowing dynamic configuration of the severity used by the `LogProcessor`. (#6116) +- Move examples from `go.opentelemetry.io/otel` to this repository under `examples` directory. (#6158) ### Changed diff --git a/README.md b/README.md index eb4b6e19889..73bb76cbabe 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Collection of 3rd-party packages for [OpenTelemetry-Go](https://github.com/open- ## Contents +- [Examples](./examples/): Examples of OpenTelemetry libraries usage. - [Instrumentation](./instrumentation/): Packages providing OpenTelemetry instrumentation for 3rd-party libraries. - [Propagators](./propagators/): Packages providing OpenTelemetry context propagators for 3rd-party propagation formats. - [Detectors](./detectors/): Packages providing OpenTelemetry resource detectors for 3rd-party cloud computing environments. From c4f78a172d551f565d3eeaa305272d9218552e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 27 Sep 2024 09:48:28 +0200 Subject: [PATCH 3/5] Add .lycheeignore --- .lycheeignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .lycheeignore diff --git a/.lycheeignore b/.lycheeignore new file mode 100644 index 00000000000..0afeace7b3a --- /dev/null +++ b/.lycheeignore @@ -0,0 +1 @@ +http://localhost From 44da8c92cfee6f00c0a38eef7a407075aa0a9f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 27 Sep 2024 09:56:24 +0200 Subject: [PATCH 4/5] nolint:gosec G404 --- examples/dice/rolldice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dice/rolldice.go b/examples/dice/rolldice.go index 4adf9acb87d..2949930e66d 100644 --- a/examples/dice/rolldice.go +++ b/examples/dice/rolldice.go @@ -42,7 +42,7 @@ func rolldice(w http.ResponseWriter, r *http.Request) { ctx, span := tracer.Start(r.Context(), "roll") defer span.End() - roll := 1 + rand.Intn(6) + roll := 1 + rand.Intn(6) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive. var msg string if player := r.PathValue("player"); player != "" { From 40351a1c676f491b61c5522610b11c5d0d92b91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 27 Sep 2024 11:11:54 +0200 Subject: [PATCH 5/5] nolint:gosec G404 --- examples/prometheus/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/prometheus/main.go b/examples/prometheus/main.go index 0f909864ecd..f8df3ac66f3 100644 --- a/examples/prometheus/main.go +++ b/examples/prometheus/main.go @@ -24,7 +24,7 @@ import ( const meterName = "go.opentelemetry.io/contrib/examples/prometheus" func main() { - rng := rand.New(rand.NewSource(time.Now().UnixNano())) + rng := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive. ctx := context.Background() // The exporter embeds a default OpenTelemetry Reader and