Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/refactor-logprofile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: ottlprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix the handling of references to location.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [41466]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
176 changes: 90 additions & 86 deletions pkg/ottl/contexts/internal/logprofile/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,71 @@ import (
"go.uber.org/zap/zapcore"
)

func getMapping(dict pprofile.ProfilesDictionary, idx int32) (mapping, error) {
mTable := dict.MappingTable()
if idx >= int32(mTable.Len()) {
return mapping{}, fmt.Errorf("mapping index out of bounds: %d", idx)
}
return newMapping(dict, mTable.At(int(idx)))
}

func getLocations(dict pprofile.ProfilesDictionary, locIdxs []int32,
start, length int32,
Comment thread
florianl marked this conversation as resolved.
) (locations, error) {
if start >= int32(len(locIdxs)) {
return locations{}, fmt.Errorf("location start index out of bounds: %d", start)
}
if start+length > int32(len(locIdxs)) {
return locations{}, fmt.Errorf("location end index out of bounds: %d", start+length)
}

locTable := dict.LocationTable()
var joinedErr error
ls := make(locations, 0, length)
for i := range length {
locIdx := locIdxs[start+i]
l, err := newLocation(dict, locTable.At(int(locIdx)))
joinedErr = errors.Join(joinedErr, err)
ls = append(ls, l)
}

return ls, joinedErr
}

func getFunction(dict pprofile.ProfilesDictionary, idx int32) (function, error) {
fnTable := dict.FunctionTable()
if idx >= int32(fnTable.Len()) {
return function{}, fmt.Errorf("function index out of bounds: %d", idx)
}
return newFunction(dict, fnTable.At(int(idx)))
}

func getLink(dict pprofile.ProfilesDictionary, idx int32) (link, error) {
lTable := dict.LinkTable()
if idx >= int32(lTable.Len()) {
return link{}, fmt.Errorf("link index out of bounds: %d", idx)
}
return link{lTable.At(int(idx))}, nil
}

func getString(dict pprofile.ProfilesDictionary, idx int32) (string, error) {
strTable := dict.StringTable()
if idx >= int32(strTable.Len()) {
return "", fmt.Errorf("string index out of bounds: %d", idx)
}
return strTable.At(int(idx)), nil
}

func getAttribute(dict pprofile.ProfilesDictionary, idx int32) (attribute, error) {
attrTable := dict.AttributeTable()
if idx >= int32(attrTable.Len()) {
return attribute{}, fmt.Errorf("attribute index out of bounds: %d", idx)
}
attr := attrTable.At(int(idx))
// Is there a better way to marshal the value?
return attribute{attr.Key(), attr.Value().AsString()}, nil
}

type Profile struct {
pprofile.Profile
Dictionary pprofile.ProfilesDictionary
Expand Down Expand Up @@ -42,7 +107,7 @@ func (p Profile) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
joinedErr = errors.Join(joinedErr, err)
joinedErr = errors.Join(joinedErr, encoder.AddArray("comments", cs))

dst, err := p.getString(p.DefaultSampleTypeIndex())
dst, err := getString(p.Dictionary, p.DefaultSampleTypeIndex())
joinedErr = errors.Join(joinedErr, err)
encoder.AddString("default_sample_type", dst)

Expand All @@ -52,75 +117,13 @@ func (p Profile) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("original_payload_format", p.OriginalPayloadFormat())
encoder.AddByteString("original_payload", p.OriginalPayload().AsRaw())

ats, err := newAttributes(p, p.AttributeIndices())
ats, err := newAttributes(p.Dictionary, p.AttributeIndices())
joinedErr = errors.Join(joinedErr, err)
joinedErr = errors.Join(joinedErr, encoder.AddArray("attributes", ats))

return joinedErr
}

func (p Profile) getString(idx int32) (string, error) {
strTable := p.Dictionary.StringTable()
if idx >= int32(strTable.Len()) {
return "", fmt.Errorf("string index out of bounds: %d", idx)
}
return strTable.At(int(idx)), nil
}

