From bab9037ffa178487a8dda6e50e045e66d813683a Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Wed, 6 Nov 2024 23:39:49 +0100 Subject: [PATCH 01/11] feat: refactor example to have both instrumented and uninstrumented --- examples/dice/README.md | 37 +++++++++++ examples/dice/go.sum | 41 ------------ examples/dice/{ => instrumented}/doc.go | 0 examples/dice/{ => instrumented}/go.mod | 2 +- examples/dice/instrumented/go.sum | 43 +++++++++++++ examples/dice/{ => instrumented}/main.go | 0 examples/dice/{ => instrumented}/otel.go | 0 examples/dice/{ => instrumented}/rolldice.go | 11 ++-- examples/dice/run.sh | 33 ++++++++++ examples/dice/uninstrumented/go.mod | 3 + examples/dice/uninstrumented/main.go | 65 ++++++++++++++++++++ examples/dice/uninstrumented/rolldice.go | 30 +++++++++ 12 files changed, 219 insertions(+), 46 deletions(-) create mode 100644 examples/dice/README.md delete mode 100644 examples/dice/go.sum rename examples/dice/{ => instrumented}/doc.go (100%) rename examples/dice/{ => instrumented}/go.mod (99%) create mode 100644 examples/dice/instrumented/go.sum rename examples/dice/{ => instrumented}/main.go (100%) rename examples/dice/{ => instrumented}/otel.go (100%) rename examples/dice/{ => instrumented}/rolldice.go (78%) 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 diff --git a/examples/dice/README.md b/examples/dice/README.md new file mode 100644 index 00000000000..38e4d2c08e4 --- /dev/null +++ b/examples/dice/README.md @@ -0,0 +1,37 @@ +Instructions on how to run instrumented and uninstrumented examples. + +## Prerequisites + +- [Go](https://golang.org/dl/) installed on your system. +- Necessary permissions to execute shell scripts. + +## Usage + +The `run.sh` script accepts one argument to determine which example to run: + +- `instrumented` +- `uninstrumented` + +### Running the Instrumented Example + +The instrumented example includes OpenTelemetry instrumentation for collecting telemetry data like traces and metrics. + +To run the instrumented example, execute: + +```bash +./run.sh instrumented +``` + +### Running the Uninstrumented Example + +The uninstrumented example is the exact same application, without OTEL instrumentation. + +To run the instrumented example, execute: + +```bash +./run.sh uninstrumented +``` + + + + diff --git a/examples/dice/go.sum b/examples/dice/go.sum deleted file mode 100644 index e4a01cfda78..00000000000 --- a/examples/dice/go.sum +++ /dev/null @@ -1,41 +0,0 @@ -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.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY= -go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE= -go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0 h1:TwmL3O3fRR80m8EshBrd8YydEZMcUCsZXzOUlnFohwM= -go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0/go.mod h1:tH98dDv5KPmPThswbXA0fr0Lwfs+OhK8HgaCo7PjRrk= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.31.0 h1:HZgBIps9wH0RDrwjrmNa3DVbNRW60HEhdzqZFyAp3fI= -go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.31.0/go.mod h1:RDRhvt6TDG0eIXmonAx5bd9IcwpqCkziwkOClzWKwAQ= -go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 h1:UGZ1QwZWY67Z6BmckTU+9Rxn04m2bD3gD6Mk0OIOCPk= -go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0/go.mod h1:fcwWuDuaObkkChiDlhEpSq9+X1C0omv+s5mBtToAQ64= -go.opentelemetry.io/otel/log v0.7.0 h1:d1abJc0b1QQZADKvfe9JqqrfmPYQCz2tUSO+0XZmuV4= -go.opentelemetry.io/otel/log v0.7.0/go.mod h1:2jf2z7uVfnzDNknKTO9G+ahcOAyWcp1fJmk/wJjULRo= -go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE= -go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY= -go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk= -go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0= -go.opentelemetry.io/otel/sdk/log v0.7.0 h1:dXkeI2S0MLc5g0/AwxTZv6EUEjctiH8aG14Am56NTmQ= -go.opentelemetry.io/otel/sdk/log v0.7.0/go.mod h1:oIRXpW+WD6M8BuGj5rtS0aRu/86cbDV/dAfNaZBIjYM= -go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc= -go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8= -go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys= -go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A= -golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= -golang.org/x/sys v0.26.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/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/go.mod b/examples/dice/instrumented/go.mod similarity index 99% rename from examples/dice/go.mod rename to examples/dice/instrumented/go.mod index 7c5e1380a54..c1147b92834 100644 --- a/examples/dice/go.mod +++ b/examples/dice/instrumented/go.mod @@ -28,4 +28,4 @@ require ( replace ( go.opentelemetry.io/contrib/bridges/otelslog => ../../bridges/otelslog go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp => ../../instrumentation/net/http/otelhttp -) +) \ No newline at end of file diff --git a/examples/dice/instrumented/go.sum b/examples/dice/instrumented/go.sum new file mode 100644 index 00000000000..4e4b5e44cb1 --- /dev/null +++ b/examples/dice/instrumented/go.sum @@ -0,0 +1,43 @@ +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/contrib/instrumentation/net/http/otelhttp v0.55.0 h1:ZIg3ZT/aQ7AfKqdwp7ECpOK6vHqquXXuyTjIO8ZdmPs= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0/go.mod h1:DQAwmETtZV00skUwgD6+0U89g80NKsJE3DCKeLLPQMI= +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.0 h1:nH66tr+dmEgW5y+F9LanGJUBYPrRgP4g2EkmPE3LeK8= +go.opentelemetry.io/otel/log v0.6.0/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.0 h1:4J8BwXY4EeDE9Mowg+CyhWVBhTSLXVXodiXxS/+PGqI= +go.opentelemetry.io/otel/sdk/log v0.6.0/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/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 78% rename from examples/dice/rolldice.go rename to examples/dice/instrumented/rolldice.go index c68356a34b3..ffc509cb267 100644 --- a/examples/dice/rolldice.go +++ b/examples/dice/instrumented/rolldice.go @@ -6,22 +6,25 @@ package main import ( "fmt" "io" + "log/slog" "math/rand" "net/http" + "os" "strconv" - "go.opentelemetry.io/contrib/bridges/otelslog" + // 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" +const name = "go.opentelemetry.io/otel/example/dice" var ( tracer = otel.Tracer(name) meter = otel.Meter(name) - logger = otelslog.NewLogger(name) + logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) // TODO: logger = otelslog.NewLogger(name). rollCnt metric.Int64Counter ) @@ -39,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) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive. + roll := 1 + rand.Intn(6) var msg string if player := r.PathValue("player"); player != "" { diff --git a/examples/dice/run.sh b/examples/dice/run.sh new file mode 100755 index 00000000000..2d13f5f52a6 --- /dev/null +++ b/examples/dice/run.sh @@ -0,0 +1,33 @@ +# 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 + go mod tidy + go mod download + export OTEL_RESOURCE_ATTRIBUTES="service.name=dice,service.version=0.1.0" + go run . + ;; + uninstrumented) + echo "Running uninstrumented example..." + cd uninstrumented || exit + go mod tidy + go mod download + go run . + ;; + *) + 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..5ba2cd95795 --- /dev/null +++ b/examples/dice/uninstrumented/rolldice.go @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "fmt" + "io" + "log" + "math/rand" + "net/http" + "strconv" +) + +func rolldice(w http.ResponseWriter, r *http.Request) { + 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" + } + 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) + } +} From f7ec0496a98afa5841035460fd57b40ae21a9fbf Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Wed, 6 Nov 2024 23:39:49 +0100 Subject: [PATCH 02/11] remove go.mod replaces to avoid linter issues Signed-off-by: Igor Eulalio --- examples/dice/README.md | 37 +++++++++++ examples/dice/{ => instrumented}/doc.go | 0 examples/dice/{ => instrumented}/go.mod | 6 -- examples/dice/{ => instrumented}/go.sum | 2 + examples/dice/{ => instrumented}/main.go | 0 examples/dice/{ => instrumented}/otel.go | 0 examples/dice/{ => instrumented}/rolldice.go | 11 ++-- examples/dice/run.sh | 33 ++++++++++ examples/dice/uninstrumented/go.mod | 3 + examples/dice/uninstrumented/main.go | 65 ++++++++++++++++++++ examples/dice/uninstrumented/rolldice.go | 30 +++++++++ 11 files changed, 177 insertions(+), 10 deletions(-) create mode 100644 examples/dice/README.md rename examples/dice/{ => instrumented}/doc.go (100%) rename examples/dice/{ => instrumented}/go.mod (77%) rename examples/dice/{ => instrumented}/go.sum (93%) rename examples/dice/{ => instrumented}/main.go (100%) rename examples/dice/{ => instrumented}/otel.go (100%) rename examples/dice/{ => instrumented}/rolldice.go (78%) 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 diff --git a/examples/dice/README.md b/examples/dice/README.md new file mode 100644 index 00000000000..38e4d2c08e4 --- /dev/null +++ b/examples/dice/README.md @@ -0,0 +1,37 @@ +Instructions on how to run instrumented and uninstrumented examples. + +## Prerequisites + +- [Go](https://golang.org/dl/) installed on your system. +- Necessary permissions to execute shell scripts. + +## Usage + +The `run.sh` script accepts one argument to determine which example to run: + +- `instrumented` +- `uninstrumented` + +### Running the Instrumented Example + +The instrumented example includes OpenTelemetry instrumentation for collecting telemetry data like traces and metrics. + +To run the instrumented example, execute: + +```bash +./run.sh instrumented +``` + +### Running the Uninstrumented Example + +The uninstrumented example is the exact same application, without OTEL instrumentation. + +To run the instrumented example, execute: + +```bash +./run.sh uninstrumented +``` + + + + 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/go.mod b/examples/dice/instrumented/go.mod similarity index 77% rename from examples/dice/go.mod rename to examples/dice/instrumented/go.mod index 7c5e1380a54..2c4a6496069 100644 --- a/examples/dice/go.mod +++ b/examples/dice/instrumented/go.mod @@ -3,7 +3,6 @@ module go.opentelemetry.io/contrib/examples/dice go 1.22 require ( - go.opentelemetry.io/contrib/bridges/otelslog v0.6.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 go.opentelemetry.io/otel v1.31.0 go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0 @@ -24,8 +23,3 @@ require ( go.opentelemetry.io/otel/trace v1.31.0 // indirect golang.org/x/sys v0.26.0 // indirect ) - -replace ( - 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 93% rename from examples/dice/go.sum rename to examples/dice/instrumented/go.sum index e4a01cfda78..d5ef526c48e 100644 --- a/examples/dice/go.sum +++ b/examples/dice/instrumented/go.sum @@ -15,6 +15,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb 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/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0/go.mod h1:qxuZLtbq5QDtdeSHsS7bcf6EH6uO6jUAgk764zd3rhM= go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY= go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE= go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0 h1:TwmL3O3fRR80m8EshBrd8YydEZMcUCsZXzOUlnFohwM= 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 78% rename from examples/dice/rolldice.go rename to examples/dice/instrumented/rolldice.go index c68356a34b3..ffc509cb267 100644 --- a/examples/dice/rolldice.go +++ b/examples/dice/instrumented/rolldice.go @@ -6,22 +6,25 @@ package main import ( "fmt" "io" + "log/slog" "math/rand" "net/http" + "os" "strconv" - "go.opentelemetry.io/contrib/bridges/otelslog" + // 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" +const name = "go.opentelemetry.io/otel/example/dice" var ( tracer = otel.Tracer(name) meter = otel.Meter(name) - logger = otelslog.NewLogger(name) + logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) // TODO: logger = otelslog.NewLogger(name). rollCnt metric.Int64Counter ) @@ -39,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) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive. + roll := 1 + rand.Intn(6) var msg string if player := r.PathValue("player"); player != "" { diff --git a/examples/dice/run.sh b/examples/dice/run.sh new file mode 100755 index 00000000000..2d13f5f52a6 --- /dev/null +++ b/examples/dice/run.sh @@ -0,0 +1,33 @@ +# 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 + go mod tidy + go mod download + export OTEL_RESOURCE_ATTRIBUTES="service.name=dice,service.version=0.1.0" + go run . + ;; + uninstrumented) + echo "Running uninstrumented example..." + cd uninstrumented || exit + go mod tidy + go mod download + go run . + ;; + *) + 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..5ba2cd95795 --- /dev/null +++ b/examples/dice/uninstrumented/rolldice.go @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package main + +import ( + "fmt" + "io" + "log" + "math/rand" + "net/http" + "strconv" +) + +func rolldice(w http.ResponseWriter, r *http.Request) { + 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" + } + 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) + } +} From a83483f18efac397e6503c884960eac6295c6d25 Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Thu, 7 Nov 2024 18:23:17 +0100 Subject: [PATCH 03/11] fix: rollback rolldice version --- examples/dice/instrumented/go.mod | 6 ++++++ examples/dice/instrumented/go.sum | 2 -- examples/dice/instrumented/rolldice.go | 11 ++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/dice/instrumented/go.mod b/examples/dice/instrumented/go.mod index 2c4a6496069..5ac9a4d7bd9 100644 --- a/examples/dice/instrumented/go.mod +++ b/examples/dice/instrumented/go.mod @@ -3,6 +3,7 @@ module go.opentelemetry.io/contrib/examples/dice go 1.22 require ( + go.opentelemetry.io/contrib/bridges/otelslog v0.6.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 go.opentelemetry.io/otel v1.31.0 go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0 @@ -23,3 +24,8 @@ require ( go.opentelemetry.io/otel/trace v1.31.0 // indirect golang.org/x/sys v0.26.0 // indirect ) + +replace ( + 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/instrumented/go.sum b/examples/dice/instrumented/go.sum index d5ef526c48e..e4a01cfda78 100644 --- a/examples/dice/instrumented/go.sum +++ b/examples/dice/instrumented/go.sum @@ -15,8 +15,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb 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/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0/go.mod h1:qxuZLtbq5QDtdeSHsS7bcf6EH6uO6jUAgk764zd3rhM= go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY= go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE= go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0 h1:TwmL3O3fRR80m8EshBrd8YydEZMcUCsZXzOUlnFohwM= diff --git a/examples/dice/instrumented/rolldice.go b/examples/dice/instrumented/rolldice.go index ffc509cb267..c68356a34b3 100644 --- a/examples/dice/instrumented/rolldice.go +++ b/examples/dice/instrumented/rolldice.go @@ -6,25 +6,22 @@ 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/contrib/bridges/otelslog" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" ) -const name = "go.opentelemetry.io/otel/example/dice" +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). + logger = otelslog.NewLogger(name) rollCnt metric.Int64Counter ) @@ -42,7 +39,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 f71b74ede8ea7c29d0f96f179a1a75113a91aee3 Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Thu, 7 Nov 2024 18:51:03 +0100 Subject: [PATCH 04/11] fix: add nolint for rand.Int --- examples/dice/uninstrumented/rolldice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dice/uninstrumented/rolldice.go b/examples/dice/uninstrumented/rolldice.go index 5ba2cd95795..414cc7dbcb0 100644 --- a/examples/dice/uninstrumented/rolldice.go +++ b/examples/dice/uninstrumented/rolldice.go @@ -13,7 +13,7 @@ import ( ) func rolldice(w http.ResponseWriter, r *http.Request) { - 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 4c54ab7aa9be226fe984cb8707c502f9c3f1d70f Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Thu, 7 Nov 2024 19:26:23 +0100 Subject: [PATCH 05/11] chore: rename dice instrumented module name, add binaries to .gitignore --- .gitignore | 3 ++- examples/dice/instrumented/go.mod | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9eb2e347269..94ae8d208fc 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/instrumented/go.mod b/examples/dice/instrumented/go.mod index 5ac9a4d7bd9..876e375ec98 100644 --- a/examples/dice/instrumented/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 From 2227bdedac901ee707975d3b620d62978f3d26f8 Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Tue, 12 Nov 2024 21:03:01 +0100 Subject: [PATCH 06/11] chore: remove pre requisites, add examples in specific order Signed-off-by: Igor Eulalio --- examples/dice/README.md | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/examples/dice/README.md b/examples/dice/README.md index 38e4d2c08e4..698e305d2ce 100644 --- a/examples/dice/README.md +++ b/examples/dice/README.md @@ -1,37 +1,30 @@ -Instructions on how to run instrumented and uninstrumented examples. - -## Prerequisites +# Dice example -- [Go](https://golang.org/dl/) installed on your system. -- Necessary permissions to execute shell scripts. +Instructions on how to run instrumented and uninstrumented examples. ## Usage The `run.sh` script accepts one argument to determine which example to run: -- `instrumented` - `uninstrumented` - -### Running the Instrumented Example - -The instrumented example includes OpenTelemetry instrumentation for collecting telemetry data like traces and metrics. - -To run the instrumented example, execute: - -```bash -./run.sh instrumented -``` +- `instrumented` ### Running the Uninstrumented Example -The uninstrumented example is the exact same application, without OTEL instrumentation. +The uninstrumented example is a very simple dice application, without OpenTelemetry instrumentation. -To run the instrumented example, execute: +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 From 6f9ad5319dd0afc502b09e04cf8b9f7ae6f16cd2 Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Fri, 22 Nov 2024 13:36:51 -0300 Subject: [PATCH 07/11] chore: add detailed information about dice example, add source for all shell scripts from docs Signed-off-by: Igor Eulalio --- examples/dice/README.md | 4 +++- examples/dice/instrumented/.tool-versions | 1 + examples/dice/instrumented/get.sh | 17 +++++++++++++++++ examples/dice/instrumented/init.sh | 6 ++++++ examples/dice/instrumented/run.sh | 8 ++++++++ examples/dice/instrumented/tidy.sh | 6 ++++++ examples/dice/run.sh | 9 +++------ examples/dice/uninstrumented/run.sh | 6 ++++++ 8 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 examples/dice/instrumented/.tool-versions create mode 100755 examples/dice/instrumented/get.sh create mode 100755 examples/dice/instrumented/init.sh create mode 100755 examples/dice/instrumented/run.sh create mode 100755 examples/dice/instrumented/tidy.sh create mode 100755 examples/dice/uninstrumented/run.sh diff --git a/examples/dice/README.md b/examples/dice/README.md index 698e305d2ce..7540f3b8007 100644 --- a/examples/dice/README.md +++ b/examples/dice/README.md @@ -1,6 +1,8 @@ # Dice example -Instructions on how to run instrumented and uninstrumented examples. +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 diff --git a/examples/dice/instrumented/.tool-versions b/examples/dice/instrumented/.tool-versions new file mode 100644 index 00000000000..f5264425ddd --- /dev/null +++ b/examples/dice/instrumented/.tool-versions @@ -0,0 +1 @@ +golang 1.22.0 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/instrumented/init.sh b/examples/dice/instrumented/init.sh new file mode 100755 index 00000000000..2aa81adbbcf --- /dev/null +++ b/examples/dice/instrumented/init.sh @@ -0,0 +1,6 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +#!/bin/bash + +module go.opentelemetry.io/contrib/examples/dice/instrumented \ No newline at end of file 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 index 2d13f5f52a6..27cf0af192b 100755 --- a/examples/dice/run.sh +++ b/examples/dice/run.sh @@ -14,17 +14,14 @@ case "$1" in instrumented) echo "Running instrumented example..." cd instrumented || exit - go mod tidy + source tidy.sh go mod download - export OTEL_RESOURCE_ATTRIBUTES="service.name=dice,service.version=0.1.0" - go run . + source run.sh ;; uninstrumented) echo "Running uninstrumented example..." cd uninstrumented || exit - go mod tidy - go mod download - go run . + source run.sh ;; *) echo "Invalid argument: $1. Use 'instrumented' or 'uninstrumented'." 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 From 8e5529af835d5c4949bf31b04889503fa2992dca Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Mon, 25 Nov 2024 11:07:40 -0300 Subject: [PATCH 08/11] remove .tool-version and remove download from run.sh to leverage only go mod tidy Signed-off-by: Igor Eulalio --- examples/dice/instrumented/.tool-versions | 1 - examples/dice/run.sh | 1 - 2 files changed, 2 deletions(-) delete mode 100644 examples/dice/instrumented/.tool-versions diff --git a/examples/dice/instrumented/.tool-versions b/examples/dice/instrumented/.tool-versions deleted file mode 100644 index f5264425ddd..00000000000 --- a/examples/dice/instrumented/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -golang 1.22.0 diff --git a/examples/dice/run.sh b/examples/dice/run.sh index 27cf0af192b..d3b81b46ab3 100755 --- a/examples/dice/run.sh +++ b/examples/dice/run.sh @@ -15,7 +15,6 @@ case "$1" in echo "Running instrumented example..." cd instrumented || exit source tidy.sh - go mod download source run.sh ;; uninstrumented) From 76e05785857aa1eed7821077cdd6276a226687f7 Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Mon, 25 Nov 2024 11:34:11 -0300 Subject: [PATCH 09/11] fix: add nolint for perfsec Signed-off-by: Igor Eulalio --- examples/dice/uninstrumented/rolldice.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dice/uninstrumented/rolldice.go b/examples/dice/uninstrumented/rolldice.go index 414cc7dbcb0..e69f7e88ad8 100644 --- a/examples/dice/uninstrumented/rolldice.go +++ b/examples/dice/uninstrumented/rolldice.go @@ -17,7 +17,7 @@ func rolldice(w http.ResponseWriter, r *http.Request) { var msg string if player := r.PathValue("player"); player != "" { - msg = fmt.Sprintf("%s is rolling the dice", player) + msg = fmt.Sprintf("%s is rolling the dice", player) //nolint:perfsprint } else { msg = "Anonymous player is rolling the dice" } From d13c0b857a67c79932541e3bd1fab32858844f0a Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Tue, 26 Nov 2024 09:05:31 -0300 Subject: [PATCH 10/11] chore: refactor uninstrumented example to match instrumented Signed-off-by: Igor Eulalio --- examples/dice/uninstrumented/rolldice.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/dice/uninstrumented/rolldice.go b/examples/dice/uninstrumented/rolldice.go index e69f7e88ad8..95ac6b4a516 100644 --- a/examples/dice/uninstrumented/rolldice.go +++ b/examples/dice/uninstrumented/rolldice.go @@ -4,7 +4,6 @@ package main import ( - "fmt" "io" "log" "math/rand" @@ -17,7 +16,7 @@ func rolldice(w http.ResponseWriter, r *http.Request) { var msg string if player := r.PathValue("player"); player != "" { - msg = fmt.Sprintf("%s is rolling the dice", player) //nolint:perfsprint + msg = player + " is rolling the dice" } else { msg = "Anonymous player is rolling the dice" } From f06e7d0073bf55371b19b223be80355b2873cfdd Mon Sep 17 00:00:00 2001 From: Igor Eulalio Date: Tue, 26 Nov 2024 19:18:00 -0300 Subject: [PATCH 11/11] fix: put right command for init source Signed-off-by: Igor Eulalio --- examples/dice/instrumented/init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dice/instrumented/init.sh b/examples/dice/instrumented/init.sh index 2aa81adbbcf..29b88d7cbba 100755 --- a/examples/dice/instrumented/init.sh +++ b/examples/dice/instrumented/init.sh @@ -3,4 +3,4 @@ #!/bin/bash -module go.opentelemetry.io/contrib/examples/dice/instrumented \ No newline at end of file +go mod init go.opentelemetry.io/contrib/examples/dice/instrumented \ No newline at end of file