From 4f053f2eecf353f2aadb621299057df9d6b45a26 Mon Sep 17 00:00:00 2001 From: Igor Eulalio <41654187+IgorEulalio@users.noreply.github.com> Date: Thu, 5 Dec 2024 06:10:29 -0500 Subject: [PATCH] Split dice example into instrumented and uninstrumented (#6300) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As discussed in this thread, we want to have two different examples, one with instrumentation and one without it. Fixes https://github.com/open-telemetry/opentelemetry-go-contrib/issues/6296 --------- Signed-off-by: Igor Eulalio Co-authored-by: Robert PajÄ…k --- .gitignore | 3 +- examples/dice/README.md | 32 ++++++++++ examples/dice/{ => instrumented}/doc.go | 0 examples/dice/instrumented/get.sh | 17 +++++ examples/dice/{ => instrumented}/go.mod | 6 +- examples/dice/{ => instrumented}/go.sum | 0 examples/dice/instrumented/init.sh | 6 ++ examples/dice/{ => instrumented}/main.go | 0 examples/dice/{ => instrumented}/otel.go | 0 examples/dice/{ => instrumented}/rolldice.go | 0 examples/dice/instrumented/run.sh | 8 +++ examples/dice/instrumented/tidy.sh | 6 ++ examples/dice/run.sh | 29 +++++++++ examples/dice/uninstrumented/go.mod | 3 + examples/dice/uninstrumented/main.go | 65 ++++++++++++++++++++ examples/dice/uninstrumented/rolldice.go | 29 +++++++++ examples/dice/uninstrumented/run.sh | 6 ++ 17 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 examples/dice/README.md rename examples/dice/{ => instrumented}/doc.go (100%) create mode 100755 examples/dice/instrumented/get.sh rename examples/dice/{ => instrumented}/go.mod (83%) rename examples/dice/{ => instrumented}/go.sum (100%) create mode 100755 examples/dice/instrumented/init.sh rename examples/dice/{ => instrumented}/main.go (100%) rename examples/dice/{ => instrumented}/otel.go (100%) rename examples/dice/{ => instrumented}/rolldice.go (100%) create mode 100755 examples/dice/instrumented/run.sh create mode 100755 examples/dice/instrumented/tidy.sh create mode 100755 examples/dice/run.sh create mode 100644 examples/dice/uninstrumented/go.mod create mode 100644 examples/dice/uninstrumented/main.go create mode 100644 examples/dice/uninstrumented/rolldice.go create mode 100755 examples/dice/uninstrumented/run.sh diff --git a/.gitignore b/.gitignore index e8ce6d8cc7b..1172e25e803 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,8 @@ coverage.* go.work go.work.sum -examples/dice/dice +examples/dice/instrumented/instrumented +examples/dice/uninstrumented/uninstrumented examples/namedtracer/namedtracer examples/otel-collector/otel-collector examples/opencensus/opencensus diff --git a/examples/dice/README.md b/examples/dice/README.md new file mode 100644 index 00000000000..7540f3b8007 --- /dev/null +++ b/examples/dice/README.md @@ -0,0 +1,32 @@ +# Dice example + +This is the foundation example for [Getting Started](https://opentelemetry.io/docs/languages/go/getting-started/) with OpenTelemetry. + +Below, you will see instructions on how to run this application, either with or without instrumentation. + +## Usage + +The `run.sh` script accepts one argument to determine which example to run: + +- `uninstrumented` +- `instrumented` + +### Running the Uninstrumented Example + +The uninstrumented example is a very simple dice application, without OpenTelemetry instrumentation. + +To run the uninstrumented example, execute: + +```bash +./run.sh uninstrumented +``` + +### Running the Instrumented Example + +The instrumented example is exactly the same application, which includes OpenTelemetry instrumentation. + +To run the instrumented example, execute: + +```bash +./run.sh instrumented +``` \ No newline at end of file diff --git a/examples/dice/doc.go b/examples/dice/instrumented/doc.go similarity index 100% rename from examples/dice/doc.go rename to examples/dice/instrumented/doc.go diff --git a/examples/dice/instrumented/get.sh b/examples/dice/instrumented/get.sh new file mode 100755 index 00000000000..1dad879f7ed --- /dev/null +++ b/examples/dice/instrumented/get.sh @@ -0,0 +1,17 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +go get "go.opentelemetry.io/otel" \ + "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" \ + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" \ + "go.opentelemetry.io/otel/exporters/stdout/stdoutlog" \ + "go.opentelemetry.io/otel/sdk/log" \ + "go.opentelemetry.io/otel/log/global" \ + "go.opentelemetry.io/otel/propagation" \ + "go.opentelemetry.io/otel/sdk/metric" \ + "go.opentelemetry.io/otel/sdk/resource" \ + "go.opentelemetry.io/otel/sdk/trace" \ + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"\ + "go.opentelemetry.io/contrib/bridges/otelslog" \ No newline at end of file diff --git a/examples/dice/go.mod b/examples/dice/instrumented/go.mod similarity index 83% rename from examples/dice/go.mod rename to examples/dice/instrumented/go.mod index daad89c7802..2e2366d4299 100644 --- a/examples/dice/go.mod +++ b/examples/dice/instrumented/go.mod @@ -1,4 +1,4 @@ -module go.opentelemetry.io/contrib/examples/dice +module go.opentelemetry.io/contrib/examples/dice/instrumented go 1.22 @@ -26,6 +26,6 @@ require ( ) replace ( - go.opentelemetry.io/contrib/bridges/otelslog => ../../bridges/otelslog - go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp => ../../instrumentation/net/http/otelhttp + go.opentelemetry.io/contrib/bridges/otelslog => ../../../bridges/otelslog + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp => ../../../instrumentation/net/http/otelhttp ) diff --git a/examples/dice/go.sum b/examples/dice/instrumented/go.sum similarity index 100% rename from examples/dice/go.sum rename to examples/dice/instrumented/go.sum diff --git a/examples/dice/instrumented/init.sh b/examples/dice/instrumented/init.sh new file mode 100755 index 00000000000..29b88d7cbba --- /dev/null +++ b/examples/dice/instrumented/init.sh @@ -0,0 +1,6 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +go mod init go.opentelemetry.io/contrib/examples/dice/instrumented \ No newline at end of file diff --git a/examples/dice/main.go b/examples/dice/instrumented/main.go similarity index 100% rename from examples/dice/main.go rename to examples/dice/instrumented/main.go diff --git a/examples/dice/otel.go b/examples/dice/instrumented/otel.go similarity index 100% rename from examples/dice/otel.go rename to examples/dice/instrumented/otel.go diff --git a/examples/dice/rolldice.go b/examples/dice/instrumented/rolldice.go similarity index 100% rename from examples/dice/rolldice.go rename to examples/dice/instrumented/rolldice.go diff --git a/examples/dice/instrumented/run.sh b/examples/dice/instrumented/run.sh new file mode 100755 index 00000000000..8477cd4bd19 --- /dev/null +++ b/examples/dice/instrumented/run.sh @@ -0,0 +1,8 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +go mod tidy +export OTEL_RESOURCE_ATTRIBUTES="service.name=dice,service.version=0.1.0" +go run . \ No newline at end of file diff --git a/examples/dice/instrumented/tidy.sh b/examples/dice/instrumented/tidy.sh new file mode 100755 index 00000000000..26beb7b358c --- /dev/null +++ b/examples/dice/instrumented/tidy.sh @@ -0,0 +1,6 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +go mod tidy \ No newline at end of file diff --git a/examples/dice/run.sh b/examples/dice/run.sh new file mode 100755 index 00000000000..d3b81b46ab3 --- /dev/null +++ b/examples/dice/run.sh @@ -0,0 +1,29 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +# Check if at least one argument is provided +if [ -z "$1" ]; then + echo "Usage: $0 {instrumented|uninstrumented}" + exit 1 +fi + +# Switch based on the first argument +case "$1" in + instrumented) + echo "Running instrumented example..." + cd instrumented || exit + source tidy.sh + source run.sh + ;; + uninstrumented) + echo "Running uninstrumented example..." + cd uninstrumented || exit + source run.sh + ;; + *) + echo "Invalid argument: $1. Use 'instrumented' or 'uninstrumented'." + exit 1 + ;; +esac diff --git a/examples/dice/uninstrumented/go.mod b/examples/dice/uninstrumented/go.mod new file mode 100644 index 00000000000..4f61f552cf1 --- /dev/null +++ b/examples/dice/uninstrumented/go.mod @@ -0,0 +1,3 @@ +module go.opentelemetry.io/otel/example/dice/uninstrumented + +go 1.22 diff --git a/examples/dice/uninstrumented/main.go b/examples/dice/uninstrumented/main.go new file mode 100644 index 00000000000..f5cd5e20b03 --- /dev/null +++ b/examples/dice/uninstrumented/main.go @@ -0,0 +1,65 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "context" + "log" + "net" + "net/http" + "os" + "os/signal" + "time" +) + +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() + + // 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() { + log.Println("Running HTTP server...") + 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() + + // Register handlers. + mux.HandleFunc("/rolldice/", rolldice) + mux.HandleFunc("/rolldice/{player}", rolldice) + + return mux +} diff --git a/examples/dice/uninstrumented/rolldice.go b/examples/dice/uninstrumented/rolldice.go new file mode 100644 index 00000000000..95ac6b4a516 --- /dev/null +++ b/examples/dice/uninstrumented/rolldice.go @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "io" + "log" + "math/rand" + "net/http" + "strconv" +) + +func rolldice(w http.ResponseWriter, r *http.Request) { + 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 != "" { + msg = player + " is rolling the dice" + } else { + msg = "Anonymous player is rolling the dice" + } + log.Printf("%s, result: %d", msg, roll) + + resp := strconv.Itoa(roll) + "\n" + if _, err := io.WriteString(w, resp); err != nil { + log.Printf("Write failed: %v", err) + } +} diff --git a/examples/dice/uninstrumented/run.sh b/examples/dice/uninstrumented/run.sh new file mode 100755 index 00000000000..aeb8a8031ff --- /dev/null +++ b/examples/dice/uninstrumented/run.sh @@ -0,0 +1,6 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +go run . \ No newline at end of file