func (p Profile) getFunction(idx int32) (function, error) {
fnTable := p.Dictionary.FunctionTable()
if idx >= int32(fnTable.Len()) {
return function{}, fmt.Errorf("function index out of bounds: %d", idx)
}
return newFunction(p, fnTable.At(int(idx)))
}

func (p Profile) getMapping(idx int32) (mapping, error) {
mTable := p.Dictionary.MappingTable()
if idx >= int32(mTable.Len()) {
return mapping{}, fmt.Errorf("mapping index out of bounds: %d", idx)
}
return newMapping(p, mTable.At(int(idx)))
}

func (p Profile) getLink(idx int32) (link, error) {
lTable := p.Dictionary.LinkTable()
if idx >= int32(lTable.Len()) {
return link{}, fmt.Errorf("link index out of bounds: %d", idx)
}
return link{lTable.At(int(idx))}, nil
}

func (p Profile) getLocations(start, length int32) (locations, error) {
locTable := p.Dictionary.LocationTable()
if start >= int32(locTable.Len()) {
return locations{}, fmt.Errorf("location start index out of bounds: %d", start)
}
if start+length > int32(locTable.Len()) {
return locations{}, fmt.Errorf("location end index out of bounds: %d", start+length)
}

var joinedErr error
ls := make(locations, 0, length)
for i := range length {
l, err := newLocation(p, locTable.At(int(start+i)))
joinedErr = errors.Join(joinedErr, err)
ls = append(ls, l)
}

return ls, joinedErr
}

func (p Profile) getAttribute(idx int32) (attribute, error) {
attrTable := p.Dictionary.AttributeTable()
if idx >= int32(attrTable.Len()) {
return attribute{}, fmt.Errorf("attribute index out of bounds: %d", idx)
}
attr := attrTable.At(int(idx))
// Is there a better way to marshal the value?
return attribute{attr.Key(), attr.Value().AsString()}, nil
}

type samples []sample

