diff --git a/04-manual-instrumentation.md b/04-manual-instrumentation.md index 187a3de..12a14f1 100644 --- a/04-manual-instrumentation.md +++ b/04-manual-instrumentation.md @@ -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 @@ -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. + + + +### 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) { @@ -88,6 +99,8 @@ func causeDelay(ctx context.Context, rate int) { } ``` + + 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 diff --git a/app/backend4/main.go b/app/backend4/main.go index fe6ad6d..ac2842c 100644 --- a/app/backend4/main.go +++ b/app/backend4/main.go @@ -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" @@ -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