Skip to content

Commit 4f053f2

Browse files
Split dice example into instrumented and uninstrumented (#6300)
As discussed in this thread, we want to have two different examples, one with instrumentation and one without it. Fixes #6296 --------- Signed-off-by: Igor Eulalio <[email protected]> Co-authored-by: Robert Pająk <[email protected]>
1 parent b3ccbc6 commit 4f053f2

File tree

17 files changed

+206
-4
lines changed

17 files changed

+206
-4
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ coverage.*
1212
go.work
1313
go.work.sum
1414

15-
examples/dice/dice
15+
examples/dice/instrumented/instrumented
16+
examples/dice/uninstrumented/uninstrumented
1617
examples/namedtracer/namedtracer
1718
examples/otel-collector/otel-collector
1819
examples/opencensus/opencensus

examples/dice/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Dice example
2+
3+
This is the foundation example for [Getting Started](https://opentelemetry.io/docs/languages/go/getting-started/) with OpenTelemetry.
4+
5+
Below, you will see instructions on how to run this application, either with or without instrumentation.
6+
7+
## Usage
8+
9+
The `run.sh` script accepts one argument to determine which example to run:
10+
11+
- `uninstrumented`
12+
- `instrumented`
13+
14+
### Running the Uninstrumented Example
15+
16+
The uninstrumented example is a very simple dice application, without OpenTelemetry instrumentation.
17+
18+
To run the uninstrumented example, execute:
19+
20+
```bash
21+
./run.sh uninstrumented
22+
```
23+
24+
### Running the Instrumented Example
25+
26+
The instrumented example is exactly the same application, which includes OpenTelemetry instrumentation.
27+
28+
To run the instrumented example, execute:
29+
30+
```bash
31+
./run.sh instrumented
32+
```
File renamed without changes.

examples/dice/instrumented/get.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/bin/bash
5+
6+
go get "go.opentelemetry.io/otel" \
7+
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" \
8+
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" \
9+
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog" \
10+
"go.opentelemetry.io/otel/sdk/log" \
11+
"go.opentelemetry.io/otel/log/global" \
12+
"go.opentelemetry.io/otel/propagation" \
13+
"go.opentelemetry.io/otel/sdk/metric" \
14+
"go.opentelemetry.io/otel/sdk/resource" \
15+
"go.opentelemetry.io/otel/sdk/trace" \
16+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"\
17+
"go.opentelemetry.io/contrib/bridges/otelslog"

examples/dice/go.mod renamed to examples/dice/instrumented/go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module go.opentelemetry.io/contrib/examples/dice
1+
module go.opentelemetry.io/contrib/examples/dice/instrumented
22

33
go 1.22
44

@@ -26,6 +26,6 @@ require (
2626
)
2727

2828
replace (
29-
go.opentelemetry.io/contrib/bridges/otelslog => ../../bridges/otelslog
30-
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp => ../../instrumentation/net/http/otelhttp
29+
go.opentelemetry.io/contrib/bridges/otelslog => ../../../bridges/otelslog
30+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp => ../../../instrumentation/net/http/otelhttp
3131
)
File renamed without changes.

examples/dice/instrumented/init.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/bin/bash
5+
6+
go mod init go.opentelemetry.io/contrib/examples/dice/instrumented
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/dice/instrumented/run.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/bin/bash
5+
6+
go mod tidy
7+
export OTEL_RESOURCE_ATTRIBUTES="service.name=dice,service.version=0.1.0"
8+
go run .

examples/dice/instrumented/tidy.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/bin/bash
5+
6+
go mod tidy

examples/dice/run.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/bin/bash
5+
6+
# Check if at least one argument is provided
7+
if [ -z "$1" ]; then
8+
echo "Usage: $0 {instrumented|uninstrumented}"
9+
exit 1
10+
fi
11+
12+
# Switch based on the first argument
13+
case "$1" in
14+
instrumented)
15+
echo "Running instrumented example..."
16+
cd instrumented || exit
17+
source tidy.sh
18+
source run.sh
19+
;;
20+
uninstrumented)
21+
echo "Running uninstrumented example..."
22+
cd uninstrumented || exit
23+
source run.sh
24+
;;
25+
*)
26+
echo "Invalid argument: $1. Use 'instrumented' or 'uninstrumented'."
27+
exit 1
28+
;;
29+
esac

examples/dice/uninstrumented/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module go.opentelemetry.io/otel/example/dice/uninstrumented
2+
3+
go 1.22

examples/dice/uninstrumented/main.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"context"
8+
"log"
9+
"net"
10+
"net/http"
11+
"os"
12+
"os/signal"
13+
"time"
14+
)
15+
16+
func main() {
17+
if err := run(); err != nil {
18+
log.Fatalln(err)
19+
}
20+
}
21+
22+
func run() (err error) {
23+
// Handle SIGINT (CTRL+C) gracefully.
24+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
25+
defer stop()
26+
27+
// Start HTTP server.
28+
srv := &http.Server{
29+
Addr: ":8080",
30+
BaseContext: func(_ net.Listener) context.Context { return ctx },
31+
ReadTimeout: time.Second,
32+
WriteTimeout: 10 * time.Second,
33+
Handler: newHTTPHandler(),
34+
}
35+
srvErr := make(chan error, 1)
36+
go func() {
37+
log.Println("Running HTTP server...")
38+
srvErr <- srv.ListenAndServe()
39+
}()
40+
41+
// Wait for interruption.
42+
select {
43+
case err = <-srvErr:
44+
// Error when starting HTTP server.
45+
return
46+
case <-ctx.Done():
47+
// Wait for first CTRL+C.
48+
// Stop receiving signal notifications as soon as possible.
49+
stop()
50+
}
51+
52+
// When Shutdown is called, ListenAndServe immediately returns ErrServerClosed.
53+
err = srv.Shutdown(context.Background())
54+
return
55+
}
56+
57+
func newHTTPHandler() http.Handler {
58+
mux := http.NewServeMux()
59+
60+
// Register handlers.
61+
mux.HandleFunc("/rolldice/", rolldice)
62+
mux.HandleFunc("/rolldice/{player}", rolldice)
63+
64+
return mux
65+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"io"
8+
"log"
9+
"math/rand"
10+
"net/http"
11+
"strconv"
12+
)
13+
14+
func rolldice(w http.ResponseWriter, r *http.Request) {
15+
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.
16+
17+
var msg string
18+
if player := r.PathValue("player"); player != "" {
19+
msg = player + " is rolling the dice"
20+
} else {
21+
msg = "Anonymous player is rolling the dice"
22+
}
23+
log.Printf("%s, result: %d", msg, roll)
24+
25+
resp := strconv.Itoa(roll) + "\n"
26+
if _, err := io.WriteString(w, resp); err != nil {
27+
log.Printf("Write failed: %v", err)
28+
}
29+
}

examples/dice/uninstrumented/run.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright The OpenTelemetry Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#!/bin/bash
5+
6+
go run .

0 commit comments

Comments
 (0)