Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify dice example codebase by separate it into instrumented and uninstrumented for clarity #5873

Closed
wants to merge 10 commits into from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ go.work.sum

gen/

/example/dice/dice
/example/dice/instrumented
/example/dice/uninstrumented
/example/namedtracer/namedtracer
/example/otel-collector/otel-collector
/example/opencensus/opencensus
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Changed

- Modify the `example/dice` structure to `instrumented` and `uninstrumented` to showcase code example of both with and withou OTel. (#5812)
- Add a bash script to help ease the user on running the code example. (#5873)

### Changed

- Enable exemplars by default in `go.opentelemetry.io/otel/sdk/metric`. Exemplars can be disabled by setting `OTEL_METRICS_EXEMPLAR_FILTER=always_off` (#5778)
- `Logger.Enabled` in `go.opentelemetry.io/otel/log` now accepts a newly introduced `EnabledParameters` type instead of `Record`. (#5791)
- `FilterProcessor.Enabled` in `go.opentelemetry.io/otel/sdk/log/internal/x` now accepts `EnabledParameters` instead of `Record`. (#5791)
Expand Down
38 changes: 38 additions & 0 deletions example/dice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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
```





File renamed without changes.
22 changes: 11 additions & 11 deletions example/dice/go.mod → example/dice/instrumented/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module go.opentelemetry.io/otel/example/dice
module go.opentelemetry.io/otel/example/dice/instrumented

go 1.22

Expand All @@ -24,22 +24,22 @@ require (
golang.org/x/sys v0.25.0 // indirect
)

replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace
replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ./../../../exporters/stdout/stdouttrace

replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ../../exporters/stdout/stdoutmetric
replace go.opentelemetry.io/otel/exporters/stdout/stdoutmetric => ./../../../exporters/stdout/stdoutmetric

replace go.opentelemetry.io/otel => ../..
replace go.opentelemetry.io/otel => ./../../..

replace go.opentelemetry.io/otel/trace => ../../trace
replace go.opentelemetry.io/otel/trace => ./../../../trace

replace go.opentelemetry.io/otel/metric => ../../metric
replace go.opentelemetry.io/otel/metric => ./../../../metric

replace go.opentelemetry.io/otel/sdk/metric => ../../sdk/metric
replace go.opentelemetry.io/otel/sdk/metric => ./../../../sdk/metric

replace go.opentelemetry.io/otel/sdk => ../../sdk
replace go.opentelemetry.io/otel/sdk => ./../../../sdk

replace go.opentelemetry.io/otel/exporters/stdout/stdoutlog => ../../exporters/stdout/stdoutlog
replace go.opentelemetry.io/otel/exporters/stdout/stdoutlog => ./../../../exporters/stdout/stdoutlog

replace go.opentelemetry.io/otel/log => ../../log
replace go.opentelemetry.io/otel/log => ./../../../log

replace go.opentelemetry.io/otel/sdk/log => ../../sdk/log
replace go.opentelemetry.io/otel/sdk/log => ./../../../sdk/log
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions example/dice/run.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions example/dice/uninstrumented/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.opentelemetry.io/otel/example/dice/uninstrumented

go 1.22
65 changes: 65 additions & 0 deletions example/dice/uninstrumented/main.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions example/dice/uninstrumented/rolldice.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 2 additions & 1 deletion versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module-sets:
- go.opentelemetry.io/otel/bridge/opencensus/test
- go.opentelemetry.io/otel/bridge/opentracing
- go.opentelemetry.io/otel/bridge/opentracing/test
- go.opentelemetry.io/otel/example/dice
- go.opentelemetry.io/otel/example/dice/instrumented
- go.opentelemetry.io/otel/example/dice/uninstrumented
- go.opentelemetry.io/otel/example/namedtracer
- go.opentelemetry.io/otel/example/opencensus
- go.opentelemetry.io/otel/example/otel-collector
Expand Down