| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"log" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
"go.opentelemetry.io/otel/sdk/resource" |
| 14 |
) |
| 15 |
|
| 16 |
func ExampleNew() { |
| 17 |
res, err := resource.New( |
| 18 |
context.Background(), |
| 19 |
resource.WithFromEnv(), // Discover and provide attributes from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables. |
| 20 |
resource.WithTelemetrySDK(), // Discover and provide information about the OpenTelemetry SDK used. |
| 21 |
resource.WithProcess(), // Discover and provide process information. |
| 22 |
resource.WithOS(), // Discover and provide OS information. |
| 23 |
resource.WithContainer(), // Discover and provide container information. |
| 24 |
resource.WithHost(), // Discover and provide host information. |
| 25 |
resource.WithAttributes(attribute.String("foo", "bar")), // Add custom resource attributes. |
| 26 |
// resource.WithDetectors(thirdparty.Detector{}), // Bring your own external Detector implementation. |
| 27 |
) |
| 28 |
if errors.Is(err, resource.ErrPartialResource) || errors.Is(err, resource.ErrSchemaURLConflict) { |
| 29 |
log.Println(err) // Log non-fatal issues. |
| 30 |
} else if err != nil { |
| 31 |
log.Fatalln(err) // The error may be fatal. |
| 32 |
} |
| 33 |
|
| 34 |
// Now, you can use the resource (e.g. pass it to a tracer or meter provider). |
| 35 |
fmt.Println(res.SchemaURL()) |
| 36 |
} |