From 31829c9ab62f6daf33b1ed38698fe3cf90cdca7d Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Thu, 13 Jun 2024 18:54:03 -0700 Subject: [PATCH] More improvements and bug fixes to Go example (#618) * More improvements and bug fixes * go mod tidy --- getting-started-guides/go/Dockerfile | 2 +- getting-started-guides/go/README.md | 2 +- getting-started-guides/go/fibonacci.go | 53 ++++++++++++-------------- getting-started-guides/go/go.mod | 4 +- 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/getting-started-guides/go/Dockerfile b/getting-started-guides/go/Dockerfile index 2ba3aa5d..c95ab989 100644 --- a/getting-started-guides/go/Dockerfile +++ b/getting-started-guides/go/Dockerfile @@ -1,6 +1,6 @@ # syntax=docker/dockerfile:1 -ARG GO_VERSION=1.20 +ARG GO_VERSION=1.22 FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build WORKDIR /src diff --git a/getting-started-guides/go/README.md b/getting-started-guides/go/README.md index 20cad638..309b4254 100644 --- a/getting-started-guides/go/README.md +++ b/getting-started-guides/go/README.md @@ -29,7 +29,7 @@ It demonstrates how to configure OpenTelemetry Go to send data to New Relic. in your web browser to ensure it is working. ```shell - go run *.go + go run . ``` 3. Experiment with providing different values for `n` in the query string. diff --git a/getting-started-guides/go/fibonacci.go b/getting-started-guides/go/fibonacci.go index b04a50ce..668be681 100644 --- a/getting-started-guides/go/fibonacci.go +++ b/getting-started-guides/go/fibonacci.go @@ -11,8 +11,9 @@ import ( "go.opentelemetry.io/contrib/bridges/otelslog" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/metric" - semconv "go.opentelemetry.io/otel/semconv/v1.17.0" + "go.opentelemetry.io/otel/trace" ) const ( @@ -97,41 +98,35 @@ func calculateFibonacci(r *http.Request, n int64) (int64, error) { ctx, span := tracer.Start(r.Context(), "fibonacci") defer span.End() - fibonacciSpanAttrs := []attribute.KeyValue{ - attribute.Int64("fibonacci.n", n), - } - - if n <= 1 || n > 90 { - log.Print(INPUT_IS_OUTSIDE_OF_RANGE) - - // Set error span attributes - fibonacciSpanAttrs = append(fibonacciSpanAttrs, - semconv.OtelStatusCodeError, - semconv.OtelStatusDescriptionKey.String(INPUT_IS_OUTSIDE_OF_RANGE), - ) - span.SetAttributes(fibonacciSpanAttrs...) + span.SetAttributes(attribute.Int64("fibonacci.n", n)) + if n < 1 || n > 90 { + err := errors.New("n must be between 1 and 90") + span.SetStatus(codes.Error, err.Error()) + span.RecordError(err, trace.WithStackTrace(true)) fibonacciInvocations.Add(ctx, 1, metric.WithAttributes(attribute.Bool("fibonacci.valid.n", false))) - - return 0, errors.New("invalid input") + msg := fmt.Sprintf("Failed to compute fib(%d).", n) + logger.InfoContext(ctx, msg, "fibonacci.n", n) + return 0, err } - var n2, n1 int64 = 0, 1 - for i := int64(2); i < n; i++ { - n2, n1 = n1, n1+n2 - } - res := n2 + n1 + var result = int64(1) + if n > 2 { + var a = int64(0) + var b = int64(1) - fibonacciSpanAttrs = append(fibonacciSpanAttrs, - attribute.Int64("fibonacci.result", res), - ) - span.SetAttributes(fibonacciSpanAttrs...) + for i := int64(1); i < n; i++ { + result = a + b + a = b + b = result + } + } - msg := fmt.Sprintf("Computed fib({%d}) = {%d}.", n, res) - logger.InfoContext(ctx, msg, "fibonacci.n", n, "fibonacci.result", res) + span.SetAttributes(attribute.Int64("fibonacci.result", result)) fibonacciInvocations.Add(ctx, 1, metric.WithAttributes(attribute.Bool("fibonacci.valid.n", true))) - - return res, nil + msg := fmt.Sprintf("Computed fib(%d) = %d.", n, result) + logger.InfoContext(ctx, msg, "fibonacci.n", n, "fibonacci.result", result) + return result, nil } func createHttpResponse(w http.ResponseWriter, statusCode int, res *responseObject) { diff --git a/getting-started-guides/go/go.mod b/getting-started-guides/go/go.mod index ab9d4cbe..e046f099 100644 --- a/getting-started-guides/go/go.mod +++ b/getting-started-guides/go/go.mod @@ -5,6 +5,7 @@ go 1.22 require ( go.opentelemetry.io/contrib/bridges/otelslog v0.2.0 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 + go.opentelemetry.io/contrib/instrumentation/runtime v0.52.0 go.opentelemetry.io/otel v1.27.0 go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0 go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0 @@ -14,6 +15,7 @@ require ( go.opentelemetry.io/otel/sdk v1.27.0 go.opentelemetry.io/otel/sdk/log v0.3.0 go.opentelemetry.io/otel/sdk/metric v1.27.0 + go.opentelemetry.io/otel/trace v1.27.0 ) require ( @@ -22,9 +24,7 @@ require ( github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect - go.opentelemetry.io/contrib/instrumentation/runtime v0.52.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0 // indirect - go.opentelemetry.io/otel/trace v1.27.0 // indirect go.opentelemetry.io/proto/otlp v1.2.0 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect