Skip to content

Commit

Permalink
[DRAFT] [DO NOT MERGE] Entities support prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Dec 19, 2024
1 parent 1ddd969 commit 706c515
Show file tree
Hide file tree
Showing 134 changed files with 8,271 additions and 67 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ ocb:
# Definitions for ProtoBuf generation.

# The source directory for OTLP ProtoBufs.
OPENTELEMETRY_PROTO_SRC_DIR=pdata/internal/opentelemetry-proto
OPENTELEMETRY_PROTO_SRC_DIR?=pdata/internal/opentelemetry-proto

# The branch matching the current version of the proto to use
OPENTELEMETRY_PROTO_VERSION=v1.4.0
Expand Down Expand Up @@ -200,7 +200,7 @@ genproto: genproto-cleanup
# Call a sub-make to ensure OPENTELEMETRY_PROTO_FILES is populated
$(MAKE) genproto_sub
$(MAKE) fmt
$(MAKE) genproto-cleanup
# $(MAKE) genproto-cleanup

genproto_sub:
@echo Generating code for the following files:
Expand Down Expand Up @@ -234,8 +234,8 @@ genproto_sub:
cp -R $(PROTO_INTERMEDIATE_DIR)/$(PROTO_PACKAGE)/* $(PROTO_TARGET_GEN_DIR)/
rm -rf $(PROTO_INTERMEDIATE_DIR)/go.opentelemetry.io

@rm -rf $(OPENTELEMETRY_PROTO_SRC_DIR)/*
@rm -rf $(OPENTELEMETRY_PROTO_SRC_DIR)/.* > /dev/null 2>&1 || true
#@rm -rf $(OPENTELEMETRY_PROTO_SRC_DIR)/*
#@rm -rf $(OPENTELEMETRY_PROTO_SRC_DIR)/.* > /dev/null 2>&1 || true

# Generate structs, functions and tests for pdata package. Must be used after any changes
# to proto and after running `make genproto`
Expand Down
1 change: 1 addition & 0 deletions cmd/builder/test/core.builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ replaces:
- go.opentelemetry.io/collector/processor => ${WORKSPACE_DIR}/processor
- go.opentelemetry.io/collector/processor/processortest => ${WORKSPACE_DIR}/processor/processortest
- go.opentelemetry.io/collector/processor/xprocessor => ${WORKSPACE_DIR}/processor/xprocessor
- go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper => ${WORKSPACE_DIR}/processor/processorhelper/xprocessorhelper
- go.opentelemetry.io/collector/receiver => ${WORKSPACE_DIR}/receiver
- go.opentelemetry.io/collector/receiver/otlpreceiver => ${WORKSPACE_DIR}/receiver/otlpreceiver
- go.opentelemetry.io/collector/receiver/xreceiver => ${WORKSPACE_DIR}/receiver/xreceiver
Expand Down
2 changes: 2 additions & 0 deletions cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,5 @@ replace go.opentelemetry.io/collector/processor => ../../processor
replace go.opentelemetry.io/collector/consumer/consumererror => ../../consumer/consumererror

replace go.opentelemetry.io/collector/scraper => ../../scraper

replace go.opentelemetry.io/collector/pipeline/xpipeline => ../../pipeline/xpipeline
26 changes: 26 additions & 0 deletions cmd/mdatagen/internal/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Metadata struct {
SemConvVersion string `mapstructure:"sem_conv_version"`
// ResourceAttributes that can be emitted by the component.
ResourceAttributes map[AttributeName]Attribute `mapstructure:"resource_attributes"`
// Entities associated with the emitted resource attributes.
Entities []Entity `mapstructure:"entities"`
// Attributes emitted by one or more metrics.
Attributes map[AttributeName]Attribute `mapstructure:"attributes"`
// Metrics that can be emitted by the component.
Expand Down Expand Up @@ -65,6 +67,9 @@ func (md *Metadata) Validate() error {
if err := md.validateMetrics(); err != nil {
errs = errors.Join(errs, err)
}
if err := md.validateEntities(); err != nil {
errs = errors.Join(errs, err)
}
return errs
}

Expand Down Expand Up @@ -114,6 +119,18 @@ func (md *Metadata) validateMetrics() error {
return errs
}

func (md *Metadata) validateEntities() error {
var errs error
for _, entity := range md.Entities {
for _, attr := range append(entity.IDAttributes, entity.DescriptiveAttributes...) {
if _, ok := md.ResourceAttributes[attr]; !ok {
errs = errors.Join(errs, fmt.Errorf("undefined resource attribute: %v", attr))
}
}
}
return errs
}

func (md *Metadata) validateAttributes(usedAttrs map[AttributeName]bool) error {
var errs error
unusedAttrs := make([]AttributeName, 0, len(md.Attributes))
Expand Down Expand Up @@ -157,6 +174,15 @@ func validateMetrics(metrics map[MetricName]Metric, attributes map[AttributeName
return errs
}

type Entity struct {
// Type of the entity.
Type string `mapstructure:"type"`
// Identifying attributes of the entity.
IDAttributes []AttributeName `mapstructure:"id_attributes"`
// Descriptive attributes of the entity.
DescriptiveAttributes []AttributeName `mapstructure:"descriptive_attributes"`
}

type AttributeName string

func (mn AttributeName) Render() (string, error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/mdatagen/internal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (ms StabilityMap) Validate() error {
c != "traces" &&
c != "logs" &&
c != "profiles" &&
c != "entities" &&
c != "traces_to_traces" &&
c != "traces_to_metrics" &&
c != "traces_to_logs" &&
Expand Down
16 changes: 16 additions & 0 deletions cmd/mdatagen/internal/templates/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ func (rb *ResourceBuilder) Set{{ $name.Render }}(val {{ $attr.Type.Primitive }})
// Emit returns the built resource and resets the internal builder state.
func (rb *ResourceBuilder) Emit() pcommon.Resource {
r := rb.res
{{- range $entity := .Entities }}
{{- range $attr := .IDAttributes }}
_, found{{ $attr.Render }} := r.Attributes().Get("{{ $attr }}")
{{- end }}
if {{ range $i, $attr := .IDAttributes }}{{ if $i }}&& {{ end }}found{{ $attr.Render }} {{ else }}true {{ end }}{
ref := pcommon.NewResourceEntityRef()
ref.SetType("{{ $entity.Type }}")
ref.IdAttrKeys().Append({{ range $i, $attr := .IDAttributes }}{{ if $i }}, {{ end }}"{{ $attr }}"{{ end }})
{{- range $attr := .DescriptiveAttributes }}
if _, ok := r.Attributes().Get("{{ $attr }}"); ok {
ref.DescrAttrKeys().Append("{{ $attr }}")
}
{{- end }}
ref.CopyTo(r.Entities().AppendEmpty())
}
{{- end }}
rb.res = pcommon.NewResource()
return r
}
7 changes: 7 additions & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ resource_attributes:
# Should be used for deprecated optional resource_attributes that will be removed soon.
if_configured:

# Optional: list of entities associated with the produced resource.
entities:
- type: string
# Array of attribute names that are used to identify the entity.
id_attributes: [string]
# Optional: array of attribute names that are used to describe the entity.
descriptive_attributes: [string]

# Optional: map of attribute definitions with the key being the attribute name and value
# being described below.
Expand Down
1 change: 1 addition & 0 deletions cmd/otelcorecol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ replaces:
- go.opentelemetry.io/collector/processor/batchprocessor => ../../processor/batchprocessor
- go.opentelemetry.io/collector/processor/memorylimiterprocessor => ../../processor/memorylimiterprocessor
- go.opentelemetry.io/collector/processor/xprocessor => ../../processor/xprocessor
- go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper => ../../processor/processorhelper/xprocessorhelper
- go.opentelemetry.io/collector/receiver => ../../receiver
- go.opentelemetry.io/collector/receiver/nopreceiver => ../../receiver/nopreceiver
- go.opentelemetry.io/collector/receiver/otlpreceiver => ../../receiver/otlpreceiver
Expand Down
3 changes: 3 additions & 0 deletions cmd/otelcorecol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ require (
go.opentelemetry.io/collector/pdata/testdata v0.116.0 // indirect
go.opentelemetry.io/collector/pipeline v0.116.0 // indirect
go.opentelemetry.io/collector/pipeline/xpipeline v0.116.0 // indirect
go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper v0.116.0 // indirect
go.opentelemetry.io/collector/processor/processortest v0.116.0 // indirect
go.opentelemetry.io/collector/processor/xprocessor v0.116.0 // indirect
go.opentelemetry.io/collector/receiver/receivertest v0.116.0 // indirect
Expand Down Expand Up @@ -293,3 +294,5 @@ replace go.opentelemetry.io/collector/scraper => ../../scraper
replace go.opentelemetry.io/collector/semconv => ../../semconv

replace go.opentelemetry.io/collector/service => ../../service

replace go.opentelemetry.io/collector/processor/processorhelper/xprocessorhelper => ../../processor/processorhelper/xprocessorhelper
Loading

0 comments on commit 706c515

Please sign in to comment.