Skip to content

Commit

Permalink
Encode Entity in JSON console output
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed Feb 9, 2024
1 parent fd15192 commit 77e60bf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
15 changes: 12 additions & 3 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ func (l *Set) Value(k Key) (Value, bool) {
rValue := l.equivalent.reflectValue()
vlen := rValue.Len()

idx := sort.Search(vlen, func(idx int) bool {
return rValue.Index(idx).Interface().(KeyValue).Key >= k
})
idx := sort.Search(
vlen, func(idx int) bool {
return rValue.Index(idx).Interface().(KeyValue).Key >= k
},
)
if idx >= vlen {
return Value{}, false
}
Expand Down Expand Up @@ -427,6 +429,13 @@ func (l *Set) MarshalJSON() ([]byte, error) {
return json.Marshal(l.equivalent.iface)
}

// MarshalableToJSON returns a value that is marshalable to JSON form.
// The returned value can be passed as an argument to json.Marshal() or
// be placed in any other structure that itself is passed to json.Marshal().
func (l *Set) MarshalableToJSON() any {
return l.equivalent.iface
}

// MarshalLog is the marshaling function used by the logging system to represent this Set.
func (l Set) MarshalLog() interface{} {
kvs := make(map[string]string)
Expand Down
2 changes: 2 additions & 0 deletions exporters/otlp/otlptrace/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ replace go.opentelemetry.io/otel/sdk => ../../../sdk
replace go.opentelemetry.io/otel/trace => ../../../trace

replace go.opentelemetry.io/otel/metric => ../../../metric

replace go.opentelemetry.io/proto/otlp => ../../../../opentelemetry-proto-go/otlp/
9 changes: 6 additions & 3 deletions exporters/otlp/otlptrace/internal/tracetransform/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ func Resource(r *resource.Resource) *resourcepb.Resource {

entity := r.Entity()
entityId := Iterator(entity.IdIter())
entityId = entityId

// TODO: set entityId and entity.Type() in the Resource{} below.
return &resourcepb.Resource{Attributes: ResourceAttributes(r)}
return &resourcepb.Resource{
Attributes: ResourceAttributes(r),
DroppedAttributesCount: 0,
EntityType: entity.Type(),
EntityId: entityId,
}
}
29 changes: 25 additions & 4 deletions sdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package resource // import "go.opentelemetry.io/otel/sdk/resource"

import (
"context"
"encoding/json"
"errors"
"sync"

Expand Down Expand Up @@ -156,11 +157,11 @@ func (r *Resource) SchemaURL() string {
return r.schemaURL
}

func (r *Resource) Entity() Entity {
func (r *Resource) Entity() *Entity {
if r == nil {
return Entity{}
return &Entity{}
}
return r.entity
return &r.entity
}

// Iter returns an iterator of the Resource attributes.
Expand Down Expand Up @@ -290,7 +291,27 @@ func (r *Resource) MarshalJSON() ([]byte, error) {
if r == nil {
r = Empty()
}
return r.attrs.MarshalJSON()

rjson := struct {
Attributes any
SchemaURL string
Entity struct {
Type string
Id any
}
}{
Attributes: r.attrs.MarshalableToJSON(),
SchemaURL: r.schemaURL,
Entity: struct {
Type string
Id any
}{
Type: r.entity.Type(),
Id: r.entity.id.MarshalableToJSON(),
},
}

return json.Marshal(rjson)
}

// Len returns the number of unique key-values in this Resource.
Expand Down

0 comments on commit 77e60bf

Please sign in to comment.