| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelconftelemetry |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
sdkresource "go.opentelemetry.io/otel/sdk/resource" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component" |
| 14 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 15 |
"go.opentelemetry.io/collector/service/internal/resource" |
| 16 |
"go.opentelemetry.io/collector/service/telemetry" |
| 17 |
) |
| 18 |
|
| 19 |
func createResource( |
| 20 |
_ context.Context, |
| 21 |
set telemetry.Settings, |
| 22 |
componentConfig component.Config, |
| 23 |
) (pcommon.Resource, error) { |
| 24 |
res := newResource(set, componentConfig.(*Config)) |
| 25 |
pcommonRes := pcommon.NewResource() |
| 26 |
for _, keyValue := range res.Attributes() { |
| 27 |
key := string(keyValue.Key) |
| 28 |
pcommonRes.Attributes().PutStr(key, mustAttributeValueString(key, keyValue.Value)) |
| 29 |
} |
| 30 |
return pcommonRes, nil |
| 31 |
} |
| 32 |
|
| 33 |
func newResource(set telemetry.Settings, cfg *Config) *sdkresource.Resource { |
| 34 |
return resource.New(set.BuildInfo, cfg.Resource) |
| 35 |
} |
| 36 |
|
| 37 |
func mustAttributeValueString(k string, v attribute.Value) string { |
| 38 |
if v.Type() != attribute.STRING { |
| 39 |
|
| 40 |
panic(fmt.Errorf("attribute %q: expected string, got %s", k, v.Type())) |
| 41 |
} |
| 42 |
return v.AsString() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func pcommonAttrsToOTelAttrs(resource *pcommon.Resource) []attribute.KeyValue { |
| 47 |
var result []attribute.KeyValue |
| 48 |
if resource != nil { |
| 49 |
attrs := resource.Attributes() |
| 50 |
attrs.Range(func(k string, v pcommon.Value) bool { |
| 51 |
result = append(result, attribute.String(k, v.AsString())) |
| 52 |
return true |
| 53 |
}) |
| 54 |
} |
| 55 |
return result |
| 56 |
} |