2021-09-02 17:22:41 +02:00
|
|
|
// 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"
|
2023-08-07 18:30:40 +02:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
|
2021-09-02 17:22:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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,
|
2023-02-07 23:42:47 +02:00
|
|
|
semconv.ServiceName("fib"),
|
|
|
|
semconv.ServiceVersion("v0.1.0"),
|
2021-09-02 17:22:41 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|