|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "io" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "os/signal" |
| 23 | + |
| 24 | + "go.opentelemetry.io/otel" |
| 25 | + "go.opentelemetry.io/otel/attribute" |
| 26 | + "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" |
| 27 | + "go.opentelemetry.io/otel/sdk/resource" |
| 28 | + "go.opentelemetry.io/otel/sdk/trace" |
| 29 | + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" |
| 30 | +) |
| 31 | + |
| 32 | +// newExporter returns a console exporter. |
| 33 | +func newExporter(w io.Writer) (trace.SpanExporter, error) { |
| 34 | + return stdouttrace.New( |
| 35 | + stdouttrace.WithWriter(w), |
| 36 | + // Use human readable output. |
| 37 | + stdouttrace.WithPrettyPrint(), |
| 38 | + // Do not print timestamps for the demo. |
| 39 | + stdouttrace.WithoutTimestamps(), |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +// newResource returns a resource describing this application. |
| 44 | +func newResource() *resource.Resource { |
| 45 | + r, _ := resource.Merge( |
| 46 | + resource.Default(), |
| 47 | + resource.NewWithAttributes( |
| 48 | + semconv.SchemaURL, |
| 49 | + semconv.ServiceNameKey.String("fib"), |
| 50 | + semconv.ServiceVersionKey.String("v0.1.0"), |
| 51 | + attribute.String("environment", "demo"), |
| 52 | + ), |
| 53 | + ) |
| 54 | + return r |
| 55 | +} |
| 56 | + |
| 57 | +func main() { |
| 58 | + l := log.New(os.Stdout, "", 0) |
| 59 | + |
| 60 | + // Write telemetry data to a file. |
| 61 | + f, err := os.Create("traces.txt") |
| 62 | + if err != nil { |
| 63 | + l.Fatal(err) |
| 64 | + } |
| 65 | + defer f.Close() |
| 66 | + |
| 67 | + exp, err := newExporter(f) |
| 68 | + if err != nil { |
| 69 | + l.Fatal(err) |
| 70 | + } |
| 71 | + |
| 72 | + tp := trace.NewTracerProvider( |
| 73 | + trace.WithBatcher(exp), |
| 74 | + trace.WithResource(newResource()), |
| 75 | + ) |
| 76 | + defer func() { |
| 77 | + if err := tp.Shutdown(context.Background()); err != nil { |
| 78 | + l.Fatal(err) |
| 79 | + } |
| 80 | + }() |
| 81 | + otel.SetTracerProvider(tp) |
| 82 | + |
| 83 | + sigCh := make(chan os.Signal, 1) |
| 84 | + signal.Notify(sigCh, os.Interrupt) |
| 85 | + |
| 86 | + errCh := make(chan error) |
| 87 | + app := NewApp(os.Stdin, l) |
| 88 | + go func() { |
| 89 | + errCh <- app.Run(context.Background()) |
| 90 | + }() |
| 91 | + |
| 92 | + select { |
| 93 | + case <-sigCh: |
| 94 | + l.Println("\ngoodbye") |
| 95 | + return |
| 96 | + case err := <-errCh: |
| 97 | + if err != nil { |
| 98 | + l.Fatal(err) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments