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 c85ac65
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 169 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
16 changes: 13 additions & 3 deletions example/dice/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,24 @@ func setupOTelSDK(ctx context.Context, serviceName, serviceVersion string) (
}

func newResource(serviceName, serviceVersion string) (*resource.Resource, error) {
return resource.Merge(
resource.Default(),
resource.NewWithAttributes(
res, err := resource.New(
context.Background(),
resource.WithHost(),
resource.WithHostID(),
resource.WithEntity(
semconv.SchemaURL,
"service",
semconv.ServiceName(serviceName),
semconv.ServiceVersion(serviceVersion),
),
)
if err != nil {
return nil, err
}
return resource.Merge(
resource.Default(),
res,
)
}

func newPropagator() propagation.TextMapPropagator {
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
18 changes: 11 additions & 7 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 @@ -70,7 +69,12 @@ func (telemetrySDK) Detect(context.Context) (*Resource, error) {

// Detect returns a *Resource that describes the host being run on.
func (host) Detect(ctx context.Context) (*Resource, error) {
return StringDetector(semconv.SchemaURL, semconv.HostNameKey, os.Hostname).Detect(ctx)
return StringDetectorWithEntity(
semconv.SchemaURL,
"host",
semconv.HostNameKey,
os.Hostname,
).Detect(ctx)
}

// StringDetector returns a Detector that will produce a *Resource
Expand Down Expand Up @@ -100,12 +104,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
50 changes: 38 additions & 12 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,22 +96,48 @@ 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 entityDetector struct {
SchemaURL string
Type string
Id []attribute.KeyValue
Attrs []attribute.KeyValue
}

type entityOption struct {
entityType string
entityId []attribute.KeyValue
func (e entityDetector) Detect(ctx context.Context) (*Resource, error) {
return NewWithEntities(
[]Entity{
{
Type: e.Type,
Id: attribute.NewSet(e.Id...),
Attrs: attribute.NewSet(e.Attrs...),
SchemaURL: e.SchemaURL,
},
},
)
}

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(schemaURL string, entityType string, Id ...attribute.KeyValue) Option {
return WithDetectors(
entityDetector{
SchemaURL: schemaURL,
Type: entityType,
Id: Id,
},
)
}

//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.
func WithOS() Option {
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))
}
}
10 changes: 7 additions & 3 deletions sdk/resource/host_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ func (hostIDDetector) Detect(ctx context.Context) (*Resource, error) {
return nil, err
}

return NewWithAttributes(
return StringDetectorWithEntity(
semconv.SchemaURL,
semconv.HostID(hostID),
), nil
"host",
semconv.HostIDKey,
func() (string, error) {
return hostID, nil
},
).Detect(ctx)
}
62 changes: 0 additions & 62 deletions sdk/resource/internal/entity.go

This file was deleted.

Loading

0 comments on commit c85ac65

Please sign in to comment.