Skip to content

Commit

Permalink
update manual instrumentation doc
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Mar 12, 2024
1 parent 42cfaec commit 3eb7be7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 14 additions & 1 deletion 04-manual-instrumentation.md
Expand Up @@ -56,6 +56,10 @@ To simulate a more complex behaviour, we find a `causeError` function in the `/r

Therefore we have to make sure that the context, which was previously created with the rootspan, is passed to this function.

### RecordError and set span status

RecordError will record err as an exception span event for this span. An additional call to SetStatus is required if the Status of the Span should be set to Error, as this method does not change the Span status. If this span is not being recorded or err is nil then this method does nothing.

```diff
func causeError(ctx context.Context, rate int) error {
+ var span trace.Span
Expand All @@ -67,13 +71,20 @@ func causeError(ctx context.Context, rate int) error {
if randomNumber < rate {
err := fmt.Errorf("internal server error")
+ span.RecordError(err)
+ span.SetStatus(codes.Error, "some error occured")
return err
}
return nil
}
```

In the same execution path we also find a function that ensures high delays of our `/rolldice` endpoint with a fixed probability.
In the same execution path we also find a function that ensures high delays of our `/rolldice` endpoint with a fixed probability.

<TODO: Insert Image>

### Add a custom Event

AddEvent adds an event with the provided name and optionsAddEvent adds an event with the provided name and options.

```diff
func causeDelay(ctx context.Context, rate int) {
Expand All @@ -88,6 +99,8 @@ func causeDelay(ctx context.Context, rate int) {
}
```

<TODO: Insert Image>

Once the code has been instrumented, we can use `go mod tidy` to update the existing `go.mod` file and start testing our application.

## Configuring an OTLP exporter and setting the endpoint
Expand Down
2 changes: 2 additions & 0 deletions app/backend4/main.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -119,6 +120,7 @@ func causeError(ctx context.Context, rate int) error {
if randomNumber < rate {
err := fmt.Errorf("internal server error")
span.RecordError(err)
span.SetStatus(codes.Error, "some error occured")
return err
}
return nil
Expand Down

0 comments on commit 3eb7be7

Please sign in to comment.