You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-07-13 01:00:22 +02:00
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>
This commit is contained in:
101
example/fib/main.go
Normal file
101
example/fib/main.go
Normal file
@ -0,0 +1,101 @@
|
||||
// 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"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
)
|
||||
|
||||
// newExporter returns a console exporter.
|
||||
func newExporter(w io.Writer) (trace.SpanExporter, error) {
|
||||
return stdouttrace.New(
|
||||
stdouttrace.WithWriter(w),
|
||||
// Use human readable output.
|
||||
stdouttrace.WithPrettyPrint(),
|
||||
// Do not print timestamps for the demo.
|
||||
stdouttrace.WithoutTimestamps(),
|
||||
)
|
||||
}
|
||||
|
||||
// newResource returns a resource describing this application.
|
||||
func newResource() *resource.Resource {
|
||||
r, _ := resource.Merge(
|
||||
resource.Default(),
|
||||
resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
semconv.ServiceNameKey.String("fib"),
|
||||
semconv.ServiceVersionKey.String("v0.1.0"),
|
||||
attribute.String("environment", "demo"),
|
||||
),
|
||||
)
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
l := log.New(os.Stdout, "", 0)
|
||||
|
||||
// Write telemetry data to a file.
|
||||
f, err := os.Create("traces.txt")
|
||||
if err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
exp, err := newExporter(f)
|
||||
if err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
|
||||
tp := trace.NewTracerProvider(
|
||||
trace.WithBatcher(exp),
|
||||
trace.WithResource(newResource()),
|
||||
)
|
||||
defer func() {
|
||||
if err := tp.Shutdown(context.Background()); err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
}()
|
||||
otel.SetTracerProvider(tp)
|
||||
|
||||
sigCh := make(chan os.Signal, 1)
|
||||
signal.Notify(sigCh, os.Interrupt)
|
||||
|
||||
errCh := make(chan error)
|
||||
app := NewApp(os.Stdin, l)
|
||||
go func() {
|
||||
errCh <- app.Run(context.Background())
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-sigCh:
|
||||
l.Println("\ngoodbye")
|
||||
return
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user