| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package x // import "go.opentelemetry.io/contrib/otelconf/x" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
"go.opentelemetry.io/otel/sdk/resource" |
| 11 |
|
| 12 |
"go.opentelemetry.io/contrib/otelconf/internal/kv" |
| 13 |
) |
| 14 |
|
| 15 |
func resourceOpts(detectors []ExperimentalResourceDetector) []resource.Option { |
| 16 |
opts := []resource.Option{} |
| 17 |
for _, d := range detectors { |
| 18 |
if d.Container != nil { |
| 19 |
opts = append(opts, resource.WithContainer()) |
| 20 |
} |
| 21 |
if d.Host != nil { |
| 22 |
opts = append(opts, resource.WithHost(), resource.WithOS()) |
| 23 |
} |
| 24 |
if d.Process != nil { |
| 25 |
opts = append(opts, resource.WithProcess()) |
| 26 |
} |
| 27 |
if d.Service != nil { |
| 28 |
opts = append(opts, resource.WithService()) |
| 29 |
} |
| 30 |
} |
| 31 |
return opts |
| 32 |
} |
| 33 |
|
| 34 |
func newResource(r *Resource) (*resource.Resource, error) { |
| 35 |
if r == nil { |
| 36 |
return resource.Default(), nil |
| 37 |
} |
| 38 |
|
| 39 |
attrs := make([]attribute.KeyValue, 0, len(r.Attributes)) |
| 40 |
for _, v := range r.Attributes { |
| 41 |
attrs = append(attrs, kv.FromNameValue(v.Name, v.Value)) |
| 42 |
} |
| 43 |
|
| 44 |
var schema string |
| 45 |
if r.SchemaUrl != nil { |
| 46 |
schema = *r.SchemaUrl |
| 47 |
} |
| 48 |
opts := []resource.Option{ |
| 49 |
resource.WithAttributes(attrs...), |
| 50 |
resource.WithSchemaURL(schema), |
| 51 |
} |
| 52 |
|
| 53 |
if r.DetectionDevelopment != nil { |
| 54 |
opts = append(opts, resourceOpts(r.DetectionDevelopment.Detectors)...) |
| 55 |
} |
| 56 |
|
| 57 |
return resource.New(context.Background(), opts...) |
| 58 |
} |