func (ss samples) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
Expand Down Expand Up @@ -156,12 +159,13 @@ func newSample(p Profile, ps pprofile.Sample) (sample, error) {

s.timestamps = newTimestamps(ps.TimestampsUnixNano())
s.values = newValues(ps.Value())
s.attributes, err = newAttributes(p, ps.AttributeIndices())
s.attributes, err = newAttributes(p.Dictionary, ps.AttributeIndices())
joinedErr = errors.Join(joinedErr, err)
s.locations, err = p.getLocations(ps.LocationsStartIndex(), ps.LocationsLength())
s.locations, err = getLocations(p.Dictionary, p.LocationIndices().AsRaw(),
ps.LocationsStartIndex(), ps.LocationsLength())
joinedErr = errors.Join(joinedErr, err)
if ps.HasLinkIndex() { // optional
l, err := p.getLink(ps.LinkIndex())
l, err := getLink(p.Dictionary, ps.LinkIndex())
joinedErr = errors.Join(joinedErr, err)
s.link = &l
}
Expand Down Expand Up @@ -210,9 +214,9 @@ func newValueType(p Profile, vt pprofile.ValueType) (valueType, error) {
var err, joinedErr error

result.aggregationTemporality = int32(vt.AggregationTemporality())
result.typ, err = p.getString(vt.TypeStrindex())
result.typ, err = getString(p.Dictionary, vt.TypeStrindex())
joinedErr = errors.Join(joinedErr, err)
result.unit, err = p.getString(vt.UnitStrindex())
result.unit, err = getString(p.Dictionary, vt.UnitStrindex())
joinedErr = errors.Join(joinedErr, err)

return result, joinedErr
Expand Down Expand Up @@ -243,19 +247,19 @@ type location struct {
attributes attributes
}

func newLocation(p Profile, pl pprofile.Location) (location, error) {
func newLocation(dict pprofile.ProfilesDictionary, pl pprofile.Location) (location, error) {
var l location
var err, joinedErr error

if pl.MappingIndex() != 0 { // optional
if l.mapping, err = p.getMapping(pl.MappingIndex()); err != nil {
if l.mapping, err = getMapping(dict, pl.MappingIndex()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
}
if l.attributes, err = newAttributes(p, pl.AttributeIndices()); err != nil {
if l.attributes, err = newAttributes(dict, pl.AttributeIndices()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
if l.lines, err = newLines(p, pl.Line()); err != nil {
if l.lines, err = newLines(dict, pl.Line()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
l.address = pl.Address()
Expand Down Expand Up @@ -283,11 +287,11 @@ type mapping struct {
hasInlineFrames bool
}

func newMapping(p Profile, pm pprofile.Mapping) (mapping, error) {
func newMapping(dict pprofile.ProfilesDictionary, pm pprofile.Mapping) (mapping, error) {
var m mapping
var err error

m.filename, err = p.getString(pm.FilenameStrindex())
m.filename, err = getString(dict, pm.FilenameStrindex())
m.memoryStart = pm.MemoryStart()
m.memoryLimit = pm.MemoryLimit()
m.fileOffset = pm.FileOffset()
Expand Down Expand Up @@ -335,11 +339,11 @@ func (s attributes) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
return joinedErr
}

func newAttributes(p Profile, pattrs pcommon.Int32Slice) (attributes, error) {
func newAttributes(dict pprofile.ProfilesDictionary, pattrs pcommon.Int32Slice) (attributes, error) {
var joinedErr error
as := make(attributes, 0, pattrs.Len())
for i := range pattrs.Len() {
a, err := p.getAttribute(pattrs.At(i))
a, err := getAttribute(dict, pattrs.At(i))
if err != nil {
joinedErr = errors.Join(joinedErr, err)
}
Expand Down Expand Up @@ -371,11 +375,11 @@ func (s lines) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
return joinedErr
}

func newLines(p Profile, plines pprofile.LineSlice) (lines, error) {
func newLines(dict pprofile.ProfilesDictionary, plines pprofile.LineSlice) (lines, error) {
var joinedErr error
ls := make(lines, 0, plines.Len())
for i := range plines.Len() {
l, err := newLine(p, plines.At(i))
l, err := newLine(dict, plines.At(i))
if err != nil {
joinedErr = errors.Join(joinedErr, err)
}
Expand All @@ -390,11 +394,11 @@ type line struct {
column int64
}

func newLine(p Profile, pl pprofile.Line) (line, error) {
func newLine(dict pprofile.ProfilesDictionary, pl pprofile.Line) (line, error) {
var l line
var err, joinedErr error

if l.function, err = p.getFunction(pl.FunctionIndex()); err != nil {
if l.function, err = getFunction(dict, pl.FunctionIndex()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
l.line = pl.Line()
Expand All @@ -416,17 +420,17 @@ type function struct {
startLine int64
}

func newFunction(p Profile, pf pprofile.Function) (function, error) {
func newFunction(dict pprofile.ProfilesDictionary, pf pprofile.Function) (function, error) {
var f function
var err, joinedErr error

if f.name, err = p.getString(pf.NameStrindex()); err != nil {
if f.name, err = getString(dict, pf.NameStrindex()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
if f.systemName, err = p.getString(pf.SystemNameStrindex()); err != nil {
if f.systemName, err = getString(dict, pf.SystemNameStrindex()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
if f.filename, err = p.getString(pf.FilenameStrindex()); err != nil {
if f.filename, err = getString(dict, pf.FilenameStrindex()); err != nil {
joinedErr = errors.Join(joinedErr, err)
}
f.startLine = pf.StartLine()
Expand Down Expand Up @@ -503,7 +507,7 @@ func (p Profile) getComments() (comments, error) {
l := p.CommentStrindices().Len()
cs := make(comments, 0, l)
for i := range l {
c, err := p.getString(p.CommentStrindices().At(i))
c, err := getString(p.Dictionary, p.CommentStrindices().At(i))
if err != nil {
joinedErr = errors.Join(joinedErr, err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/ottl/contexts/internal/logprofile/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestProfile_MarshalLogObject(t *testing.T) {
p := &pprofiletest.Profile{
ProfileID: pprofile.ProfileID([]byte("profileid1111111")),
Attributes: []pprofiletest.Attribute{{Key: "container-attr1", Value: "value1"}},
Comment: []string{"comment1", "comment2"},
}
return dic, p.Transform(dic, pprofile.NewScopeProfiles())
},
Expand Down