1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/example/fib/app.go
Tyler Yahn 04de34a2d6
Update the website getting started docs (#2203)
* Update the website getting started docs

Add a new example, fib, that contains an application for the computation
of Fibonacci numbers. Use this example to update the website getting
started documentation.

* Revise docs english

* Update example/fib/go.mod

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Add a What's Next section

* Clean up intro

* Apply suggestions from code review

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Apply feedback

* Return from Poll on error

* Update website_docs/getting-started.md

Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>

* Add root and parent relationship info

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
2021-09-02 08:22:41 -07:00

105 lines
2.5 KiB
Go

// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"io"
"log"
"strconv"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// name is the Tracer name used to identify this instrumentation library.
const name = "fib"
// App is an Fibonacci computation application.
type App struct {
r io.Reader
l *log.Logger
}
// NewApp returns a new App.
func NewApp(r io.Reader, l *log.Logger) *App {
return &App{r: r, l: l}
}
// Run starts polling users for Fibonacci number requests and writes results.
func (a *App) Run(ctx context.Context) error {
for {
var span trace.Span
ctx, span = otel.Tracer(name).Start(ctx, "Run")
n, err := a.Poll(ctx)
if err != nil {
span.End()
return err
}
a.Write(ctx, n)
span.End()
}
}
// Poll asks a user for input and returns the request.
func (a *App) Poll(ctx context.Context) (uint, error) {
_, span := otel.Tracer(name).Start(ctx, "Poll")
defer span.End()
a.l.Print("What Fibonacci number would you like to know: ")
var n uint
_, err := fmt.Fscanf(a.r, "%d", &n)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return 0, err
}
// Store n as a string to not overflow an int64.
nStr := strconv.FormatUint(uint64(n), 10)
span.SetAttributes(attribute.String("request.n", nStr))
return n, nil
}
// Write writes the n-th Fibonacci number back to the user.
func (a *App) Write(ctx context.Context, n uint) {
var span trace.Span
ctx, span = otel.Tracer(name).Start(ctx, "Write")
defer span.End()
f, err := func(ctx context.Context) (uint64, error) {
_, span := otel.Tracer(name).Start(ctx, "Fibonacci")
defer span.End()
f, err := Fibonacci(n)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
return f, err
}(ctx)
if err != nil {
a.l.Printf("Fibonacci(%d): %v\n", n, err)
} else {
a.l.Printf("Fibonacci(%d) = %d\n", n, f)
}
}