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

Merged
merged 18 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
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
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved

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
```
1 change: 1 addition & 0 deletions examples/dice/instrumented/.tool-versions
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.22.0
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/dice/instrumented/get.sh
pellared marked this conversation as resolved.
Show resolved Hide resolved
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
svrnm marked this conversation as resolved.
Show resolved Hide resolved
)
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/dice/instrumented/init.sh
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
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

module go.opentelemetry.io/contrib/examples/dice/instrumented
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.
8 changes: 8 additions & 0 deletions examples/dice/instrumented/run.sh
pellared marked this conversation as resolved.
Show resolved Hide resolved
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
pellared marked this conversation as resolved.
Show resolved Hide resolved
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
30 changes: 30 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,30 @@
# 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
go mod download
IgorEulalio marked this conversation as resolved.
Show resolved Hide resolved
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
}
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)
}
}
6 changes: 6 additions & 0 deletions examples/dice/uninstrumented/run.sh
pellared marked this conversation as resolved.
Show resolved Hide resolved
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 .