| 1 |
// Code generated by mdatagen. DO NOT EDIT. |
| 2 |
|
| 3 |
package {{ .Package }} |
| 4 |
|
| 5 |
import ( |
| 6 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 7 |
{{- if .HasEntities }} |
| 8 |
"go.opentelemetry.io/collector/pdata/xpdata/entity" |
| 9 |
{{- end }} |
| 10 |
) |
| 11 |
|
| 12 |
// ResourceBuilder is a helper struct to build resources predefined in metadata.yaml. |
| 13 |
// The ResourceBuilder is not thread-safe and must not to be used in multiple goroutines. |
| 14 |
type ResourceBuilder struct { |
| 15 |
config ResourceAttributesConfig |
| 16 |
res pcommon.Resource |
| 17 |
} |
| 18 |
|
| 19 |
// NewResourceBuilder creates a new ResourceBuilder. This method should be called on the start of the application. |
| 20 |
func NewResourceBuilder(rac ResourceAttributesConfig) *ResourceBuilder { |
| 21 |
return &ResourceBuilder{ |
| 22 |
config: rac, |
| 23 |
res: pcommon.NewResource(), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
{{- range $name, $attr := .ResourceAttributes }} |
| 28 |
{{- range $attr.Enum }} |
| 29 |
// Set{{ $name.Render }}{{ . | publicVar }} sets "{{ $name }}={{ . }}" attribute. |
| 30 |
func (rb *ResourceBuilder) Set{{ $name.Render }}{{ . | publicVar }}() { |
| 31 |
if rb.config.{{ $name.Render }}.Enabled { |
| 32 |
rb.res.Attributes().PutStr("{{ $name }}", "{{ . }}") |
| 33 |
} |
| 34 |
} |
| 35 |
{{- else }} |
| 36 |
// Set{{ $name.Render }} sets provided value as "{{ $name }}" attribute. |
| 37 |
func (rb *ResourceBuilder) Set{{ $name.Render }}(val {{ $attr.Type.Primitive }}) { |
| 38 |
if rb.config.{{ $name.Render }}.Enabled { |
| 39 |
{{- if or (eq $attr.Type.String "Bytes") (eq $attr.Type.String "Slice") (eq $attr.Type.String "Map") }} |
| 40 |
rb.res.Attributes().PutEmpty{{ $attr.Type }}("{{ $name }}").FromRaw(val) |
| 41 |
{{- else }} |
| 42 |
rb.res.Attributes().Put{{ $attr.Type }}("{{ $name }}", val) |
| 43 |
{{- end }} |
| 44 |
} |
| 45 |
} |
| 46 |
{{- end }} |
| 47 |
{{ end }} |
| 48 |
|
| 49 |
{{- if .HasEntities }} |
| 50 |
|
| 51 |
{{ range $entity := .Entities }} |
| 52 |
// AssociateWith{{ $entity.Type | publicVar }} associates the resource with entity type "{{ $entity.Type }}". |
| 53 |
// This method is experimental and will be replaced with an entity builder pattern in the future. |
| 54 |
// However, for now, it allows associating resources with entities and producing correct entity references. |
| 55 |
func (rb *ResourceBuilder) AssociateWith{{ $entity.Type | publicVar }}() { |
| 56 |
entityRef := entity.ResourceEntityRefs(rb.res).AppendEmpty() |
| 57 |
entityRef.SetType("{{ $entity.Type }}") |
| 58 |
{{- if $entity.Identity }} |
| 59 |
idKeys := entityRef.IdKeys() |
| 60 |
{{- range $entity.Identity }} |
| 61 |
idKeys.Append("{{ .Ref }}") |
| 62 |
{{- end }} |
| 63 |
{{- end }} |
| 64 |
{{- if $entity.Description }} |
| 65 |
descKeys := entityRef.DescriptionKeys() |
| 66 |
{{- range $entity.Description }} |
| 67 |
descKeys.Append("{{ .Ref }}") |
| 68 |
{{- end }} |
| 69 |
{{- end }} |
| 70 |
} |
| 71 |
{{ end }} |
| 72 |
{{- end }} |
| 73 |
|
| 74 |
// Emit returns the built resource and resets the internal builder state. |
| 75 |
func (rb *ResourceBuilder) Emit() pcommon.Resource { |
| 76 |
r := rb.res |
| 77 |
rb.res = pcommon.NewResource() |
| 78 |
return r |
| 79 |
} |