| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package x_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel" |
| 13 |
"go.opentelemetry.io/otel/log/global" |
| 14 |
|
| 15 |
"go.opentelemetry.io/contrib/otelconf" |
| 16 |
) |
| 17 |
|
| 18 |
func Example() { |
| 19 |
b, err := os.ReadFile(filepath.Join("testdata", "v1.0.0.yaml")) |
| 20 |
if err != nil { |
| 21 |
log.Fatal(err) |
| 22 |
} |
| 23 |
|
| 24 |
// Parse a configuration file into an OpenTelemetryConfiguration model. |
| 25 |
c, err := otelconf.ParseYAML(b) |
| 26 |
if err != nil { |
| 27 |
log.Fatal(err) |
| 28 |
} |
| 29 |
|
| 30 |
// Create SDK components with the parsed configuration. |
| 31 |
s, err := otelconf.NewSDK(otelconf.WithOpenTelemetryConfiguration(*c)) |
| 32 |
if err != nil { |
| 33 |
log.Fatal(err) |
| 34 |
} |
| 35 |
|
| 36 |
// Ensure shutdown is eventually called for all components of the SDK. |
| 37 |
defer func() { |
| 38 |
if err := s.Shutdown(context.Background()); err != nil { |
| 39 |
log.Fatal(err) |
| 40 |
} |
| 41 |
}() |
| 42 |
|
| 43 |
// Set the global providers. |
| 44 |
otel.SetTracerProvider(s.TracerProvider()) |
| 45 |
otel.SetMeterProvider(s.MeterProvider()) |
| 46 |
global.SetLoggerProvider(s.LoggerProvider()) |
| 47 |
// Set the global propagator. |
| 48 |
otel.SetTextMapPropagator(s.Propagator()) |
| 49 |
} |