Skip to content

Commit

Permalink
Split dice example into instrumented and uninstrumented (open-telemet…
Browse files Browse the repository at this point in the history
…ry#6300)

As discussed in this thread, we want to have two different examples, one
with instrumentation and one without it.

Fixes
open-telemetry#6296

---------

Signed-off-by: Igor Eulalio <[email protected]>
Co-authored-by: Robert Pająk <[email protected]>
  • Loading branch information
IgorEulalio and pellared authored Dec 5, 2024
1 parent b3ccbc6 commit 4f053f2
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions examples/dice/README.md
Original file line number Diff line number Diff line change
@@ -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
```
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/dice/instrumented/get.sh
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 3 additions & 3 deletions examples/dice/go.mod → examples/dice/instrumented/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module go.opentelemetry.io/contrib/examples/dice
module go.opentelemetry.io/contrib/examples/dice/instrumented

go 1.22

Expand Down Expand Up @@ -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
)
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/dice/instrumented/init.sh
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/dice/instrumented/run.sh
Original file line number Diff line number Diff line change
@@ -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 .
6 changes: 6 additions & 0 deletions examples/dice/instrumented/tidy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

#!/bin/bash

go mod tidy
29 changes: 29 additions & 0 deletions examples/dice/run.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/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 examples/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
}
29 changes: 29 additions & 0 deletions examples/dice/uninstrumented/rolldice.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions examples/dice/uninstrumented/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

#!/bin/bash

go run .

0 comments on commit 4f053f2

Please sign in to comment.