Skip to content

Commit

Permalink
Support multiple entity refs in the Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed Oct 17, 2024
1 parent 781faa5 commit b4611c8
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 167 deletions.
9 changes: 9 additions & 0 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ func filteredToFront(slice []KeyValue, keep Filter) int {
return j
}

func (l *Set) Clone() Set {
s, _ := l.Filter(
func(KeyValue) bool {
return true
},
)
return s
}

// Filter returns a filtered copy of this Set. See the documentation for
// NewSetWithSortableFiltered for more details.
func (l *Set) Filter(re Filter) (Set, []KeyValue) {
Expand Down
18 changes: 14 additions & 4 deletions exporters/otlp/otlptrace/internal/tracetransform/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ func Resource(r *resource.Resource) *resourcepb.Resource {
}

attrs := Iterator(r.Iter())
entityId := Iterator(r.EntityId().Iter())

return &resourcepb.Resource{
out := &resourcepb.Resource{
Attributes: attrs,
EntityType: r.EntityType(),
EntityId: entityId,
}

for _, entity := range r.EntityRefs() {
out.Entities = append(
out.Entities, &resourcepb.ResourceEntityRef{
SchemaUrl: entity.SchemaUrl,
Type: entity.Type,
IdAttrKeys: entity.IdKeys,
DescrAttrKeys: entity.AttrsKeys,
},
)
}

return out
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
11 changes: 5 additions & 6 deletions sdk/resource/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk"
"go.opentelemetry.io/otel/sdk/resource/internal"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
)

Expand Down Expand Up @@ -100,12 +99,12 @@ func (sd stringDetector) Detect(ctx context.Context) (*Resource, error) {
return nil, fmt.Errorf("invalid attribute: %q -> %q", a.Key, a.Value.Emit())
}
id := attribute.NewSet(sd.K.String(value))
entity := internal.EntityData{
Type: sd.entityType,
Id: id,
Attrs: id,
entity := Entity{
Type: sd.entityType,
Id: id,
SchemaURL: sd.schemaURL,
}
return NewWithEntity(sd.schemaURL, &entity), nil
return NewWithEntities([]Entity{entity})
}

// Detect implements Detector.
Expand Down
34 changes: 17 additions & 17 deletions sdk/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type config struct {
// SchemaURL to associate with the Resource.
schemaURL string

entityType string
entityId []attribute.KeyValue
//entityType string
//entityId []attribute.KeyValue
}

// Option is the interface that applies a configuration option.
Expand Down Expand Up @@ -96,21 +96,21 @@ func (o schemaURLOption) apply(cfg config) config {
return cfg
}

// WithEntity sets the schema URL for the configured resource.
func WithEntity(entityType string, entityId ...attribute.KeyValue) Option {
return entityOption{entityType, entityId}
}

type entityOption struct {
entityType string
entityId []attribute.KeyValue
}

func (o entityOption) apply(cfg config) config {
cfg.entityType = o.entityType
cfg.entityId = o.entityId
return cfg
}
//// WithEntity sets the schema URL for the configured resource.
//func WithEntity(entityType string, entityId ...attribute.KeyValue) Option {
// return entityOption{entityType, entityId}
//}
//
//type entityOption struct {
// entityType string
// entityId []attribute.KeyValue
//}
//
//func (o entityOption) apply(cfg config) config {
// cfg.entityType = o.entityType
// cfg.entityId = o.entityId
// return cfg
//}

// WithOS adds all the OS attributes to the configured Resource.
// See individual WithOS* functions to configure specific attributes.
Expand Down
10 changes: 10 additions & 0 deletions sdk/resource/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package resource

import "go.opentelemetry.io/otel/attribute"

type Entity struct {
Type string
Id attribute.Set
Attrs attribute.Set
SchemaURL string
}
29 changes: 29 additions & 0 deletions sdk/resource/entityref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package resource

import "go.opentelemetry.io/otel/attribute"

type resourceEntityRef struct {
SchemaUrl string

// Defines the entity type, e.g "service", "k8s.pod", etc.
Type string

// Set of Resource attribute keys that identify the entity.
Id map[attribute.Key]bool

// Set of Resource attribute keys that describe the entity.
Attrs map[attribute.Key]bool

// Id and Attrs materialized as slices for faster exporting.
IdKeys []string
AttrsKeys []string
}

func (r *resourceEntityRef) materialize() {
for k := range r.Id {
r.IdKeys = append(r.IdKeys, string(k))
}
for k := range r.Attrs {
r.AttrsKeys = append(r.AttrsKeys, string(k))
}
}
62 changes: 0 additions & 62 deletions sdk/resource/internal/entity.go

This file was deleted.

Loading

0 comments on commit b4611c8

Please sign in to comment.