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

More improvements and bug fixes to Go example #618

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion getting-started-guides/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion getting-started-guides/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 24 additions & 29 deletions getting-started-guides/go/fibonacci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions getting-started-guides/go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand All @@ -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
Expand Down
Loading