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

Split dice example into instrumented and uninstrumented #6300

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/dice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Instructions on how to run instrumented and uninstrumented examples.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved

## Prerequisites

- [Go](https://golang.org/dl/) installed on your system.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
- Necessary permissions to execute shell scripts.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved

## 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.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved

To run the instrumented example, execute:

```bash
./run.sh instrumented
```

### Running the Uninstrumented Example
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved

The uninstrumented example is the exact same application, without OTEL instrumentation.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved

To run the instrumented example, execute:

```bash
./run.sh uninstrumented
```




IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/dice/go.mod → examples/dice/instrumented/go.mod
Original file line number Diff line number Diff line change
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
svrnm marked this conversation as resolved.
Show resolved Hide resolved
)
File renamed without changes.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
File renamed without changes.
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
33 changes: 33 additions & 0 deletions examples/dice/run.sh
svrnm marked this conversation as resolved.
Show resolved Hide resolved
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
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
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 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
}
30 changes: 30 additions & 0 deletions examples/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) //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 = 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)
}
}
Loading