Skip to content

Commit

Permalink
Remove "Attribute" part from common pdata collections names (#5001)
Browse files Browse the repository at this point in the history
* Remove "Attribute" part from common pdata collections names

All of the pdata wrappers for collections include Attribute part in its name because the fields used to be part of the attributes fields only. But it's not the case anymore since `pdata.LogRecord.Body` uses AnyValue that can contain any common collection. This change renames AttributeMap and AttributeValueSlice collections by removing Attribute part from their names and making them consistent.

* Change NewMapFromRaw to take interface{}

Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
dmitryax and bogdandrutu authored Mar 21, 2022
1 parent 2b7a90e commit d6b6251
Show file tree
Hide file tree
Showing 23 changed files with 691 additions and 435 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
- `pdata.AttributeValueType` type is deprecated in favor of `pdata.ValueType`
- `pdata.AttributeValueType...` constants are deprecated in favor of `pdata.ValueType...`
- `pdata.NewAttributeValue...` funcs are deprecated in favor of `pdata.NewValue...`
- Remove "Attribute" part from common pdata collections names (#4999)
- Deprecate `pdata.AttributeMap` struct in favor of `pdata.Map`
- Deprecate `pdata.NewAttributeMap` func in favor of `pdata.NewMap`
- Deprecate `pdata.NewAttributeMapFromMap` func in favor of `pdata.NewMapFromRaw`
- Deprecate `pdata.AttributeValueSlice` struct in favor of `pdata.Slice`
- Deprecate `pdata.NewAttributeValueSlice` func in favor of `pdata.NewSlice`
- Deprecate LogRecord.Name(), it was deprecated in the data model (#5054)

### 💡 Enhancements 💡
Expand Down
28 changes: 14 additions & 14 deletions internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (b *dataBuffer) logAttr(label string, value string) {
b.logEntry(" %-15s: %s", label, value)
}

func (b *dataBuffer) logAttributeMap(label string, am pdata.AttributeMap) {
if am.Len() == 0 {
func (b *dataBuffer) logAttributes(label string, m pdata.Map) {
if m.Len() == 0 {
return
}

b.logEntry("%s:", label)
am.Range(func(k string, v pdata.Value) bool {
m.Range(func(k string, v pdata.Value) bool {
b.logEntry(" -> %s: %s(%s)", k, v.Type().String(), attributeValueToString(v))
return true
})
Expand Down Expand Up @@ -200,8 +200,8 @@ func (b *dataBuffer) logDoubleSummaryDataPoints(ps pdata.SummaryDataPointSlice)
}
}

func (b *dataBuffer) logDataPointAttributes(labels pdata.AttributeMap) {
b.logAttributeMap("Data point attributes", labels)
func (b *dataBuffer) logDataPointAttributes(labels pdata.Map) {
b.logAttributes("Data point attributes", labels)
}

func (b *dataBuffer) logEvents(description string, se pdata.SpanEventSlice) {
Expand Down Expand Up @@ -264,34 +264,34 @@ func attributeValueToString(v pdata.Value) string {
case pdata.ValueTypeInt:
return strconv.FormatInt(v.IntVal(), 10)
case pdata.ValueTypeArray:
return attributeValueSliceToString(v.SliceVal())
return sliceToString(v.SliceVal())
case pdata.ValueTypeMap:
return attributeMapToString(v.MapVal())
return mapToString(v.MapVal())
default:
return fmt.Sprintf("<Unknown OpenTelemetry attribute value type %q>", v.Type())
}
}

func attributeValueSliceToString(av pdata.AttributeValueSlice) string {
func sliceToString(s pdata.Slice) string {
var b strings.Builder
b.WriteByte('[')
for i := 0; i < av.Len(); i++ {
if i < av.Len()-1 {
fmt.Fprintf(&b, "%s, ", attributeValueToString(av.At(i)))
for i := 0; i < s.Len(); i++ {
if i < s.Len()-1 {
fmt.Fprintf(&b, "%s, ", attributeValueToString(s.At(i)))
} else {
b.WriteString(attributeValueToString(av.At(i)))
b.WriteString(attributeValueToString(s.At(i)))
}
}

b.WriteByte(']')
return b.String()
}

func attributeMapToString(av pdata.AttributeMap) string {
func mapToString(m pdata.Map) string {
var b strings.Builder
b.WriteString("{\n")

av.Sort().Range(func(k string, v pdata.Value) bool {
m.Sort().Range(func(k string, v pdata.Value) bool {
fmt.Fprintf(&b, " -> %s: %s(%s)\n", k, v.Type(), v.AsString())
return true
})
Expand Down
4 changes: 2 additions & 2 deletions internal/otlptext/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (textLogsMarshaler) MarshalLogs(ld pdata.Logs) ([]byte, error) {
buf.logEntry("ResourceLog #%d", i)
rl := rls.At(i)
buf.logEntry("Resource SchemaURL: %s", rl.SchemaUrl())
buf.logAttributeMap("Resource labels", rl.Resource().Attributes())
buf.logAttributes("Resource labels", rl.Resource().Attributes())
ills := rl.InstrumentationLibraryLogs()
for j := 0; j < ills.Len(); j++ {
buf.logEntry("InstrumentationLibraryLogs #%d", j)
Expand All @@ -49,7 +49,7 @@ func (textLogsMarshaler) MarshalLogs(ld pdata.Logs) ([]byte, error) {
buf.logEntry("Severity: %s", lr.SeverityText())
buf.logEntry("ShortName: %s", lr.Name())
buf.logEntry("Body: %s", attributeValueToString(lr.Body()))
buf.logAttributeMap("Attributes", lr.Attributes())
buf.logAttributes("Attributes", lr.Attributes())
buf.logEntry("Trace ID: %s", lr.TraceID().HexString())
buf.logEntry("Span ID: %s", lr.SpanID().HexString())
buf.logEntry("Flags: %d", lr.Flags())
Expand Down
2 changes: 1 addition & 1 deletion internal/otlptext/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (textMetricsMarshaler) MarshalMetrics(md pdata.Metrics) ([]byte, error) {
buf.logEntry("ResourceMetrics #%d", i)
rm := rms.At(i)
buf.logEntry("Resource SchemaURL: %s", rm.SchemaUrl())
buf.logAttributeMap("Resource labels", rm.Resource().Attributes())
buf.logAttributes("Resource labels", rm.Resource().Attributes())
ilms := rm.InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
buf.logEntry("InstrumentationLibraryMetrics #%d", j)
Expand Down
4 changes: 2 additions & 2 deletions internal/otlptext/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (textTracesMarshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
buf.logEntry("ResourceSpans #%d", i)
rs := rss.At(i)
buf.logEntry("Resource SchemaURL: %s", rs.SchemaUrl())
buf.logAttributeMap("Resource labels", rs.Resource().Attributes())
buf.logAttributes("Resource labels", rs.Resource().Attributes())
ilss := rs.InstrumentationLibrarySpans()
for j := 0; j < ilss.Len(); j++ {
buf.logEntry("InstrumentationLibrarySpans #%d", j)
Expand All @@ -56,7 +56,7 @@ func (textTracesMarshaler) MarshalTraces(td pdata.Traces) ([]byte, error) {
buf.logAttr("Status code", span.Status().Code().String())
buf.logAttr("Status message", span.Status().Message())

buf.logAttributeMap("Attributes", span.Attributes())
buf.logAttributes("Attributes", span.Attributes())
buf.logEvents("Events", span.Events())
buf.logLinks("Links", span.Links())
}
Expand Down
32 changes: 16 additions & 16 deletions internal/testdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
)

var (
resourceAttributes1 = pdata.NewAttributeMapFromMap(map[string]pdata.Value{"resource-attr": pdata.NewValueString("resource-attr-val-1")})
resourceAttributes2 = pdata.NewAttributeMapFromMap(map[string]pdata.Value{"resource-attr": pdata.NewValueString("resource-attr-val-2")})
spanEventAttributes = pdata.NewAttributeMapFromMap(map[string]pdata.Value{"span-event-attr": pdata.NewValueString("span-event-attr-val")})
spanLinkAttributes = pdata.NewAttributeMapFromMap(map[string]pdata.Value{"span-link-attr": pdata.NewValueString("span-link-attr-val")})
spanAttributes = pdata.NewAttributeMapFromMap(map[string]pdata.Value{"span-attr": pdata.NewValueString("span-attr-val")})
metricAttachment = pdata.NewAttributeMapFromMap(map[string]pdata.Value{"exemplar-attachment": pdata.NewValueString("exemplar-attachment-value")})
resourceAttributes1 = pdata.NewMapFromRaw(map[string]interface{}{"resource-attr": "resource-attr-val-1"})
resourceAttributes2 = pdata.NewMapFromRaw(map[string]interface{}{"resource-attr": "resource-attr-val-2"})
spanEventAttributes = pdata.NewMapFromRaw(map[string]interface{}{"span-event-attr": "span-event-attr-val"})
spanLinkAttributes = pdata.NewMapFromRaw(map[string]interface{}{"span-link-attr": "span-link-attr-val"})
spanAttributes = pdata.NewMapFromRaw(map[string]interface{}{"span-attr": "span-attr-val"})
metricAttachment = pdata.NewMapFromRaw(map[string]interface{}{"exemplar-attachment": "exemplar-attachment-value"})
)

const (
Expand All @@ -36,52 +36,52 @@ const (
TestLabelValue3 = "label-value-3"
)

func initResourceAttributes1(dest pdata.AttributeMap) {
func initResourceAttributes1(dest pdata.Map) {
dest.Clear()
resourceAttributes1.CopyTo(dest)
}

func initResourceAttributes2(dest pdata.AttributeMap) {
func initResourceAttributes2(dest pdata.Map) {
dest.Clear()
resourceAttributes2.CopyTo(dest)
}

func initSpanAttributes(dest pdata.AttributeMap) {
func initSpanAttributes(dest pdata.Map) {
dest.Clear()
spanAttributes.CopyTo(dest)
}

func initSpanEventAttributes(dest pdata.AttributeMap) {
func initSpanEventAttributes(dest pdata.Map) {
dest.Clear()
spanEventAttributes.CopyTo(dest)
}

func initSpanLinkAttributes(dest pdata.AttributeMap) {
func initSpanLinkAttributes(dest pdata.Map) {
dest.Clear()
spanLinkAttributes.CopyTo(dest)
}

func initMetricAttachment(dest pdata.AttributeMap) {
func initMetricAttachment(dest pdata.Map) {
dest.Clear()
metricAttachment.CopyTo(dest)
}

func initMetricAttributes1(dest pdata.AttributeMap) {
func initMetricAttributes1(dest pdata.Map) {
dest.Clear()
dest.InsertString(TestLabelKey1, TestLabelValue1)
}

func initMetricAttributes12(dest pdata.AttributeMap) {
func initMetricAttributes12(dest pdata.Map) {
initMetricAttributes1(dest)
dest.InsertString(TestLabelKey2, TestLabelValue2)
}

func initMetricAttributes13(dest pdata.AttributeMap) {
func initMetricAttributes13(dest pdata.Map) {
initMetricAttributes1(dest)
dest.InsertString(TestLabelKey3, TestLabelValue3)
}

func initMetricAttributes2(dest pdata.AttributeMap) {
func initMetricAttributes2(dest pdata.Map) {
dest.Clear()
dest.InsertString(TestLabelKey2, TestLabelValue2)
}
8 changes: 4 additions & 4 deletions model/internal/cmd/pdatagen/internal/common_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ var instrumentationLibrary = &messageValueStruct{

// This will not be generated by this class.
// Defined here just to be available as returned message for the fields.
var attributeMap = &sliceOfPtrs{
structName: "AttributeMap",
var mapStruct = &sliceOfPtrs{
structName: "Map",
element: attributeKeyValue,
}

Expand Down Expand Up @@ -93,7 +93,7 @@ var endTimeField = &primitiveTypedField{
var attributes = &sliceField{
fieldName: "Attributes",
originFieldName: "Attributes",
returnSlice: attributeMap,
returnSlice: mapStruct,
}

var nameField = &primitiveField{
Expand All @@ -110,7 +110,7 @@ var anyValue = &messageValueStruct{
}

var attributeValueArray = &sliceOfValues{
structName: "AttributeValueSlice",
structName: "Slice",
element: anyValue,
}

Expand Down
2 changes: 1 addition & 1 deletion model/internal/cmd/pdatagen/internal/metrics_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ var exemplar = &messageValueStruct{
&sliceField{
fieldName: "FilteredAttributes",
originFieldName: "FilteredAttributes",
returnSlice: attributeMap,
returnSlice: mapStruct,
},
traceIDField,
spanIDField,
Expand Down
Loading

0 comments on commit d6b6251

Please sign in to comment.