diff --git a/.chloggen/profiles-pdata.yaml b/.chloggen/profiles-pdata.yaml new file mode 100644 index 00000000000..bd8bf13065d --- /dev/null +++ b/.chloggen/profiles-pdata.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: pdata + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Introduce generated experimental pdata for profiling signal. + +# One or more tracking issues or pull requests related to the change +issues: [10195] + +# (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: + +# 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: [api] diff --git a/pdata/internal/cmd/pdatagen/internal/packages.go b/pdata/internal/cmd/pdatagen/internal/packages.go index f44b597708a..340912dbc01 100644 --- a/pdata/internal/cmd/pdatagen/internal/packages.go +++ b/pdata/internal/cmd/pdatagen/internal/packages.go @@ -25,6 +25,8 @@ var AllPackages = []*Package{ pmetricotlp, ptrace, ptraceotlp, + pprofile, + pprofileotlp, } // Package is a struct used to generate files. diff --git a/pdata/internal/cmd/pdatagen/internal/pprofile_otlp_package.go b/pdata/internal/cmd/pdatagen/internal/pprofile_otlp_package.go new file mode 100644 index 00000000000..ddd3a5df437 --- /dev/null +++ b/pdata/internal/cmd/pdatagen/internal/pprofile_otlp_package.go @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" +import ( + "path/filepath" +) + +var pprofileotlp = &Package{ + name: "pprofileotlp", + path: filepath.Join("pprofile", "pprofileotlp"), + imports: []string{ + `otlpcollectorprofile "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/profiles/v1experimental"`, + }, + testImports: []string{ + `"testing"`, + ``, + `"github.com/stretchr/testify/assert"`, + }, + structs: []baseStruct{ + exportProfilesPartialSuccess, + }, +} + +var exportProfilesPartialSuccess = &messageValueStruct{ + structName: "ExportPartialSuccess", + description: "// ExportPartialSuccess represents the details of a partially successful export request.", + originFullName: "otlpcollectorprofile.ExportProfilesPartialSuccess", + fields: []baseField{ + &primitiveField{ + fieldName: "RejectedProfiles", + returnType: "int64", + defaultVal: `int64(0)`, + testVal: `int64(13)`, + }, + &primitiveField{ + fieldName: "ErrorMessage", + returnType: "string", + defaultVal: `""`, + testVal: `"error message"`, + }, + }, +} diff --git a/pdata/internal/cmd/pdatagen/internal/pprofile_package.go b/pdata/internal/cmd/pdatagen/internal/pprofile_package.go new file mode 100644 index 00000000000..bd300eae607 --- /dev/null +++ b/pdata/internal/cmd/pdatagen/internal/pprofile_package.go @@ -0,0 +1,603 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package internal // import "go.opentelemetry.io/collector/pdata/internal/cmd/pdatagen/internal" + +var pprofile = &Package{ + name: "pprofile", + path: "pprofile", + imports: []string{ + `"go.opentelemetry.io/collector/pdata/internal"`, + `"go.opentelemetry.io/collector/pdata/internal/data"`, + `otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental"`, + `"go.opentelemetry.io/collector/pdata/pcommon"`, + }, + testImports: []string{ + `"testing"`, + `"unsafe"`, + ``, + `"github.com/stretchr/testify/assert"`, + ``, + `"go.opentelemetry.io/collector/pdata/internal"`, + `otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental"`, + `"go.opentelemetry.io/collector/pdata/pcommon"`, + }, + structs: []baseStruct{ + resourceProfilesSlice, + resourceProfiles, + scopeProfilesSlice, + scopeProfiles, + profilesContainersSlice, + profileContainer, + profile, + valueTypeSlice, + valueType, + sampleSlice, + sample, + labelSlice, + label, + mappingSlice, + mapping, + locationSlice, + location, + lineSlice, + line, + functionSlice, + function, + attributeUnitSlice, + attributeUnit, + linkSlice, + link, + }, +} + +var resourceProfilesSlice = &sliceOfPtrs{ + structName: "ResourceProfilesSlice", + element: resourceProfiles, +} + +var resourceProfiles = &messageValueStruct{ + structName: "ResourceProfiles", + description: "// ResourceProfiles is a collection of profiles from a Resource.", + originFullName: "otlpprofiles.ResourceProfiles", + fields: []baseField{ + resourceField, + schemaURLField, + &sliceField{ + fieldName: "ScopeProfiles", + returnSlice: scopeProfilesSlice, + }, + }, +} + +var scopeProfilesSlice = &sliceOfPtrs{ + structName: "ScopeProfilesSlice", + element: scopeProfiles, +} + +var scopeProfiles = &messageValueStruct{ + structName: "ScopeProfiles", + description: "// ScopeProfiles is a collection of profiles from a LibraryInstrumentation.", + originFullName: "otlpprofiles.ScopeProfiles", + fields: []baseField{ + scopeField, + schemaURLField, + &sliceField{ + fieldName: "Profiles", + returnSlice: profilesContainersSlice, + }, + }, +} + +var profilesContainersSlice = &sliceOfPtrs{ + structName: "ProfilesContainersSlice", + element: profileContainer, +} + +var profileContainer = &messageValueStruct{ + structName: "ProfileContainer", + description: "// ProfileContainer are an experimental implementation of the OpenTelemetry Profiles Data Model.\n", + originFullName: "otlpprofiles.ProfileContainer", + fields: []baseField{ + &sliceField{ + fieldName: "ProfileID", + originFieldName: "ProfileId", + returnSlice: byteSlice, + }, + &primitiveTypedField{ + fieldName: "StartTime", + originFieldName: "StartTimeUnixNano", + returnType: timestampType, + }, + &primitiveTypedField{ + fieldName: "EndTime", + originFieldName: "EndTimeUnixNano", + returnType: timestampType, + }, + attributes, + droppedAttributesCount, + &messageValueField{ + fieldName: "Profile", + returnMessage: profile, + }, + }, +} + +var profile = &messageValueStruct{ + structName: "Profile", + description: "// Profile are an implementation of the pprofextended data model.\n", + originFullName: "otlpprofiles.Profile", + fields: []baseField{ + &sliceField{ + fieldName: "SampleType", + returnSlice: valueTypeSlice, + }, + &sliceField{ + fieldName: "Sample", + returnSlice: sampleSlice, + }, + &sliceField{ + fieldName: "Mapping", + returnSlice: mappingSlice, + }, + &sliceField{ + fieldName: "Location", + returnSlice: locationSlice, + }, + &sliceField{ + fieldName: "LocationIndices", + returnSlice: int64Slice, + }, + &sliceField{ + fieldName: "Function", + returnSlice: functionSlice, + }, + &sliceField{ + fieldName: "AttributeTable", + returnSlice: mapStruct, + }, + &sliceField{ + fieldName: "AttributeUnits", + returnSlice: attributeUnitSlice, + }, + &sliceField{ + fieldName: "LinkTable", + returnSlice: linkSlice, + }, + &sliceField{ + fieldName: "StringTable", + returnSlice: stringSlice, + }, + &primitiveField{ + fieldName: "DropFrames", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "KeepFrames", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveTypedField{ + fieldName: "StartTime", + originFieldName: "TimeNanos", + returnType: &primitiveType{ + structName: "Timestamp", + packageName: "pcommon", + rawType: "int64", + defaultVal: "0", + testVal: "1234567890", + }, + }, + &primitiveTypedField{ + fieldName: "Duration", + originFieldName: "DurationNanos", + returnType: &primitiveType{ + structName: "Timestamp", + packageName: "pcommon", + rawType: "int64", + defaultVal: "0", + testVal: "1234567890", + }, + }, + &messageValueField{ + fieldName: "PeriodType", + returnMessage: valueType, + }, + &primitiveField{ + fieldName: "Period", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &sliceField{ + fieldName: "Comment", + returnSlice: int64Slice, + }, + &primitiveField{ + fieldName: "DefaultSampleType", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + }, +} + +var valueTypeSlice = &sliceOfValues{ + structName: "ValueTypeSlice", + element: valueType, +} + +var valueType = &messageValueStruct{ + structName: "ValueType", + description: "// ValueType describes the type and units of a value, with an optional aggregation temporality.", + originFullName: "otlpprofiles.ValueType", + fields: []baseField{ + &primitiveField{ + fieldName: "Type", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "Unit", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "AggregationTemporality", + returnType: "otlpprofiles.AggregationTemporality", + defaultVal: "otlpprofiles.AggregationTemporality(0)", + testVal: "otlpprofiles.AggregationTemporality(1)", + }, + }, +} + +var sampleSlice = &sliceOfValues{ + structName: "SampleSlice", + element: sample, +} + +var sample = &messageValueStruct{ + structName: "Sample", + description: "// Sample represents each record value encountered within a profiled program.", + originFullName: "otlpprofiles.Sample", + fields: []baseField{ + &sliceField{ + fieldName: "LocationIndex", + returnSlice: uInt64Slice, + }, + &primitiveField{ + fieldName: "LocationsStartIndex", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "LocationsLength", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "StacktraceIdIndex", + returnType: "uint32", + defaultVal: "uint32(0)", + testVal: "uint32(1)", + }, + &sliceField{ + fieldName: "Value", + returnSlice: int64Slice, + }, + &sliceField{ + fieldName: "Label", + returnSlice: labelSlice, + }, + &sliceField{ + fieldName: "Attributes", + returnSlice: uInt64Slice, + }, + &primitiveField{ + fieldName: "Link", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &sliceField{ + fieldName: "TimestampsUnixNano", + returnSlice: uInt64Slice, + }, + }, +} + +var labelSlice = &sliceOfValues{ + structName: "LabelSlice", + element: label, +} + +var label = &messageValueStruct{ + structName: "Label", + description: "// Label provided additional context for a sample", + originFullName: "otlpprofiles.Label", + fields: []baseField{ + &primitiveField{ + fieldName: "Key", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "Str", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "Num", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "NumUnit", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + }, +} + +var mappingSlice = &sliceOfValues{ + structName: "MappingSlice", + element: mapping, +} + +var mapping = &messageValueStruct{ + structName: "Mapping", + description: "// Mapping describes the mapping of a binary in memory, including its address range, file offset, and metadata like build ID", + originFullName: "otlpprofiles.Mapping", + fields: []baseField{ + &primitiveField{ + fieldName: "ID", + originFieldName: "Id", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "MemoryStart", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "MemoryLimit", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "FileOffset", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "Filename", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "BuildID", + originFieldName: "BuildId", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "BuildIDKind", + originFieldName: "BuildIdKind", + returnType: "otlpprofiles.BuildIdKind", + defaultVal: "otlpprofiles.BuildIdKind(0)", + testVal: "otlpprofiles.BuildIdKind(1)", + }, + &sliceField{ + fieldName: "Attributes", + returnSlice: uInt64Slice, + }, + &primitiveField{ + fieldName: "HasFunctions", + returnType: "bool", + defaultVal: "false", + testVal: "true", + }, + &primitiveField{ + fieldName: "HasFilenames", + returnType: "bool", + defaultVal: "false", + testVal: "true", + }, + &primitiveField{ + fieldName: "HasLineNumbers", + returnType: "bool", + defaultVal: "false", + testVal: "true", + }, + &primitiveField{ + fieldName: "HasInlineFrames", + returnType: "bool", + defaultVal: "false", + testVal: "true", + }, + }, +} + +var locationSlice = &sliceOfValues{ + structName: "LocationSlice", + element: location, +} +var location = &messageValueStruct{ + structName: "Location", + description: "// Location describes function and line table debug information.", + originFullName: "otlpprofiles.Location", + fields: []baseField{ + &primitiveField{ + fieldName: "ID", + originFieldName: "Id", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "MappingIndex", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "Address", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &sliceField{ + fieldName: "Line", + returnSlice: lineSlice, + }, + &primitiveField{ + fieldName: "IsFolded", + returnType: "bool", + defaultVal: "false", + testVal: "true", + }, + &primitiveField{ + fieldName: "TypeIndex", + returnType: "uint32", + defaultVal: "uint32(0)", + testVal: "uint32(1)", + }, + &sliceField{ + fieldName: "Attributes", + returnSlice: uInt64Slice, + }, + }, +} + +var lineSlice = &sliceOfValues{ + structName: "LineSlice", + element: line, +} + +var line = &messageValueStruct{ + structName: "Line", + description: "// Line details a specific line in a source code, linked to a function.", + originFullName: "otlpprofiles.Line", + fields: []baseField{ + &primitiveField{ + fieldName: "FunctionIndex", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "Line", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "Column", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + }, +} + +var functionSlice = &sliceOfValues{ + structName: "FunctionSlice", + element: function, +} + +var function = &messageValueStruct{ + structName: "Function", + description: "// Function describes a function, including its human-readable name, system name, source file, and starting line number in the source.", + originFullName: "otlpprofiles.Function", + fields: []baseField{ + &primitiveField{ + fieldName: "ID", + originFieldName: "Id", + returnType: "uint64", + defaultVal: "uint64(0)", + testVal: "uint64(1)", + }, + &primitiveField{ + fieldName: "Name", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "SystemName", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "Filename", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "StartLine", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + }, +} + +var attributeUnitSlice = &sliceOfValues{ + structName: "AttributeUnitSlice", + element: attributeUnit, +} + +var attributeUnit = &messageValueStruct{ + structName: "AttributeUnit", + description: "// AttributeUnit Represents a mapping between Attribute Keys and Units.", + originFullName: "otlpprofiles.AttributeUnit", + fields: []baseField{ + &primitiveField{ + fieldName: "AttributeKey", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + &primitiveField{ + fieldName: "Unit", + returnType: "int64", + defaultVal: "int64(0)", + testVal: "int64(1)", + }, + }, +} + +var linkSlice = &sliceOfValues{ + structName: "LinkSlice", + element: link, +} + +var link = &messageValueStruct{ + structName: "Link", + description: "// Link represents a pointer from a profile Sample to a trace Span.", + originFullName: "otlpprofiles.Link", + fields: []baseField{ + traceIDField, + spanIDField, + }, +} diff --git a/pdata/pprofile/Makefile b/pdata/pprofile/Makefile new file mode 100644 index 00000000000..ded7a36092d --- /dev/null +++ b/pdata/pprofile/Makefile @@ -0,0 +1 @@ +include ../../Makefile.Common diff --git a/pdata/pprofile/generated_attributeunit.go b/pdata/pprofile/generated_attributeunit.go new file mode 100644 index 00000000000..bb6f5e4d0dd --- /dev/null +++ b/pdata/pprofile/generated_attributeunit.go @@ -0,0 +1,75 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// AttributeUnit Represents a mapping between Attribute Keys and Units. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewAttributeUnit function to create new instances. +// Important: zero-initialized instance is not valid for use. +type AttributeUnit struct { + orig *otlpprofiles.AttributeUnit + state *internal.State +} + +func newAttributeUnit(orig *otlpprofiles.AttributeUnit, state *internal.State) AttributeUnit { + return AttributeUnit{orig: orig, state: state} +} + +// NewAttributeUnit creates a new empty AttributeUnit. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewAttributeUnit() AttributeUnit { + state := internal.StateMutable + return newAttributeUnit(&otlpprofiles.AttributeUnit{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms AttributeUnit) MoveTo(dest AttributeUnit) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.AttributeUnit{} +} + +// AttributeKey returns the attributekey associated with this AttributeUnit. +func (ms AttributeUnit) AttributeKey() int64 { + return ms.orig.AttributeKey +} + +// SetAttributeKey replaces the attributekey associated with this AttributeUnit. +func (ms AttributeUnit) SetAttributeKey(v int64) { + ms.state.AssertMutable() + ms.orig.AttributeKey = v +} + +// Unit returns the unit associated with this AttributeUnit. +func (ms AttributeUnit) Unit() int64 { + return ms.orig.Unit +} + +// SetUnit replaces the unit associated with this AttributeUnit. +func (ms AttributeUnit) SetUnit(v int64) { + ms.state.AssertMutable() + ms.orig.Unit = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms AttributeUnit) CopyTo(dest AttributeUnit) { + dest.state.AssertMutable() + dest.SetAttributeKey(ms.AttributeKey()) + dest.SetUnit(ms.Unit()) +} diff --git a/pdata/pprofile/generated_attributeunit_test.go b/pdata/pprofile/generated_attributeunit_test.go new file mode 100644 index 00000000000..e4a54c99b12 --- /dev/null +++ b/pdata/pprofile/generated_attributeunit_test.go @@ -0,0 +1,68 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestAttributeUnit_MoveTo(t *testing.T) { + ms := generateTestAttributeUnit() + dest := NewAttributeUnit() + ms.MoveTo(dest) + assert.Equal(t, NewAttributeUnit(), ms) + assert.Equal(t, generateTestAttributeUnit(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newAttributeUnit(&otlpprofiles.AttributeUnit{}, &sharedState)) }) + assert.Panics(t, func() { newAttributeUnit(&otlpprofiles.AttributeUnit{}, &sharedState).MoveTo(dest) }) +} + +func TestAttributeUnit_CopyTo(t *testing.T) { + ms := NewAttributeUnit() + orig := NewAttributeUnit() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestAttributeUnit() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newAttributeUnit(&otlpprofiles.AttributeUnit{}, &sharedState)) }) +} + +func TestAttributeUnit_AttributeKey(t *testing.T) { + ms := NewAttributeUnit() + assert.Equal(t, int64(0), ms.AttributeKey()) + ms.SetAttributeKey(int64(1)) + assert.Equal(t, int64(1), ms.AttributeKey()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newAttributeUnit(&otlpprofiles.AttributeUnit{}, &sharedState).SetAttributeKey(int64(1)) }) +} + +func TestAttributeUnit_Unit(t *testing.T) { + ms := NewAttributeUnit() + assert.Equal(t, int64(0), ms.Unit()) + ms.SetUnit(int64(1)) + assert.Equal(t, int64(1), ms.Unit()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newAttributeUnit(&otlpprofiles.AttributeUnit{}, &sharedState).SetUnit(int64(1)) }) +} + +func generateTestAttributeUnit() AttributeUnit { + tv := NewAttributeUnit() + fillTestAttributeUnit(tv) + return tv +} + +func fillTestAttributeUnit(tv AttributeUnit) { + tv.orig.AttributeKey = int64(1) + tv.orig.Unit = int64(1) +} diff --git a/pdata/pprofile/generated_attributeunitslice.go b/pdata/pprofile/generated_attributeunitslice.go new file mode 100644 index 00000000000..95274cfe446 --- /dev/null +++ b/pdata/pprofile/generated_attributeunitslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// AttributeUnitSlice logically represents a slice of AttributeUnit. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewAttributeUnitSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type AttributeUnitSlice struct { + orig *[]otlpprofiles.AttributeUnit + state *internal.State +} + +func newAttributeUnitSlice(orig *[]otlpprofiles.AttributeUnit, state *internal.State) AttributeUnitSlice { + return AttributeUnitSlice{orig: orig, state: state} +} + +// NewAttributeUnitSlice creates a AttributeUnitSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewAttributeUnitSlice() AttributeUnitSlice { + orig := []otlpprofiles.AttributeUnit(nil) + state := internal.StateMutable + return newAttributeUnitSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewAttributeUnitSlice()". +func (es AttributeUnitSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es AttributeUnitSlice) At(i int) AttributeUnit { + return newAttributeUnit(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new AttributeUnitSlice can be initialized: +// +// es := NewAttributeUnitSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es AttributeUnitSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.AttributeUnit, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty AttributeUnit. +// It returns the newly added AttributeUnit. +func (es AttributeUnitSlice) AppendEmpty() AttributeUnit { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.AttributeUnit{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es AttributeUnitSlice) MoveAndAppendTo(dest AttributeUnitSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es AttributeUnitSlice) RemoveIf(f func(AttributeUnit) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es AttributeUnitSlice) CopyTo(dest AttributeUnitSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.AttributeUnit, srcLen) + } + for i := range *es.orig { + newAttributeUnit(&(*es.orig)[i], es.state).CopyTo(newAttributeUnit(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_attributeunitslice_test.go b/pdata/pprofile/generated_attributeunitslice_test.go new file mode 100644 index 00000000000..0dd3420015a --- /dev/null +++ b/pdata/pprofile/generated_attributeunitslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestAttributeUnitSlice(t *testing.T) { + es := NewAttributeUnitSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newAttributeUnitSlice(&[]otlpprofiles.AttributeUnit{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewAttributeUnit() + testVal := generateTestAttributeUnit() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestAttributeUnit(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestAttributeUnitSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newAttributeUnitSlice(&[]otlpprofiles.AttributeUnit{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewAttributeUnitSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestAttributeUnitSlice_CopyTo(t *testing.T) { + dest := NewAttributeUnitSlice() + // Test CopyTo to empty + NewAttributeUnitSlice().CopyTo(dest) + assert.Equal(t, NewAttributeUnitSlice(), dest) + + // Test CopyTo larger slice + generateTestAttributeUnitSlice().CopyTo(dest) + assert.Equal(t, generateTestAttributeUnitSlice(), dest) + + // Test CopyTo same size slice + generateTestAttributeUnitSlice().CopyTo(dest) + assert.Equal(t, generateTestAttributeUnitSlice(), dest) +} + +func TestAttributeUnitSlice_EnsureCapacity(t *testing.T) { + es := generateTestAttributeUnitSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestAttributeUnitSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestAttributeUnitSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestAttributeUnitSlice(), es) +} + +func TestAttributeUnitSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestAttributeUnitSlice() + dest := NewAttributeUnitSlice() + src := generateTestAttributeUnitSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestAttributeUnitSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestAttributeUnitSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestAttributeUnitSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestAttributeUnitSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewAttributeUnitSlice() + emptySlice.RemoveIf(func(el AttributeUnit) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestAttributeUnitSlice() + pos := 0 + filtered.RemoveIf(func(el AttributeUnit) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestAttributeUnitSlice() AttributeUnitSlice { + es := NewAttributeUnitSlice() + fillTestAttributeUnitSlice(es) + return es +} + +func fillTestAttributeUnitSlice(es AttributeUnitSlice) { + *es.orig = make([]otlpprofiles.AttributeUnit, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.AttributeUnit{} + fillTestAttributeUnit(newAttributeUnit(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_function.go b/pdata/pprofile/generated_function.go new file mode 100644 index 00000000000..324b8c7b109 --- /dev/null +++ b/pdata/pprofile/generated_function.go @@ -0,0 +1,111 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// Function describes a function, including its human-readable name, system name, source file, and starting line number in the source. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewFunction function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Function struct { + orig *otlpprofiles.Function + state *internal.State +} + +func newFunction(orig *otlpprofiles.Function, state *internal.State) Function { + return Function{orig: orig, state: state} +} + +// NewFunction creates a new empty Function. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewFunction() Function { + state := internal.StateMutable + return newFunction(&otlpprofiles.Function{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Function) MoveTo(dest Function) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Function{} +} + +// ID returns the id associated with this Function. +func (ms Function) ID() uint64 { + return ms.orig.Id +} + +// SetID replaces the id associated with this Function. +func (ms Function) SetID(v uint64) { + ms.state.AssertMutable() + ms.orig.Id = v +} + +// Name returns the name associated with this Function. +func (ms Function) Name() int64 { + return ms.orig.Name +} + +// SetName replaces the name associated with this Function. +func (ms Function) SetName(v int64) { + ms.state.AssertMutable() + ms.orig.Name = v +} + +// SystemName returns the systemname associated with this Function. +func (ms Function) SystemName() int64 { + return ms.orig.SystemName +} + +// SetSystemName replaces the systemname associated with this Function. +func (ms Function) SetSystemName(v int64) { + ms.state.AssertMutable() + ms.orig.SystemName = v +} + +// Filename returns the filename associated with this Function. +func (ms Function) Filename() int64 { + return ms.orig.Filename +} + +// SetFilename replaces the filename associated with this Function. +func (ms Function) SetFilename(v int64) { + ms.state.AssertMutable() + ms.orig.Filename = v +} + +// StartLine returns the startline associated with this Function. +func (ms Function) StartLine() int64 { + return ms.orig.StartLine +} + +// SetStartLine replaces the startline associated with this Function. +func (ms Function) SetStartLine(v int64) { + ms.state.AssertMutable() + ms.orig.StartLine = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Function) CopyTo(dest Function) { + dest.state.AssertMutable() + dest.SetID(ms.ID()) + dest.SetName(ms.Name()) + dest.SetSystemName(ms.SystemName()) + dest.SetFilename(ms.Filename()) + dest.SetStartLine(ms.StartLine()) +} diff --git a/pdata/pprofile/generated_function_test.go b/pdata/pprofile/generated_function_test.go new file mode 100644 index 00000000000..83f0ace5d10 --- /dev/null +++ b/pdata/pprofile/generated_function_test.go @@ -0,0 +1,98 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestFunction_MoveTo(t *testing.T) { + ms := generateTestFunction() + dest := NewFunction() + ms.MoveTo(dest) + assert.Equal(t, NewFunction(), ms) + assert.Equal(t, generateTestFunction(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newFunction(&otlpprofiles.Function{}, &sharedState)) }) + assert.Panics(t, func() { newFunction(&otlpprofiles.Function{}, &sharedState).MoveTo(dest) }) +} + +func TestFunction_CopyTo(t *testing.T) { + ms := NewFunction() + orig := NewFunction() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestFunction() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newFunction(&otlpprofiles.Function{}, &sharedState)) }) +} + +func TestFunction_ID(t *testing.T) { + ms := NewFunction() + assert.Equal(t, uint64(0), ms.ID()) + ms.SetID(uint64(1)) + assert.Equal(t, uint64(1), ms.ID()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newFunction(&otlpprofiles.Function{}, &sharedState).SetID(uint64(1)) }) +} + +func TestFunction_Name(t *testing.T) { + ms := NewFunction() + assert.Equal(t, int64(0), ms.Name()) + ms.SetName(int64(1)) + assert.Equal(t, int64(1), ms.Name()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newFunction(&otlpprofiles.Function{}, &sharedState).SetName(int64(1)) }) +} + +func TestFunction_SystemName(t *testing.T) { + ms := NewFunction() + assert.Equal(t, int64(0), ms.SystemName()) + ms.SetSystemName(int64(1)) + assert.Equal(t, int64(1), ms.SystemName()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newFunction(&otlpprofiles.Function{}, &sharedState).SetSystemName(int64(1)) }) +} + +func TestFunction_Filename(t *testing.T) { + ms := NewFunction() + assert.Equal(t, int64(0), ms.Filename()) + ms.SetFilename(int64(1)) + assert.Equal(t, int64(1), ms.Filename()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newFunction(&otlpprofiles.Function{}, &sharedState).SetFilename(int64(1)) }) +} + +func TestFunction_StartLine(t *testing.T) { + ms := NewFunction() + assert.Equal(t, int64(0), ms.StartLine()) + ms.SetStartLine(int64(1)) + assert.Equal(t, int64(1), ms.StartLine()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newFunction(&otlpprofiles.Function{}, &sharedState).SetStartLine(int64(1)) }) +} + +func generateTestFunction() Function { + tv := NewFunction() + fillTestFunction(tv) + return tv +} + +func fillTestFunction(tv Function) { + tv.orig.Id = uint64(1) + tv.orig.Name = int64(1) + tv.orig.SystemName = int64(1) + tv.orig.Filename = int64(1) + tv.orig.StartLine = int64(1) +} diff --git a/pdata/pprofile/generated_functionslice.go b/pdata/pprofile/generated_functionslice.go new file mode 100644 index 00000000000..171ba345b01 --- /dev/null +++ b/pdata/pprofile/generated_functionslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// FunctionSlice logically represents a slice of Function. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewFunctionSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type FunctionSlice struct { + orig *[]otlpprofiles.Function + state *internal.State +} + +func newFunctionSlice(orig *[]otlpprofiles.Function, state *internal.State) FunctionSlice { + return FunctionSlice{orig: orig, state: state} +} + +// NewFunctionSlice creates a FunctionSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewFunctionSlice() FunctionSlice { + orig := []otlpprofiles.Function(nil) + state := internal.StateMutable + return newFunctionSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewFunctionSlice()". +func (es FunctionSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es FunctionSlice) At(i int) Function { + return newFunction(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new FunctionSlice can be initialized: +// +// es := NewFunctionSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es FunctionSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Function, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Function. +// It returns the newly added Function. +func (es FunctionSlice) AppendEmpty() Function { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Function{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es FunctionSlice) MoveAndAppendTo(dest FunctionSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es FunctionSlice) RemoveIf(f func(Function) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es FunctionSlice) CopyTo(dest FunctionSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Function, srcLen) + } + for i := range *es.orig { + newFunction(&(*es.orig)[i], es.state).CopyTo(newFunction(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_functionslice_test.go b/pdata/pprofile/generated_functionslice_test.go new file mode 100644 index 00000000000..c4311bf52d9 --- /dev/null +++ b/pdata/pprofile/generated_functionslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestFunctionSlice(t *testing.T) { + es := NewFunctionSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newFunctionSlice(&[]otlpprofiles.Function{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewFunction() + testVal := generateTestFunction() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestFunction(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestFunctionSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newFunctionSlice(&[]otlpprofiles.Function{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewFunctionSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestFunctionSlice_CopyTo(t *testing.T) { + dest := NewFunctionSlice() + // Test CopyTo to empty + NewFunctionSlice().CopyTo(dest) + assert.Equal(t, NewFunctionSlice(), dest) + + // Test CopyTo larger slice + generateTestFunctionSlice().CopyTo(dest) + assert.Equal(t, generateTestFunctionSlice(), dest) + + // Test CopyTo same size slice + generateTestFunctionSlice().CopyTo(dest) + assert.Equal(t, generateTestFunctionSlice(), dest) +} + +func TestFunctionSlice_EnsureCapacity(t *testing.T) { + es := generateTestFunctionSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestFunctionSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestFunctionSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestFunctionSlice(), es) +} + +func TestFunctionSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestFunctionSlice() + dest := NewFunctionSlice() + src := generateTestFunctionSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestFunctionSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestFunctionSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestFunctionSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestFunctionSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewFunctionSlice() + emptySlice.RemoveIf(func(el Function) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestFunctionSlice() + pos := 0 + filtered.RemoveIf(func(el Function) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestFunctionSlice() FunctionSlice { + es := NewFunctionSlice() + fillTestFunctionSlice(es) + return es +} + +func fillTestFunctionSlice(es FunctionSlice) { + *es.orig = make([]otlpprofiles.Function, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Function{} + fillTestFunction(newFunction(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_label.go b/pdata/pprofile/generated_label.go new file mode 100644 index 00000000000..fc93d13b390 --- /dev/null +++ b/pdata/pprofile/generated_label.go @@ -0,0 +1,99 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// Label provided additional context for a sample +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewLabel function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Label struct { + orig *otlpprofiles.Label + state *internal.State +} + +func newLabel(orig *otlpprofiles.Label, state *internal.State) Label { + return Label{orig: orig, state: state} +} + +// NewLabel creates a new empty Label. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewLabel() Label { + state := internal.StateMutable + return newLabel(&otlpprofiles.Label{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Label) MoveTo(dest Label) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Label{} +} + +// Key returns the key associated with this Label. +func (ms Label) Key() int64 { + return ms.orig.Key +} + +// SetKey replaces the key associated with this Label. +func (ms Label) SetKey(v int64) { + ms.state.AssertMutable() + ms.orig.Key = v +} + +// Str returns the str associated with this Label. +func (ms Label) Str() int64 { + return ms.orig.Str +} + +// SetStr replaces the str associated with this Label. +func (ms Label) SetStr(v int64) { + ms.state.AssertMutable() + ms.orig.Str = v +} + +// Num returns the num associated with this Label. +func (ms Label) Num() int64 { + return ms.orig.Num +} + +// SetNum replaces the num associated with this Label. +func (ms Label) SetNum(v int64) { + ms.state.AssertMutable() + ms.orig.Num = v +} + +// NumUnit returns the numunit associated with this Label. +func (ms Label) NumUnit() int64 { + return ms.orig.NumUnit +} + +// SetNumUnit replaces the numunit associated with this Label. +func (ms Label) SetNumUnit(v int64) { + ms.state.AssertMutable() + ms.orig.NumUnit = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Label) CopyTo(dest Label) { + dest.state.AssertMutable() + dest.SetKey(ms.Key()) + dest.SetStr(ms.Str()) + dest.SetNum(ms.Num()) + dest.SetNumUnit(ms.NumUnit()) +} diff --git a/pdata/pprofile/generated_label_test.go b/pdata/pprofile/generated_label_test.go new file mode 100644 index 00000000000..c4ed6722376 --- /dev/null +++ b/pdata/pprofile/generated_label_test.go @@ -0,0 +1,88 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestLabel_MoveTo(t *testing.T) { + ms := generateTestLabel() + dest := NewLabel() + ms.MoveTo(dest) + assert.Equal(t, NewLabel(), ms) + assert.Equal(t, generateTestLabel(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newLabel(&otlpprofiles.Label{}, &sharedState)) }) + assert.Panics(t, func() { newLabel(&otlpprofiles.Label{}, &sharedState).MoveTo(dest) }) +} + +func TestLabel_CopyTo(t *testing.T) { + ms := NewLabel() + orig := NewLabel() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestLabel() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newLabel(&otlpprofiles.Label{}, &sharedState)) }) +} + +func TestLabel_Key(t *testing.T) { + ms := NewLabel() + assert.Equal(t, int64(0), ms.Key()) + ms.SetKey(int64(1)) + assert.Equal(t, int64(1), ms.Key()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLabel(&otlpprofiles.Label{}, &sharedState).SetKey(int64(1)) }) +} + +func TestLabel_Str(t *testing.T) { + ms := NewLabel() + assert.Equal(t, int64(0), ms.Str()) + ms.SetStr(int64(1)) + assert.Equal(t, int64(1), ms.Str()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLabel(&otlpprofiles.Label{}, &sharedState).SetStr(int64(1)) }) +} + +func TestLabel_Num(t *testing.T) { + ms := NewLabel() + assert.Equal(t, int64(0), ms.Num()) + ms.SetNum(int64(1)) + assert.Equal(t, int64(1), ms.Num()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLabel(&otlpprofiles.Label{}, &sharedState).SetNum(int64(1)) }) +} + +func TestLabel_NumUnit(t *testing.T) { + ms := NewLabel() + assert.Equal(t, int64(0), ms.NumUnit()) + ms.SetNumUnit(int64(1)) + assert.Equal(t, int64(1), ms.NumUnit()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLabel(&otlpprofiles.Label{}, &sharedState).SetNumUnit(int64(1)) }) +} + +func generateTestLabel() Label { + tv := NewLabel() + fillTestLabel(tv) + return tv +} + +func fillTestLabel(tv Label) { + tv.orig.Key = int64(1) + tv.orig.Str = int64(1) + tv.orig.Num = int64(1) + tv.orig.NumUnit = int64(1) +} diff --git a/pdata/pprofile/generated_labelslice.go b/pdata/pprofile/generated_labelslice.go new file mode 100644 index 00000000000..5f865bfcbae --- /dev/null +++ b/pdata/pprofile/generated_labelslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// LabelSlice logically represents a slice of Label. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewLabelSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type LabelSlice struct { + orig *[]otlpprofiles.Label + state *internal.State +} + +func newLabelSlice(orig *[]otlpprofiles.Label, state *internal.State) LabelSlice { + return LabelSlice{orig: orig, state: state} +} + +// NewLabelSlice creates a LabelSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewLabelSlice() LabelSlice { + orig := []otlpprofiles.Label(nil) + state := internal.StateMutable + return newLabelSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewLabelSlice()". +func (es LabelSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es LabelSlice) At(i int) Label { + return newLabel(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new LabelSlice can be initialized: +// +// es := NewLabelSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es LabelSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Label, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Label. +// It returns the newly added Label. +func (es LabelSlice) AppendEmpty() Label { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Label{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es LabelSlice) MoveAndAppendTo(dest LabelSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es LabelSlice) RemoveIf(f func(Label) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es LabelSlice) CopyTo(dest LabelSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Label, srcLen) + } + for i := range *es.orig { + newLabel(&(*es.orig)[i], es.state).CopyTo(newLabel(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_labelslice_test.go b/pdata/pprofile/generated_labelslice_test.go new file mode 100644 index 00000000000..c399046f92e --- /dev/null +++ b/pdata/pprofile/generated_labelslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestLabelSlice(t *testing.T) { + es := NewLabelSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newLabelSlice(&[]otlpprofiles.Label{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewLabel() + testVal := generateTestLabel() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestLabel(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestLabelSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newLabelSlice(&[]otlpprofiles.Label{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewLabelSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestLabelSlice_CopyTo(t *testing.T) { + dest := NewLabelSlice() + // Test CopyTo to empty + NewLabelSlice().CopyTo(dest) + assert.Equal(t, NewLabelSlice(), dest) + + // Test CopyTo larger slice + generateTestLabelSlice().CopyTo(dest) + assert.Equal(t, generateTestLabelSlice(), dest) + + // Test CopyTo same size slice + generateTestLabelSlice().CopyTo(dest) + assert.Equal(t, generateTestLabelSlice(), dest) +} + +func TestLabelSlice_EnsureCapacity(t *testing.T) { + es := generateTestLabelSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestLabelSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestLabelSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestLabelSlice(), es) +} + +func TestLabelSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestLabelSlice() + dest := NewLabelSlice() + src := generateTestLabelSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLabelSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLabelSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestLabelSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestLabelSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewLabelSlice() + emptySlice.RemoveIf(func(el Label) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestLabelSlice() + pos := 0 + filtered.RemoveIf(func(el Label) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestLabelSlice() LabelSlice { + es := NewLabelSlice() + fillTestLabelSlice(es) + return es +} + +func fillTestLabelSlice(es LabelSlice) { + *es.orig = make([]otlpprofiles.Label, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Label{} + fillTestLabel(newLabel(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_line.go b/pdata/pprofile/generated_line.go new file mode 100644 index 00000000000..80147d393a5 --- /dev/null +++ b/pdata/pprofile/generated_line.go @@ -0,0 +1,87 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// Line details a specific line in a source code, linked to a function. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewLine function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Line struct { + orig *otlpprofiles.Line + state *internal.State +} + +func newLine(orig *otlpprofiles.Line, state *internal.State) Line { + return Line{orig: orig, state: state} +} + +// NewLine creates a new empty Line. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewLine() Line { + state := internal.StateMutable + return newLine(&otlpprofiles.Line{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Line) MoveTo(dest Line) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Line{} +} + +// FunctionIndex returns the functionindex associated with this Line. +func (ms Line) FunctionIndex() uint64 { + return ms.orig.FunctionIndex +} + +// SetFunctionIndex replaces the functionindex associated with this Line. +func (ms Line) SetFunctionIndex(v uint64) { + ms.state.AssertMutable() + ms.orig.FunctionIndex = v +} + +// Line returns the line associated with this Line. +func (ms Line) Line() int64 { + return ms.orig.Line +} + +// SetLine replaces the line associated with this Line. +func (ms Line) SetLine(v int64) { + ms.state.AssertMutable() + ms.orig.Line = v +} + +// Column returns the column associated with this Line. +func (ms Line) Column() int64 { + return ms.orig.Column +} + +// SetColumn replaces the column associated with this Line. +func (ms Line) SetColumn(v int64) { + ms.state.AssertMutable() + ms.orig.Column = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Line) CopyTo(dest Line) { + dest.state.AssertMutable() + dest.SetFunctionIndex(ms.FunctionIndex()) + dest.SetLine(ms.Line()) + dest.SetColumn(ms.Column()) +} diff --git a/pdata/pprofile/generated_line_test.go b/pdata/pprofile/generated_line_test.go new file mode 100644 index 00000000000..aef0f583145 --- /dev/null +++ b/pdata/pprofile/generated_line_test.go @@ -0,0 +1,78 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestLine_MoveTo(t *testing.T) { + ms := generateTestLine() + dest := NewLine() + ms.MoveTo(dest) + assert.Equal(t, NewLine(), ms) + assert.Equal(t, generateTestLine(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newLine(&otlpprofiles.Line{}, &sharedState)) }) + assert.Panics(t, func() { newLine(&otlpprofiles.Line{}, &sharedState).MoveTo(dest) }) +} + +func TestLine_CopyTo(t *testing.T) { + ms := NewLine() + orig := NewLine() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestLine() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newLine(&otlpprofiles.Line{}, &sharedState)) }) +} + +func TestLine_FunctionIndex(t *testing.T) { + ms := NewLine() + assert.Equal(t, uint64(0), ms.FunctionIndex()) + ms.SetFunctionIndex(uint64(1)) + assert.Equal(t, uint64(1), ms.FunctionIndex()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLine(&otlpprofiles.Line{}, &sharedState).SetFunctionIndex(uint64(1)) }) +} + +func TestLine_Line(t *testing.T) { + ms := NewLine() + assert.Equal(t, int64(0), ms.Line()) + ms.SetLine(int64(1)) + assert.Equal(t, int64(1), ms.Line()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLine(&otlpprofiles.Line{}, &sharedState).SetLine(int64(1)) }) +} + +func TestLine_Column(t *testing.T) { + ms := NewLine() + assert.Equal(t, int64(0), ms.Column()) + ms.SetColumn(int64(1)) + assert.Equal(t, int64(1), ms.Column()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLine(&otlpprofiles.Line{}, &sharedState).SetColumn(int64(1)) }) +} + +func generateTestLine() Line { + tv := NewLine() + fillTestLine(tv) + return tv +} + +func fillTestLine(tv Line) { + tv.orig.FunctionIndex = uint64(1) + tv.orig.Line = int64(1) + tv.orig.Column = int64(1) +} diff --git a/pdata/pprofile/generated_lineslice.go b/pdata/pprofile/generated_lineslice.go new file mode 100644 index 00000000000..b354560d090 --- /dev/null +++ b/pdata/pprofile/generated_lineslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// LineSlice logically represents a slice of Line. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewLineSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type LineSlice struct { + orig *[]otlpprofiles.Line + state *internal.State +} + +func newLineSlice(orig *[]otlpprofiles.Line, state *internal.State) LineSlice { + return LineSlice{orig: orig, state: state} +} + +// NewLineSlice creates a LineSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewLineSlice() LineSlice { + orig := []otlpprofiles.Line(nil) + state := internal.StateMutable + return newLineSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewLineSlice()". +func (es LineSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es LineSlice) At(i int) Line { + return newLine(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new LineSlice can be initialized: +// +// es := NewLineSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es LineSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Line, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Line. +// It returns the newly added Line. +func (es LineSlice) AppendEmpty() Line { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Line{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es LineSlice) MoveAndAppendTo(dest LineSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es LineSlice) RemoveIf(f func(Line) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es LineSlice) CopyTo(dest LineSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Line, srcLen) + } + for i := range *es.orig { + newLine(&(*es.orig)[i], es.state).CopyTo(newLine(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_lineslice_test.go b/pdata/pprofile/generated_lineslice_test.go new file mode 100644 index 00000000000..1f5a69ca3f4 --- /dev/null +++ b/pdata/pprofile/generated_lineslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestLineSlice(t *testing.T) { + es := NewLineSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newLineSlice(&[]otlpprofiles.Line{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewLine() + testVal := generateTestLine() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestLine(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestLineSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newLineSlice(&[]otlpprofiles.Line{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewLineSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestLineSlice_CopyTo(t *testing.T) { + dest := NewLineSlice() + // Test CopyTo to empty + NewLineSlice().CopyTo(dest) + assert.Equal(t, NewLineSlice(), dest) + + // Test CopyTo larger slice + generateTestLineSlice().CopyTo(dest) + assert.Equal(t, generateTestLineSlice(), dest) + + // Test CopyTo same size slice + generateTestLineSlice().CopyTo(dest) + assert.Equal(t, generateTestLineSlice(), dest) +} + +func TestLineSlice_EnsureCapacity(t *testing.T) { + es := generateTestLineSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestLineSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestLineSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestLineSlice(), es) +} + +func TestLineSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestLineSlice() + dest := NewLineSlice() + src := generateTestLineSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLineSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLineSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestLineSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestLineSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewLineSlice() + emptySlice.RemoveIf(func(el Line) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestLineSlice() + pos := 0 + filtered.RemoveIf(func(el Line) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestLineSlice() LineSlice { + es := NewLineSlice() + fillTestLineSlice(es) + return es +} + +func fillTestLineSlice(es LineSlice) { + *es.orig = make([]otlpprofiles.Line, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Line{} + fillTestLine(newLine(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_link.go b/pdata/pprofile/generated_link.go new file mode 100644 index 00000000000..70af00112b0 --- /dev/null +++ b/pdata/pprofile/generated_link.go @@ -0,0 +1,77 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + "go.opentelemetry.io/collector/pdata/internal/data" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// Link represents a pointer from a profile Sample to a trace Span. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewLink function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Link struct { + orig *otlpprofiles.Link + state *internal.State +} + +func newLink(orig *otlpprofiles.Link, state *internal.State) Link { + return Link{orig: orig, state: state} +} + +// NewLink creates a new empty Link. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewLink() Link { + state := internal.StateMutable + return newLink(&otlpprofiles.Link{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Link) MoveTo(dest Link) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Link{} +} + +// TraceID returns the traceid associated with this Link. +func (ms Link) TraceID() pcommon.TraceID { + return pcommon.TraceID(ms.orig.TraceId) +} + +// SetTraceID replaces the traceid associated with this Link. +func (ms Link) SetTraceID(v pcommon.TraceID) { + ms.state.AssertMutable() + ms.orig.TraceId = data.TraceID(v) +} + +// SpanID returns the spanid associated with this Link. +func (ms Link) SpanID() pcommon.SpanID { + return pcommon.SpanID(ms.orig.SpanId) +} + +// SetSpanID replaces the spanid associated with this Link. +func (ms Link) SetSpanID(v pcommon.SpanID) { + ms.state.AssertMutable() + ms.orig.SpanId = data.SpanID(v) +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Link) CopyTo(dest Link) { + dest.state.AssertMutable() + dest.SetTraceID(ms.TraceID()) + dest.SetSpanID(ms.SpanID()) +} diff --git a/pdata/pprofile/generated_link_test.go b/pdata/pprofile/generated_link_test.go new file mode 100644 index 00000000000..8e10072d2dd --- /dev/null +++ b/pdata/pprofile/generated_link_test.go @@ -0,0 +1,68 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + "go.opentelemetry.io/collector/pdata/internal/data" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestLink_MoveTo(t *testing.T) { + ms := generateTestLink() + dest := NewLink() + ms.MoveTo(dest) + assert.Equal(t, NewLink(), ms) + assert.Equal(t, generateTestLink(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newLink(&otlpprofiles.Link{}, &sharedState)) }) + assert.Panics(t, func() { newLink(&otlpprofiles.Link{}, &sharedState).MoveTo(dest) }) +} + +func TestLink_CopyTo(t *testing.T) { + ms := NewLink() + orig := NewLink() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestLink() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newLink(&otlpprofiles.Link{}, &sharedState)) }) +} + +func TestLink_TraceID(t *testing.T) { + ms := NewLink() + assert.Equal(t, pcommon.TraceID(data.TraceID([16]byte{})), ms.TraceID()) + testValTraceID := pcommon.TraceID(data.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})) + ms.SetTraceID(testValTraceID) + assert.Equal(t, testValTraceID, ms.TraceID()) +} + +func TestLink_SpanID(t *testing.T) { + ms := NewLink() + assert.Equal(t, pcommon.SpanID(data.SpanID([8]byte{})), ms.SpanID()) + testValSpanID := pcommon.SpanID(data.SpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1})) + ms.SetSpanID(testValSpanID) + assert.Equal(t, testValSpanID, ms.SpanID()) +} + +func generateTestLink() Link { + tv := NewLink() + fillTestLink(tv) + return tv +} + +func fillTestLink(tv Link) { + tv.orig.TraceId = data.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) + tv.orig.SpanId = data.SpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1}) +} diff --git a/pdata/pprofile/generated_linkslice.go b/pdata/pprofile/generated_linkslice.go new file mode 100644 index 00000000000..3ae925ac09c --- /dev/null +++ b/pdata/pprofile/generated_linkslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// LinkSlice logically represents a slice of Link. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewLinkSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type LinkSlice struct { + orig *[]otlpprofiles.Link + state *internal.State +} + +func newLinkSlice(orig *[]otlpprofiles.Link, state *internal.State) LinkSlice { + return LinkSlice{orig: orig, state: state} +} + +// NewLinkSlice creates a LinkSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewLinkSlice() LinkSlice { + orig := []otlpprofiles.Link(nil) + state := internal.StateMutable + return newLinkSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewLinkSlice()". +func (es LinkSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es LinkSlice) At(i int) Link { + return newLink(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new LinkSlice can be initialized: +// +// es := NewLinkSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es LinkSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Link, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Link. +// It returns the newly added Link. +func (es LinkSlice) AppendEmpty() Link { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Link{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es LinkSlice) MoveAndAppendTo(dest LinkSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es LinkSlice) RemoveIf(f func(Link) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es LinkSlice) CopyTo(dest LinkSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Link, srcLen) + } + for i := range *es.orig { + newLink(&(*es.orig)[i], es.state).CopyTo(newLink(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_linkslice_test.go b/pdata/pprofile/generated_linkslice_test.go new file mode 100644 index 00000000000..5d22fcd69ca --- /dev/null +++ b/pdata/pprofile/generated_linkslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestLinkSlice(t *testing.T) { + es := NewLinkSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newLinkSlice(&[]otlpprofiles.Link{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewLink() + testVal := generateTestLink() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestLink(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestLinkSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newLinkSlice(&[]otlpprofiles.Link{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewLinkSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestLinkSlice_CopyTo(t *testing.T) { + dest := NewLinkSlice() + // Test CopyTo to empty + NewLinkSlice().CopyTo(dest) + assert.Equal(t, NewLinkSlice(), dest) + + // Test CopyTo larger slice + generateTestLinkSlice().CopyTo(dest) + assert.Equal(t, generateTestLinkSlice(), dest) + + // Test CopyTo same size slice + generateTestLinkSlice().CopyTo(dest) + assert.Equal(t, generateTestLinkSlice(), dest) +} + +func TestLinkSlice_EnsureCapacity(t *testing.T) { + es := generateTestLinkSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestLinkSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestLinkSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestLinkSlice(), es) +} + +func TestLinkSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestLinkSlice() + dest := NewLinkSlice() + src := generateTestLinkSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLinkSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLinkSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestLinkSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestLinkSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewLinkSlice() + emptySlice.RemoveIf(func(el Link) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestLinkSlice() + pos := 0 + filtered.RemoveIf(func(el Link) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestLinkSlice() LinkSlice { + es := NewLinkSlice() + fillTestLinkSlice(es) + return es +} + +func fillTestLinkSlice(es LinkSlice) { + *es.orig = make([]otlpprofiles.Link, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Link{} + fillTestLink(newLink(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_location.go b/pdata/pprofile/generated_location.go new file mode 100644 index 00000000000..5ed030f6d6d --- /dev/null +++ b/pdata/pprofile/generated_location.go @@ -0,0 +1,124 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// Location describes function and line table debug information. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewLocation function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Location struct { + orig *otlpprofiles.Location + state *internal.State +} + +func newLocation(orig *otlpprofiles.Location, state *internal.State) Location { + return Location{orig: orig, state: state} +} + +// NewLocation creates a new empty Location. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewLocation() Location { + state := internal.StateMutable + return newLocation(&otlpprofiles.Location{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Location) MoveTo(dest Location) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Location{} +} + +// ID returns the id associated with this Location. +func (ms Location) ID() uint64 { + return ms.orig.Id +} + +// SetID replaces the id associated with this Location. +func (ms Location) SetID(v uint64) { + ms.state.AssertMutable() + ms.orig.Id = v +} + +// MappingIndex returns the mappingindex associated with this Location. +func (ms Location) MappingIndex() uint64 { + return ms.orig.MappingIndex +} + +// SetMappingIndex replaces the mappingindex associated with this Location. +func (ms Location) SetMappingIndex(v uint64) { + ms.state.AssertMutable() + ms.orig.MappingIndex = v +} + +// Address returns the address associated with this Location. +func (ms Location) Address() uint64 { + return ms.orig.Address +} + +// SetAddress replaces the address associated with this Location. +func (ms Location) SetAddress(v uint64) { + ms.state.AssertMutable() + ms.orig.Address = v +} + +// Line returns the Line associated with this Location. +func (ms Location) Line() LineSlice { + return newLineSlice(&ms.orig.Line, ms.state) +} + +// IsFolded returns the isfolded associated with this Location. +func (ms Location) IsFolded() bool { + return ms.orig.IsFolded +} + +// SetIsFolded replaces the isfolded associated with this Location. +func (ms Location) SetIsFolded(v bool) { + ms.state.AssertMutable() + ms.orig.IsFolded = v +} + +// TypeIndex returns the typeindex associated with this Location. +func (ms Location) TypeIndex() uint32 { + return ms.orig.TypeIndex +} + +// SetTypeIndex replaces the typeindex associated with this Location. +func (ms Location) SetTypeIndex(v uint32) { + ms.state.AssertMutable() + ms.orig.TypeIndex = v +} + +// Attributes returns the Attributes associated with this Location. +func (ms Location) Attributes() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.orig.Attributes, ms.state)) +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Location) CopyTo(dest Location) { + dest.state.AssertMutable() + dest.SetID(ms.ID()) + dest.SetMappingIndex(ms.MappingIndex()) + dest.SetAddress(ms.Address()) + ms.Line().CopyTo(dest.Line()) + dest.SetIsFolded(ms.IsFolded()) + dest.SetTypeIndex(ms.TypeIndex()) + ms.Attributes().CopyTo(dest.Attributes()) +} diff --git a/pdata/pprofile/generated_location_test.go b/pdata/pprofile/generated_location_test.go new file mode 100644 index 00000000000..60f6d553bb6 --- /dev/null +++ b/pdata/pprofile/generated_location_test.go @@ -0,0 +1,115 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestLocation_MoveTo(t *testing.T) { + ms := generateTestLocation() + dest := NewLocation() + ms.MoveTo(dest) + assert.Equal(t, NewLocation(), ms) + assert.Equal(t, generateTestLocation(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newLocation(&otlpprofiles.Location{}, &sharedState)) }) + assert.Panics(t, func() { newLocation(&otlpprofiles.Location{}, &sharedState).MoveTo(dest) }) +} + +func TestLocation_CopyTo(t *testing.T) { + ms := NewLocation() + orig := NewLocation() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestLocation() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newLocation(&otlpprofiles.Location{}, &sharedState)) }) +} + +func TestLocation_ID(t *testing.T) { + ms := NewLocation() + assert.Equal(t, uint64(0), ms.ID()) + ms.SetID(uint64(1)) + assert.Equal(t, uint64(1), ms.ID()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLocation(&otlpprofiles.Location{}, &sharedState).SetID(uint64(1)) }) +} + +func TestLocation_MappingIndex(t *testing.T) { + ms := NewLocation() + assert.Equal(t, uint64(0), ms.MappingIndex()) + ms.SetMappingIndex(uint64(1)) + assert.Equal(t, uint64(1), ms.MappingIndex()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLocation(&otlpprofiles.Location{}, &sharedState).SetMappingIndex(uint64(1)) }) +} + +func TestLocation_Address(t *testing.T) { + ms := NewLocation() + assert.Equal(t, uint64(0), ms.Address()) + ms.SetAddress(uint64(1)) + assert.Equal(t, uint64(1), ms.Address()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLocation(&otlpprofiles.Location{}, &sharedState).SetAddress(uint64(1)) }) +} + +func TestLocation_Line(t *testing.T) { + ms := NewLocation() + assert.Equal(t, NewLineSlice(), ms.Line()) + fillTestLineSlice(ms.Line()) + assert.Equal(t, generateTestLineSlice(), ms.Line()) +} + +func TestLocation_IsFolded(t *testing.T) { + ms := NewLocation() + assert.Equal(t, false, ms.IsFolded()) + ms.SetIsFolded(true) + assert.Equal(t, true, ms.IsFolded()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLocation(&otlpprofiles.Location{}, &sharedState).SetIsFolded(true) }) +} + +func TestLocation_TypeIndex(t *testing.T) { + ms := NewLocation() + assert.Equal(t, uint32(0), ms.TypeIndex()) + ms.SetTypeIndex(uint32(1)) + assert.Equal(t, uint32(1), ms.TypeIndex()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newLocation(&otlpprofiles.Location{}, &sharedState).SetTypeIndex(uint32(1)) }) +} + +func TestLocation_Attributes(t *testing.T) { + ms := NewLocation() + assert.Equal(t, pcommon.NewUInt64Slice(), ms.Attributes()) + internal.FillTestUInt64Slice(internal.UInt64Slice(ms.Attributes())) + assert.Equal(t, pcommon.UInt64Slice(internal.GenerateTestUInt64Slice()), ms.Attributes()) +} + +func generateTestLocation() Location { + tv := NewLocation() + fillTestLocation(tv) + return tv +} + +func fillTestLocation(tv Location) { + tv.orig.Id = uint64(1) + tv.orig.MappingIndex = uint64(1) + tv.orig.Address = uint64(1) + fillTestLineSlice(newLineSlice(&tv.orig.Line, tv.state)) + tv.orig.IsFolded = true + tv.orig.TypeIndex = uint32(1) + internal.FillTestUInt64Slice(internal.NewUInt64Slice(&tv.orig.Attributes, tv.state)) +} diff --git a/pdata/pprofile/generated_locationslice.go b/pdata/pprofile/generated_locationslice.go new file mode 100644 index 00000000000..e5751c4dcb6 --- /dev/null +++ b/pdata/pprofile/generated_locationslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// LocationSlice logically represents a slice of Location. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewLocationSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type LocationSlice struct { + orig *[]otlpprofiles.Location + state *internal.State +} + +func newLocationSlice(orig *[]otlpprofiles.Location, state *internal.State) LocationSlice { + return LocationSlice{orig: orig, state: state} +} + +// NewLocationSlice creates a LocationSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewLocationSlice() LocationSlice { + orig := []otlpprofiles.Location(nil) + state := internal.StateMutable + return newLocationSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewLocationSlice()". +func (es LocationSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es LocationSlice) At(i int) Location { + return newLocation(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new LocationSlice can be initialized: +// +// es := NewLocationSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es LocationSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Location, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Location. +// It returns the newly added Location. +func (es LocationSlice) AppendEmpty() Location { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Location{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es LocationSlice) MoveAndAppendTo(dest LocationSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es LocationSlice) RemoveIf(f func(Location) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es LocationSlice) CopyTo(dest LocationSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Location, srcLen) + } + for i := range *es.orig { + newLocation(&(*es.orig)[i], es.state).CopyTo(newLocation(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_locationslice_test.go b/pdata/pprofile/generated_locationslice_test.go new file mode 100644 index 00000000000..06d6946279f --- /dev/null +++ b/pdata/pprofile/generated_locationslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestLocationSlice(t *testing.T) { + es := NewLocationSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newLocationSlice(&[]otlpprofiles.Location{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewLocation() + testVal := generateTestLocation() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestLocation(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestLocationSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newLocationSlice(&[]otlpprofiles.Location{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewLocationSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestLocationSlice_CopyTo(t *testing.T) { + dest := NewLocationSlice() + // Test CopyTo to empty + NewLocationSlice().CopyTo(dest) + assert.Equal(t, NewLocationSlice(), dest) + + // Test CopyTo larger slice + generateTestLocationSlice().CopyTo(dest) + assert.Equal(t, generateTestLocationSlice(), dest) + + // Test CopyTo same size slice + generateTestLocationSlice().CopyTo(dest) + assert.Equal(t, generateTestLocationSlice(), dest) +} + +func TestLocationSlice_EnsureCapacity(t *testing.T) { + es := generateTestLocationSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestLocationSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestLocationSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestLocationSlice(), es) +} + +func TestLocationSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestLocationSlice() + dest := NewLocationSlice() + src := generateTestLocationSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLocationSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestLocationSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestLocationSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestLocationSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewLocationSlice() + emptySlice.RemoveIf(func(el Location) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestLocationSlice() + pos := 0 + filtered.RemoveIf(func(el Location) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestLocationSlice() LocationSlice { + es := NewLocationSlice() + fillTestLocationSlice(es) + return es +} + +func fillTestLocationSlice(es LocationSlice) { + *es.orig = make([]otlpprofiles.Location, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Location{} + fillTestLocation(newLocation(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_mapping.go b/pdata/pprofile/generated_mapping.go new file mode 100644 index 00000000000..66c7cd165df --- /dev/null +++ b/pdata/pprofile/generated_mapping.go @@ -0,0 +1,190 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// Mapping describes the mapping of a binary in memory, including its address range, file offset, and metadata like build ID +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewMapping function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Mapping struct { + orig *otlpprofiles.Mapping + state *internal.State +} + +func newMapping(orig *otlpprofiles.Mapping, state *internal.State) Mapping { + return Mapping{orig: orig, state: state} +} + +// NewMapping creates a new empty Mapping. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewMapping() Mapping { + state := internal.StateMutable + return newMapping(&otlpprofiles.Mapping{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Mapping) MoveTo(dest Mapping) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Mapping{} +} + +// ID returns the id associated with this Mapping. +func (ms Mapping) ID() uint64 { + return ms.orig.Id +} + +// SetID replaces the id associated with this Mapping. +func (ms Mapping) SetID(v uint64) { + ms.state.AssertMutable() + ms.orig.Id = v +} + +// MemoryStart returns the memorystart associated with this Mapping. +func (ms Mapping) MemoryStart() uint64 { + return ms.orig.MemoryStart +} + +// SetMemoryStart replaces the memorystart associated with this Mapping. +func (ms Mapping) SetMemoryStart(v uint64) { + ms.state.AssertMutable() + ms.orig.MemoryStart = v +} + +// MemoryLimit returns the memorylimit associated with this Mapping. +func (ms Mapping) MemoryLimit() uint64 { + return ms.orig.MemoryLimit +} + +// SetMemoryLimit replaces the memorylimit associated with this Mapping. +func (ms Mapping) SetMemoryLimit(v uint64) { + ms.state.AssertMutable() + ms.orig.MemoryLimit = v +} + +// FileOffset returns the fileoffset associated with this Mapping. +func (ms Mapping) FileOffset() uint64 { + return ms.orig.FileOffset +} + +// SetFileOffset replaces the fileoffset associated with this Mapping. +func (ms Mapping) SetFileOffset(v uint64) { + ms.state.AssertMutable() + ms.orig.FileOffset = v +} + +// Filename returns the filename associated with this Mapping. +func (ms Mapping) Filename() int64 { + return ms.orig.Filename +} + +// SetFilename replaces the filename associated with this Mapping. +func (ms Mapping) SetFilename(v int64) { + ms.state.AssertMutable() + ms.orig.Filename = v +} + +// BuildID returns the buildid associated with this Mapping. +func (ms Mapping) BuildID() int64 { + return ms.orig.BuildId +} + +// SetBuildID replaces the buildid associated with this Mapping. +func (ms Mapping) SetBuildID(v int64) { + ms.state.AssertMutable() + ms.orig.BuildId = v +} + +// BuildIDKind returns the buildidkind associated with this Mapping. +func (ms Mapping) BuildIDKind() otlpprofiles.BuildIdKind { + return ms.orig.BuildIdKind +} + +// SetBuildIDKind replaces the buildidkind associated with this Mapping. +func (ms Mapping) SetBuildIDKind(v otlpprofiles.BuildIdKind) { + ms.state.AssertMutable() + ms.orig.BuildIdKind = v +} + +// Attributes returns the Attributes associated with this Mapping. +func (ms Mapping) Attributes() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.orig.Attributes, ms.state)) +} + +// HasFunctions returns the hasfunctions associated with this Mapping. +func (ms Mapping) HasFunctions() bool { + return ms.orig.HasFunctions +} + +// SetHasFunctions replaces the hasfunctions associated with this Mapping. +func (ms Mapping) SetHasFunctions(v bool) { + ms.state.AssertMutable() + ms.orig.HasFunctions = v +} + +// HasFilenames returns the hasfilenames associated with this Mapping. +func (ms Mapping) HasFilenames() bool { + return ms.orig.HasFilenames +} + +// SetHasFilenames replaces the hasfilenames associated with this Mapping. +func (ms Mapping) SetHasFilenames(v bool) { + ms.state.AssertMutable() + ms.orig.HasFilenames = v +} + +// HasLineNumbers returns the haslinenumbers associated with this Mapping. +func (ms Mapping) HasLineNumbers() bool { + return ms.orig.HasLineNumbers +} + +// SetHasLineNumbers replaces the haslinenumbers associated with this Mapping. +func (ms Mapping) SetHasLineNumbers(v bool) { + ms.state.AssertMutable() + ms.orig.HasLineNumbers = v +} + +// HasInlineFrames returns the hasinlineframes associated with this Mapping. +func (ms Mapping) HasInlineFrames() bool { + return ms.orig.HasInlineFrames +} + +// SetHasInlineFrames replaces the hasinlineframes associated with this Mapping. +func (ms Mapping) SetHasInlineFrames(v bool) { + ms.state.AssertMutable() + ms.orig.HasInlineFrames = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Mapping) CopyTo(dest Mapping) { + dest.state.AssertMutable() + dest.SetID(ms.ID()) + dest.SetMemoryStart(ms.MemoryStart()) + dest.SetMemoryLimit(ms.MemoryLimit()) + dest.SetFileOffset(ms.FileOffset()) + dest.SetFilename(ms.Filename()) + dest.SetBuildID(ms.BuildID()) + dest.SetBuildIDKind(ms.BuildIDKind()) + ms.Attributes().CopyTo(dest.Attributes()) + dest.SetHasFunctions(ms.HasFunctions()) + dest.SetHasFilenames(ms.HasFilenames()) + dest.SetHasLineNumbers(ms.HasLineNumbers()) + dest.SetHasInlineFrames(ms.HasInlineFrames()) +} diff --git a/pdata/pprofile/generated_mapping_test.go b/pdata/pprofile/generated_mapping_test.go new file mode 100644 index 00000000000..6a50c205084 --- /dev/null +++ b/pdata/pprofile/generated_mapping_test.go @@ -0,0 +1,167 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestMapping_MoveTo(t *testing.T) { + ms := generateTestMapping() + dest := NewMapping() + ms.MoveTo(dest) + assert.Equal(t, NewMapping(), ms) + assert.Equal(t, generateTestMapping(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newMapping(&otlpprofiles.Mapping{}, &sharedState)) }) + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).MoveTo(dest) }) +} + +func TestMapping_CopyTo(t *testing.T) { + ms := NewMapping() + orig := NewMapping() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestMapping() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newMapping(&otlpprofiles.Mapping{}, &sharedState)) }) +} + +func TestMapping_ID(t *testing.T) { + ms := NewMapping() + assert.Equal(t, uint64(0), ms.ID()) + ms.SetID(uint64(1)) + assert.Equal(t, uint64(1), ms.ID()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetID(uint64(1)) }) +} + +func TestMapping_MemoryStart(t *testing.T) { + ms := NewMapping() + assert.Equal(t, uint64(0), ms.MemoryStart()) + ms.SetMemoryStart(uint64(1)) + assert.Equal(t, uint64(1), ms.MemoryStart()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetMemoryStart(uint64(1)) }) +} + +func TestMapping_MemoryLimit(t *testing.T) { + ms := NewMapping() + assert.Equal(t, uint64(0), ms.MemoryLimit()) + ms.SetMemoryLimit(uint64(1)) + assert.Equal(t, uint64(1), ms.MemoryLimit()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetMemoryLimit(uint64(1)) }) +} + +func TestMapping_FileOffset(t *testing.T) { + ms := NewMapping() + assert.Equal(t, uint64(0), ms.FileOffset()) + ms.SetFileOffset(uint64(1)) + assert.Equal(t, uint64(1), ms.FileOffset()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetFileOffset(uint64(1)) }) +} + +func TestMapping_Filename(t *testing.T) { + ms := NewMapping() + assert.Equal(t, int64(0), ms.Filename()) + ms.SetFilename(int64(1)) + assert.Equal(t, int64(1), ms.Filename()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetFilename(int64(1)) }) +} + +func TestMapping_BuildID(t *testing.T) { + ms := NewMapping() + assert.Equal(t, int64(0), ms.BuildID()) + ms.SetBuildID(int64(1)) + assert.Equal(t, int64(1), ms.BuildID()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetBuildID(int64(1)) }) +} + +func TestMapping_BuildIDKind(t *testing.T) { + ms := NewMapping() + assert.Equal(t, otlpprofiles.BuildIdKind(0), ms.BuildIDKind()) + ms.SetBuildIDKind(otlpprofiles.BuildIdKind(1)) + assert.Equal(t, otlpprofiles.BuildIdKind(1), ms.BuildIDKind()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetBuildIDKind(otlpprofiles.BuildIdKind(1)) }) +} + +func TestMapping_Attributes(t *testing.T) { + ms := NewMapping() + assert.Equal(t, pcommon.NewUInt64Slice(), ms.Attributes()) + internal.FillTestUInt64Slice(internal.UInt64Slice(ms.Attributes())) + assert.Equal(t, pcommon.UInt64Slice(internal.GenerateTestUInt64Slice()), ms.Attributes()) +} + +func TestMapping_HasFunctions(t *testing.T) { + ms := NewMapping() + assert.Equal(t, false, ms.HasFunctions()) + ms.SetHasFunctions(true) + assert.Equal(t, true, ms.HasFunctions()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetHasFunctions(true) }) +} + +func TestMapping_HasFilenames(t *testing.T) { + ms := NewMapping() + assert.Equal(t, false, ms.HasFilenames()) + ms.SetHasFilenames(true) + assert.Equal(t, true, ms.HasFilenames()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetHasFilenames(true) }) +} + +func TestMapping_HasLineNumbers(t *testing.T) { + ms := NewMapping() + assert.Equal(t, false, ms.HasLineNumbers()) + ms.SetHasLineNumbers(true) + assert.Equal(t, true, ms.HasLineNumbers()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetHasLineNumbers(true) }) +} + +func TestMapping_HasInlineFrames(t *testing.T) { + ms := NewMapping() + assert.Equal(t, false, ms.HasInlineFrames()) + ms.SetHasInlineFrames(true) + assert.Equal(t, true, ms.HasInlineFrames()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newMapping(&otlpprofiles.Mapping{}, &sharedState).SetHasInlineFrames(true) }) +} + +func generateTestMapping() Mapping { + tv := NewMapping() + fillTestMapping(tv) + return tv +} + +func fillTestMapping(tv Mapping) { + tv.orig.Id = uint64(1) + tv.orig.MemoryStart = uint64(1) + tv.orig.MemoryLimit = uint64(1) + tv.orig.FileOffset = uint64(1) + tv.orig.Filename = int64(1) + tv.orig.BuildId = int64(1) + tv.orig.BuildIdKind = otlpprofiles.BuildIdKind(1) + internal.FillTestUInt64Slice(internal.NewUInt64Slice(&tv.orig.Attributes, tv.state)) + tv.orig.HasFunctions = true + tv.orig.HasFilenames = true + tv.orig.HasLineNumbers = true + tv.orig.HasInlineFrames = true +} diff --git a/pdata/pprofile/generated_mappingslice.go b/pdata/pprofile/generated_mappingslice.go new file mode 100644 index 00000000000..cf0f1361483 --- /dev/null +++ b/pdata/pprofile/generated_mappingslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// MappingSlice logically represents a slice of Mapping. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewMappingSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type MappingSlice struct { + orig *[]otlpprofiles.Mapping + state *internal.State +} + +func newMappingSlice(orig *[]otlpprofiles.Mapping, state *internal.State) MappingSlice { + return MappingSlice{orig: orig, state: state} +} + +// NewMappingSlice creates a MappingSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewMappingSlice() MappingSlice { + orig := []otlpprofiles.Mapping(nil) + state := internal.StateMutable + return newMappingSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewMappingSlice()". +func (es MappingSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es MappingSlice) At(i int) Mapping { + return newMapping(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new MappingSlice can be initialized: +// +// es := NewMappingSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es MappingSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Mapping, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Mapping. +// It returns the newly added Mapping. +func (es MappingSlice) AppendEmpty() Mapping { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Mapping{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es MappingSlice) MoveAndAppendTo(dest MappingSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es MappingSlice) RemoveIf(f func(Mapping) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es MappingSlice) CopyTo(dest MappingSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Mapping, srcLen) + } + for i := range *es.orig { + newMapping(&(*es.orig)[i], es.state).CopyTo(newMapping(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_mappingslice_test.go b/pdata/pprofile/generated_mappingslice_test.go new file mode 100644 index 00000000000..b25cb952ce1 --- /dev/null +++ b/pdata/pprofile/generated_mappingslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestMappingSlice(t *testing.T) { + es := NewMappingSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newMappingSlice(&[]otlpprofiles.Mapping{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewMapping() + testVal := generateTestMapping() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestMapping(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestMappingSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newMappingSlice(&[]otlpprofiles.Mapping{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewMappingSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestMappingSlice_CopyTo(t *testing.T) { + dest := NewMappingSlice() + // Test CopyTo to empty + NewMappingSlice().CopyTo(dest) + assert.Equal(t, NewMappingSlice(), dest) + + // Test CopyTo larger slice + generateTestMappingSlice().CopyTo(dest) + assert.Equal(t, generateTestMappingSlice(), dest) + + // Test CopyTo same size slice + generateTestMappingSlice().CopyTo(dest) + assert.Equal(t, generateTestMappingSlice(), dest) +} + +func TestMappingSlice_EnsureCapacity(t *testing.T) { + es := generateTestMappingSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestMappingSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestMappingSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestMappingSlice(), es) +} + +func TestMappingSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestMappingSlice() + dest := NewMappingSlice() + src := generateTestMappingSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestMappingSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestMappingSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestMappingSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestMappingSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewMappingSlice() + emptySlice.RemoveIf(func(el Mapping) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestMappingSlice() + pos := 0 + filtered.RemoveIf(func(el Mapping) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestMappingSlice() MappingSlice { + es := NewMappingSlice() + fillTestMappingSlice(es) + return es +} + +func fillTestMappingSlice(es MappingSlice) { + *es.orig = make([]otlpprofiles.Mapping, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Mapping{} + fillTestMapping(newMapping(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_profile.go b/pdata/pprofile/generated_profile.go new file mode 100644 index 00000000000..38f1cf3f0f3 --- /dev/null +++ b/pdata/pprofile/generated_profile.go @@ -0,0 +1,196 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// Profile are an implementation of the pprofextended data model. + +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewProfile function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Profile struct { + orig *otlpprofiles.Profile + state *internal.State +} + +func newProfile(orig *otlpprofiles.Profile, state *internal.State) Profile { + return Profile{orig: orig, state: state} +} + +// NewProfile creates a new empty Profile. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewProfile() Profile { + state := internal.StateMutable + return newProfile(&otlpprofiles.Profile{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Profile) MoveTo(dest Profile) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Profile{} +} + +// SampleType returns the SampleType associated with this Profile. +func (ms Profile) SampleType() ValueTypeSlice { + return newValueTypeSlice(&ms.orig.SampleType, ms.state) +} + +// Sample returns the Sample associated with this Profile. +func (ms Profile) Sample() SampleSlice { + return newSampleSlice(&ms.orig.Sample, ms.state) +} + +// Mapping returns the Mapping associated with this Profile. +func (ms Profile) Mapping() MappingSlice { + return newMappingSlice(&ms.orig.Mapping, ms.state) +} + +// Location returns the Location associated with this Profile. +func (ms Profile) Location() LocationSlice { + return newLocationSlice(&ms.orig.Location, ms.state) +} + +// LocationIndices returns the LocationIndices associated with this Profile. +func (ms Profile) LocationIndices() pcommon.Int64Slice { + return pcommon.Int64Slice(internal.NewInt64Slice(&ms.orig.LocationIndices, ms.state)) +} + +// Function returns the Function associated with this Profile. +func (ms Profile) Function() FunctionSlice { + return newFunctionSlice(&ms.orig.Function, ms.state) +} + +// AttributeTable returns the AttributeTable associated with this Profile. +func (ms Profile) AttributeTable() pcommon.Map { + return pcommon.Map(internal.NewMap(&ms.orig.AttributeTable, ms.state)) +} + +// AttributeUnits returns the AttributeUnits associated with this Profile. +func (ms Profile) AttributeUnits() AttributeUnitSlice { + return newAttributeUnitSlice(&ms.orig.AttributeUnits, ms.state) +} + +// LinkTable returns the LinkTable associated with this Profile. +func (ms Profile) LinkTable() LinkSlice { + return newLinkSlice(&ms.orig.LinkTable, ms.state) +} + +// StringTable returns the StringTable associated with this Profile. +func (ms Profile) StringTable() pcommon.StringSlice { + return pcommon.StringSlice(internal.NewStringSlice(&ms.orig.StringTable, ms.state)) +} + +// DropFrames returns the dropframes associated with this Profile. +func (ms Profile) DropFrames() int64 { + return ms.orig.DropFrames +} + +// SetDropFrames replaces the dropframes associated with this Profile. +func (ms Profile) SetDropFrames(v int64) { + ms.state.AssertMutable() + ms.orig.DropFrames = v +} + +// KeepFrames returns the keepframes associated with this Profile. +func (ms Profile) KeepFrames() int64 { + return ms.orig.KeepFrames +} + +// SetKeepFrames replaces the keepframes associated with this Profile. +func (ms Profile) SetKeepFrames(v int64) { + ms.state.AssertMutable() + ms.orig.KeepFrames = v +} + +// StartTime returns the starttime associated with this Profile. +func (ms Profile) StartTime() pcommon.Timestamp { + return pcommon.Timestamp(ms.orig.TimeNanos) +} + +// SetStartTime replaces the starttime associated with this Profile. +func (ms Profile) SetStartTime(v pcommon.Timestamp) { + ms.state.AssertMutable() + ms.orig.TimeNanos = int64(v) +} + +// Duration returns the duration associated with this Profile. +func (ms Profile) Duration() pcommon.Timestamp { + return pcommon.Timestamp(ms.orig.DurationNanos) +} + +// SetDuration replaces the duration associated with this Profile. +func (ms Profile) SetDuration(v pcommon.Timestamp) { + ms.state.AssertMutable() + ms.orig.DurationNanos = int64(v) +} + +// PeriodType returns the periodtype associated with this Profile. +func (ms Profile) PeriodType() ValueType { + return newValueType(&ms.orig.PeriodType, ms.state) +} + +// Period returns the period associated with this Profile. +func (ms Profile) Period() int64 { + return ms.orig.Period +} + +// SetPeriod replaces the period associated with this Profile. +func (ms Profile) SetPeriod(v int64) { + ms.state.AssertMutable() + ms.orig.Period = v +} + +// Comment returns the Comment associated with this Profile. +func (ms Profile) Comment() pcommon.Int64Slice { + return pcommon.Int64Slice(internal.NewInt64Slice(&ms.orig.Comment, ms.state)) +} + +// DefaultSampleType returns the defaultsampletype associated with this Profile. +func (ms Profile) DefaultSampleType() int64 { + return ms.orig.DefaultSampleType +} + +// SetDefaultSampleType replaces the defaultsampletype associated with this Profile. +func (ms Profile) SetDefaultSampleType(v int64) { + ms.state.AssertMutable() + ms.orig.DefaultSampleType = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Profile) CopyTo(dest Profile) { + dest.state.AssertMutable() + ms.SampleType().CopyTo(dest.SampleType()) + ms.Sample().CopyTo(dest.Sample()) + ms.Mapping().CopyTo(dest.Mapping()) + ms.Location().CopyTo(dest.Location()) + ms.LocationIndices().CopyTo(dest.LocationIndices()) + ms.Function().CopyTo(dest.Function()) + ms.AttributeTable().CopyTo(dest.AttributeTable()) + ms.AttributeUnits().CopyTo(dest.AttributeUnits()) + ms.LinkTable().CopyTo(dest.LinkTable()) + ms.StringTable().CopyTo(dest.StringTable()) + dest.SetDropFrames(ms.DropFrames()) + dest.SetKeepFrames(ms.KeepFrames()) + dest.SetStartTime(ms.StartTime()) + dest.SetDuration(ms.Duration()) + ms.PeriodType().CopyTo(dest.PeriodType()) + dest.SetPeriod(ms.Period()) + ms.Comment().CopyTo(dest.Comment()) + dest.SetDefaultSampleType(ms.DefaultSampleType()) +} diff --git a/pdata/pprofile/generated_profile_test.go b/pdata/pprofile/generated_profile_test.go new file mode 100644 index 00000000000..e040f2c0dc4 --- /dev/null +++ b/pdata/pprofile/generated_profile_test.go @@ -0,0 +1,202 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestProfile_MoveTo(t *testing.T) { + ms := generateTestProfile() + dest := NewProfile() + ms.MoveTo(dest) + assert.Equal(t, NewProfile(), ms) + assert.Equal(t, generateTestProfile(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newProfile(&otlpprofiles.Profile{}, &sharedState)) }) + assert.Panics(t, func() { newProfile(&otlpprofiles.Profile{}, &sharedState).MoveTo(dest) }) +} + +func TestProfile_CopyTo(t *testing.T) { + ms := NewProfile() + orig := NewProfile() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestProfile() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newProfile(&otlpprofiles.Profile{}, &sharedState)) }) +} + +func TestProfile_SampleType(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewValueTypeSlice(), ms.SampleType()) + fillTestValueTypeSlice(ms.SampleType()) + assert.Equal(t, generateTestValueTypeSlice(), ms.SampleType()) +} + +func TestProfile_Sample(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewSampleSlice(), ms.Sample()) + fillTestSampleSlice(ms.Sample()) + assert.Equal(t, generateTestSampleSlice(), ms.Sample()) +} + +func TestProfile_Mapping(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewMappingSlice(), ms.Mapping()) + fillTestMappingSlice(ms.Mapping()) + assert.Equal(t, generateTestMappingSlice(), ms.Mapping()) +} + +func TestProfile_Location(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewLocationSlice(), ms.Location()) + fillTestLocationSlice(ms.Location()) + assert.Equal(t, generateTestLocationSlice(), ms.Location()) +} + +func TestProfile_LocationIndices(t *testing.T) { + ms := NewProfile() + assert.Equal(t, pcommon.NewInt64Slice(), ms.LocationIndices()) + internal.FillTestInt64Slice(internal.Int64Slice(ms.LocationIndices())) + assert.Equal(t, pcommon.Int64Slice(internal.GenerateTestInt64Slice()), ms.LocationIndices()) +} + +func TestProfile_Function(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewFunctionSlice(), ms.Function()) + fillTestFunctionSlice(ms.Function()) + assert.Equal(t, generateTestFunctionSlice(), ms.Function()) +} + +func TestProfile_AttributeTable(t *testing.T) { + ms := NewProfile() + assert.Equal(t, pcommon.NewMap(), ms.AttributeTable()) + internal.FillTestMap(internal.Map(ms.AttributeTable())) + assert.Equal(t, pcommon.Map(internal.GenerateTestMap()), ms.AttributeTable()) +} + +func TestProfile_AttributeUnits(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewAttributeUnitSlice(), ms.AttributeUnits()) + fillTestAttributeUnitSlice(ms.AttributeUnits()) + assert.Equal(t, generateTestAttributeUnitSlice(), ms.AttributeUnits()) +} + +func TestProfile_LinkTable(t *testing.T) { + ms := NewProfile() + assert.Equal(t, NewLinkSlice(), ms.LinkTable()) + fillTestLinkSlice(ms.LinkTable()) + assert.Equal(t, generateTestLinkSlice(), ms.LinkTable()) +} + +func TestProfile_StringTable(t *testing.T) { + ms := NewProfile() + assert.Equal(t, pcommon.NewStringSlice(), ms.StringTable()) + internal.FillTestStringSlice(internal.StringSlice(ms.StringTable())) + assert.Equal(t, pcommon.StringSlice(internal.GenerateTestStringSlice()), ms.StringTable()) +} + +func TestProfile_DropFrames(t *testing.T) { + ms := NewProfile() + assert.Equal(t, int64(0), ms.DropFrames()) + ms.SetDropFrames(int64(1)) + assert.Equal(t, int64(1), ms.DropFrames()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newProfile(&otlpprofiles.Profile{}, &sharedState).SetDropFrames(int64(1)) }) +} + +func TestProfile_KeepFrames(t *testing.T) { + ms := NewProfile() + assert.Equal(t, int64(0), ms.KeepFrames()) + ms.SetKeepFrames(int64(1)) + assert.Equal(t, int64(1), ms.KeepFrames()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newProfile(&otlpprofiles.Profile{}, &sharedState).SetKeepFrames(int64(1)) }) +} + +func TestProfile_StartTime(t *testing.T) { + ms := NewProfile() + assert.Equal(t, pcommon.Timestamp(0), ms.StartTime()) + testValStartTime := pcommon.Timestamp(1234567890) + ms.SetStartTime(testValStartTime) + assert.Equal(t, testValStartTime, ms.StartTime()) +} + +func TestProfile_Duration(t *testing.T) { + ms := NewProfile() + assert.Equal(t, pcommon.Timestamp(0), ms.Duration()) + testValDuration := pcommon.Timestamp(1234567890) + ms.SetDuration(testValDuration) + assert.Equal(t, testValDuration, ms.Duration()) +} + +func TestProfile_PeriodType(t *testing.T) { + ms := NewProfile() + fillTestValueType(ms.PeriodType()) + assert.Equal(t, generateTestValueType(), ms.PeriodType()) +} + +func TestProfile_Period(t *testing.T) { + ms := NewProfile() + assert.Equal(t, int64(0), ms.Period()) + ms.SetPeriod(int64(1)) + assert.Equal(t, int64(1), ms.Period()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newProfile(&otlpprofiles.Profile{}, &sharedState).SetPeriod(int64(1)) }) +} + +func TestProfile_Comment(t *testing.T) { + ms := NewProfile() + assert.Equal(t, pcommon.NewInt64Slice(), ms.Comment()) + internal.FillTestInt64Slice(internal.Int64Slice(ms.Comment())) + assert.Equal(t, pcommon.Int64Slice(internal.GenerateTestInt64Slice()), ms.Comment()) +} + +func TestProfile_DefaultSampleType(t *testing.T) { + ms := NewProfile() + assert.Equal(t, int64(0), ms.DefaultSampleType()) + ms.SetDefaultSampleType(int64(1)) + assert.Equal(t, int64(1), ms.DefaultSampleType()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newProfile(&otlpprofiles.Profile{}, &sharedState).SetDefaultSampleType(int64(1)) }) +} + +func generateTestProfile() Profile { + tv := NewProfile() + fillTestProfile(tv) + return tv +} + +func fillTestProfile(tv Profile) { + fillTestValueTypeSlice(newValueTypeSlice(&tv.orig.SampleType, tv.state)) + fillTestSampleSlice(newSampleSlice(&tv.orig.Sample, tv.state)) + fillTestMappingSlice(newMappingSlice(&tv.orig.Mapping, tv.state)) + fillTestLocationSlice(newLocationSlice(&tv.orig.Location, tv.state)) + internal.FillTestInt64Slice(internal.NewInt64Slice(&tv.orig.LocationIndices, tv.state)) + fillTestFunctionSlice(newFunctionSlice(&tv.orig.Function, tv.state)) + internal.FillTestMap(internal.NewMap(&tv.orig.AttributeTable, tv.state)) + fillTestAttributeUnitSlice(newAttributeUnitSlice(&tv.orig.AttributeUnits, tv.state)) + fillTestLinkSlice(newLinkSlice(&tv.orig.LinkTable, tv.state)) + internal.FillTestStringSlice(internal.NewStringSlice(&tv.orig.StringTable, tv.state)) + tv.orig.DropFrames = int64(1) + tv.orig.KeepFrames = int64(1) + tv.orig.TimeNanos = 1234567890 + tv.orig.DurationNanos = 1234567890 + fillTestValueType(newValueType(&tv.orig.PeriodType, tv.state)) + tv.orig.Period = int64(1) + internal.FillTestInt64Slice(internal.NewInt64Slice(&tv.orig.Comment, tv.state)) + tv.orig.DefaultSampleType = int64(1) +} diff --git a/pdata/pprofile/generated_profilecontainer.go b/pdata/pprofile/generated_profilecontainer.go new file mode 100644 index 00000000000..6bf15a98b45 --- /dev/null +++ b/pdata/pprofile/generated_profilecontainer.go @@ -0,0 +1,106 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// ProfileContainer are an experimental implementation of the OpenTelemetry Profiles Data Model. + +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewProfileContainer function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ProfileContainer struct { + orig *otlpprofiles.ProfileContainer + state *internal.State +} + +func newProfileContainer(orig *otlpprofiles.ProfileContainer, state *internal.State) ProfileContainer { + return ProfileContainer{orig: orig, state: state} +} + +// NewProfileContainer creates a new empty ProfileContainer. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewProfileContainer() ProfileContainer { + state := internal.StateMutable + return newProfileContainer(&otlpprofiles.ProfileContainer{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms ProfileContainer) MoveTo(dest ProfileContainer) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.ProfileContainer{} +} + +// ProfileID returns the ProfileId associated with this ProfileContainer. +func (ms ProfileContainer) ProfileID() pcommon.ByteSlice { + return pcommon.ByteSlice(internal.NewByteSlice(&ms.orig.ProfileId, ms.state)) +} + +// StartTime returns the starttime associated with this ProfileContainer. +func (ms ProfileContainer) StartTime() pcommon.Timestamp { + return pcommon.Timestamp(ms.orig.StartTimeUnixNano) +} + +// SetStartTime replaces the starttime associated with this ProfileContainer. +func (ms ProfileContainer) SetStartTime(v pcommon.Timestamp) { + ms.state.AssertMutable() + ms.orig.StartTimeUnixNano = uint64(v) +} + +// EndTime returns the endtime associated with this ProfileContainer. +func (ms ProfileContainer) EndTime() pcommon.Timestamp { + return pcommon.Timestamp(ms.orig.EndTimeUnixNano) +} + +// SetEndTime replaces the endtime associated with this ProfileContainer. +func (ms ProfileContainer) SetEndTime(v pcommon.Timestamp) { + ms.state.AssertMutable() + ms.orig.EndTimeUnixNano = uint64(v) +} + +// Attributes returns the Attributes associated with this ProfileContainer. +func (ms ProfileContainer) Attributes() pcommon.Map { + return pcommon.Map(internal.NewMap(&ms.orig.Attributes, ms.state)) +} + +// DroppedAttributesCount returns the droppedattributescount associated with this ProfileContainer. +func (ms ProfileContainer) DroppedAttributesCount() uint32 { + return ms.orig.DroppedAttributesCount +} + +// SetDroppedAttributesCount replaces the droppedattributescount associated with this ProfileContainer. +func (ms ProfileContainer) SetDroppedAttributesCount(v uint32) { + ms.state.AssertMutable() + ms.orig.DroppedAttributesCount = v +} + +// Profile returns the profile associated with this ProfileContainer. +func (ms ProfileContainer) Profile() Profile { + return newProfile(&ms.orig.Profile, ms.state) +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms ProfileContainer) CopyTo(dest ProfileContainer) { + dest.state.AssertMutable() + ms.ProfileID().CopyTo(dest.ProfileID()) + dest.SetStartTime(ms.StartTime()) + dest.SetEndTime(ms.EndTime()) + ms.Attributes().CopyTo(dest.Attributes()) + dest.SetDroppedAttributesCount(ms.DroppedAttributesCount()) + ms.Profile().CopyTo(dest.Profile()) +} diff --git a/pdata/pprofile/generated_profilecontainer_test.go b/pdata/pprofile/generated_profilecontainer_test.go new file mode 100644 index 00000000000..72a922a457d --- /dev/null +++ b/pdata/pprofile/generated_profilecontainer_test.go @@ -0,0 +1,102 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestProfileContainer_MoveTo(t *testing.T) { + ms := generateTestProfileContainer() + dest := NewProfileContainer() + ms.MoveTo(dest) + assert.Equal(t, NewProfileContainer(), ms) + assert.Equal(t, generateTestProfileContainer(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newProfileContainer(&otlpprofiles.ProfileContainer{}, &sharedState)) }) + assert.Panics(t, func() { newProfileContainer(&otlpprofiles.ProfileContainer{}, &sharedState).MoveTo(dest) }) +} + +func TestProfileContainer_CopyTo(t *testing.T) { + ms := NewProfileContainer() + orig := NewProfileContainer() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestProfileContainer() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newProfileContainer(&otlpprofiles.ProfileContainer{}, &sharedState)) }) +} + +func TestProfileContainer_ProfileID(t *testing.T) { + ms := NewProfileContainer() + assert.Equal(t, pcommon.NewByteSlice(), ms.ProfileID()) + internal.FillTestByteSlice(internal.ByteSlice(ms.ProfileID())) + assert.Equal(t, pcommon.ByteSlice(internal.GenerateTestByteSlice()), ms.ProfileID()) +} + +func TestProfileContainer_StartTime(t *testing.T) { + ms := NewProfileContainer() + assert.Equal(t, pcommon.Timestamp(0), ms.StartTime()) + testValStartTime := pcommon.Timestamp(1234567890) + ms.SetStartTime(testValStartTime) + assert.Equal(t, testValStartTime, ms.StartTime()) +} + +func TestProfileContainer_EndTime(t *testing.T) { + ms := NewProfileContainer() + assert.Equal(t, pcommon.Timestamp(0), ms.EndTime()) + testValEndTime := pcommon.Timestamp(1234567890) + ms.SetEndTime(testValEndTime) + assert.Equal(t, testValEndTime, ms.EndTime()) +} + +func TestProfileContainer_Attributes(t *testing.T) { + ms := NewProfileContainer() + assert.Equal(t, pcommon.NewMap(), ms.Attributes()) + internal.FillTestMap(internal.Map(ms.Attributes())) + assert.Equal(t, pcommon.Map(internal.GenerateTestMap()), ms.Attributes()) +} + +func TestProfileContainer_DroppedAttributesCount(t *testing.T) { + ms := NewProfileContainer() + assert.Equal(t, uint32(0), ms.DroppedAttributesCount()) + ms.SetDroppedAttributesCount(uint32(17)) + assert.Equal(t, uint32(17), ms.DroppedAttributesCount()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + newProfileContainer(&otlpprofiles.ProfileContainer{}, &sharedState).SetDroppedAttributesCount(uint32(17)) + }) +} + +func TestProfileContainer_Profile(t *testing.T) { + ms := NewProfileContainer() + fillTestProfile(ms.Profile()) + assert.Equal(t, generateTestProfile(), ms.Profile()) +} + +func generateTestProfileContainer() ProfileContainer { + tv := NewProfileContainer() + fillTestProfileContainer(tv) + return tv +} + +func fillTestProfileContainer(tv ProfileContainer) { + internal.FillTestByteSlice(internal.NewByteSlice(&tv.orig.ProfileId, tv.state)) + tv.orig.StartTimeUnixNano = 1234567890 + tv.orig.EndTimeUnixNano = 1234567890 + internal.FillTestMap(internal.NewMap(&tv.orig.Attributes, tv.state)) + tv.orig.DroppedAttributesCount = uint32(17) + fillTestProfile(newProfile(&tv.orig.Profile, tv.state)) +} diff --git a/pdata/pprofile/generated_profilescontainersslice.go b/pdata/pprofile/generated_profilescontainersslice.go new file mode 100644 index 00000000000..74ac2cc4578 --- /dev/null +++ b/pdata/pprofile/generated_profilescontainersslice.go @@ -0,0 +1,152 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "sort" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// ProfilesContainersSlice logically represents a slice of ProfileContainer. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewProfilesContainersSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ProfilesContainersSlice struct { + orig *[]*otlpprofiles.ProfileContainer + state *internal.State +} + +func newProfilesContainersSlice(orig *[]*otlpprofiles.ProfileContainer, state *internal.State) ProfilesContainersSlice { + return ProfilesContainersSlice{orig: orig, state: state} +} + +// NewProfilesContainersSlice creates a ProfilesContainersSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewProfilesContainersSlice() ProfilesContainersSlice { + orig := []*otlpprofiles.ProfileContainer(nil) + state := internal.StateMutable + return newProfilesContainersSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewProfilesContainersSlice()". +func (es ProfilesContainersSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es ProfilesContainersSlice) At(i int) ProfileContainer { + return newProfileContainer((*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new ProfilesContainersSlice can be initialized: +// +// es := NewProfilesContainersSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es ProfilesContainersSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]*otlpprofiles.ProfileContainer, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty ProfileContainer. +// It returns the newly added ProfileContainer. +func (es ProfilesContainersSlice) AppendEmpty() ProfileContainer { + es.state.AssertMutable() + *es.orig = append(*es.orig, &otlpprofiles.ProfileContainer{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es ProfilesContainersSlice) MoveAndAppendTo(dest ProfilesContainersSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es ProfilesContainersSlice) RemoveIf(f func(ProfileContainer) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es ProfilesContainersSlice) CopyTo(dest ProfilesContainersSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + for i := range *es.orig { + newProfileContainer((*es.orig)[i], es.state).CopyTo(newProfileContainer((*dest.orig)[i], dest.state)) + } + return + } + origs := make([]otlpprofiles.ProfileContainer, srcLen) + wrappers := make([]*otlpprofiles.ProfileContainer, srcLen) + for i := range *es.orig { + wrappers[i] = &origs[i] + newProfileContainer((*es.orig)[i], es.state).CopyTo(newProfileContainer(wrappers[i], dest.state)) + } + *dest.orig = wrappers +} + +// Sort sorts the ProfileContainer elements within ProfilesContainersSlice given the +// provided less function so that two instances of ProfilesContainersSlice +// can be compared. +func (es ProfilesContainersSlice) Sort(less func(a, b ProfileContainer) bool) { + es.state.AssertMutable() + sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) }) +} diff --git a/pdata/pprofile/generated_profilescontainersslice_test.go b/pdata/pprofile/generated_profilescontainersslice_test.go new file mode 100644 index 00000000000..428fd677c3a --- /dev/null +++ b/pdata/pprofile/generated_profilescontainersslice_test.go @@ -0,0 +1,154 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + "unsafe" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestProfilesContainersSlice(t *testing.T) { + es := NewProfilesContainersSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newProfilesContainersSlice(&[]*otlpprofiles.ProfileContainer{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewProfileContainer() + testVal := generateTestProfileContainer() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestProfileContainer(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestProfilesContainersSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newProfilesContainersSlice(&[]*otlpprofiles.ProfileContainer{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewProfilesContainersSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestProfilesContainersSlice_CopyTo(t *testing.T) { + dest := NewProfilesContainersSlice() + // Test CopyTo to empty + NewProfilesContainersSlice().CopyTo(dest) + assert.Equal(t, NewProfilesContainersSlice(), dest) + + // Test CopyTo larger slice + generateTestProfilesContainersSlice().CopyTo(dest) + assert.Equal(t, generateTestProfilesContainersSlice(), dest) + + // Test CopyTo same size slice + generateTestProfilesContainersSlice().CopyTo(dest) + assert.Equal(t, generateTestProfilesContainersSlice(), dest) +} + +func TestProfilesContainersSlice_EnsureCapacity(t *testing.T) { + es := generateTestProfilesContainersSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestProfilesContainersSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestProfilesContainersSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestProfilesContainersSlice(), es) +} + +func TestProfilesContainersSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestProfilesContainersSlice() + dest := NewProfilesContainersSlice() + src := generateTestProfilesContainersSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestProfilesContainersSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestProfilesContainersSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestProfilesContainersSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestProfilesContainersSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewProfilesContainersSlice() + emptySlice.RemoveIf(func(el ProfileContainer) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestProfilesContainersSlice() + pos := 0 + filtered.RemoveIf(func(el ProfileContainer) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func TestProfilesContainersSlice_Sort(t *testing.T) { + es := generateTestProfilesContainersSlice() + es.Sort(func(a, b ProfileContainer) bool { + return uintptr(unsafe.Pointer(a.orig)) < uintptr(unsafe.Pointer(b.orig)) + }) + for i := 1; i < es.Len(); i++ { + assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) < uintptr(unsafe.Pointer(es.At(i).orig))) + } + es.Sort(func(a, b ProfileContainer) bool { + return uintptr(unsafe.Pointer(a.orig)) > uintptr(unsafe.Pointer(b.orig)) + }) + for i := 1; i < es.Len(); i++ { + assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) > uintptr(unsafe.Pointer(es.At(i).orig))) + } +} + +func generateTestProfilesContainersSlice() ProfilesContainersSlice { + es := NewProfilesContainersSlice() + fillTestProfilesContainersSlice(es) + return es +} + +func fillTestProfilesContainersSlice(es ProfilesContainersSlice) { + *es.orig = make([]*otlpprofiles.ProfileContainer, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = &otlpprofiles.ProfileContainer{} + fillTestProfileContainer(newProfileContainer((*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_resourceprofiles.go b/pdata/pprofile/generated_resourceprofiles.go new file mode 100644 index 00000000000..35c0161d7d7 --- /dev/null +++ b/pdata/pprofile/generated_resourceprofiles.go @@ -0,0 +1,76 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// ResourceProfiles is a collection of profiles from a Resource. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewResourceProfiles function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ResourceProfiles struct { + orig *otlpprofiles.ResourceProfiles + state *internal.State +} + +func newResourceProfiles(orig *otlpprofiles.ResourceProfiles, state *internal.State) ResourceProfiles { + return ResourceProfiles{orig: orig, state: state} +} + +// NewResourceProfiles creates a new empty ResourceProfiles. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewResourceProfiles() ResourceProfiles { + state := internal.StateMutable + return newResourceProfiles(&otlpprofiles.ResourceProfiles{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms ResourceProfiles) MoveTo(dest ResourceProfiles) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.ResourceProfiles{} +} + +// Resource returns the resource associated with this ResourceProfiles. +func (ms ResourceProfiles) Resource() pcommon.Resource { + return pcommon.Resource(internal.NewResource(&ms.orig.Resource, ms.state)) +} + +// SchemaUrl returns the schemaurl associated with this ResourceProfiles. +func (ms ResourceProfiles) SchemaUrl() string { + return ms.orig.SchemaUrl +} + +// SetSchemaUrl replaces the schemaurl associated with this ResourceProfiles. +func (ms ResourceProfiles) SetSchemaUrl(v string) { + ms.state.AssertMutable() + ms.orig.SchemaUrl = v +} + +// ScopeProfiles returns the ScopeProfiles associated with this ResourceProfiles. +func (ms ResourceProfiles) ScopeProfiles() ScopeProfilesSlice { + return newScopeProfilesSlice(&ms.orig.ScopeProfiles, ms.state) +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms ResourceProfiles) CopyTo(dest ResourceProfiles) { + dest.state.AssertMutable() + ms.Resource().CopyTo(dest.Resource()) + dest.SetSchemaUrl(ms.SchemaUrl()) + ms.ScopeProfiles().CopyTo(dest.ScopeProfiles()) +} diff --git a/pdata/pprofile/generated_resourceprofiles_test.go b/pdata/pprofile/generated_resourceprofiles_test.go new file mode 100644 index 00000000000..b4da47c3a8c --- /dev/null +++ b/pdata/pprofile/generated_resourceprofiles_test.go @@ -0,0 +1,76 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestResourceProfiles_MoveTo(t *testing.T) { + ms := generateTestResourceProfiles() + dest := NewResourceProfiles() + ms.MoveTo(dest) + assert.Equal(t, NewResourceProfiles(), ms) + assert.Equal(t, generateTestResourceProfiles(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newResourceProfiles(&otlpprofiles.ResourceProfiles{}, &sharedState)) }) + assert.Panics(t, func() { newResourceProfiles(&otlpprofiles.ResourceProfiles{}, &sharedState).MoveTo(dest) }) +} + +func TestResourceProfiles_CopyTo(t *testing.T) { + ms := NewResourceProfiles() + orig := NewResourceProfiles() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestResourceProfiles() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newResourceProfiles(&otlpprofiles.ResourceProfiles{}, &sharedState)) }) +} + +func TestResourceProfiles_Resource(t *testing.T) { + ms := NewResourceProfiles() + internal.FillTestResource(internal.Resource(ms.Resource())) + assert.Equal(t, pcommon.Resource(internal.GenerateTestResource()), ms.Resource()) +} + +func TestResourceProfiles_SchemaUrl(t *testing.T) { + ms := NewResourceProfiles() + assert.Equal(t, "", ms.SchemaUrl()) + ms.SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") + assert.Equal(t, "https://opentelemetry.io/schemas/1.5.0", ms.SchemaUrl()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + newResourceProfiles(&otlpprofiles.ResourceProfiles{}, &sharedState).SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") + }) +} + +func TestResourceProfiles_ScopeProfiles(t *testing.T) { + ms := NewResourceProfiles() + assert.Equal(t, NewScopeProfilesSlice(), ms.ScopeProfiles()) + fillTestScopeProfilesSlice(ms.ScopeProfiles()) + assert.Equal(t, generateTestScopeProfilesSlice(), ms.ScopeProfiles()) +} + +func generateTestResourceProfiles() ResourceProfiles { + tv := NewResourceProfiles() + fillTestResourceProfiles(tv) + return tv +} + +func fillTestResourceProfiles(tv ResourceProfiles) { + internal.FillTestResource(internal.NewResource(&tv.orig.Resource, tv.state)) + tv.orig.SchemaUrl = "https://opentelemetry.io/schemas/1.5.0" + fillTestScopeProfilesSlice(newScopeProfilesSlice(&tv.orig.ScopeProfiles, tv.state)) +} diff --git a/pdata/pprofile/generated_resourceprofilesslice.go b/pdata/pprofile/generated_resourceprofilesslice.go new file mode 100644 index 00000000000..b8ea2a71ba1 --- /dev/null +++ b/pdata/pprofile/generated_resourceprofilesslice.go @@ -0,0 +1,152 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "sort" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// ResourceProfilesSlice logically represents a slice of ResourceProfiles. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewResourceProfilesSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ResourceProfilesSlice struct { + orig *[]*otlpprofiles.ResourceProfiles + state *internal.State +} + +func newResourceProfilesSlice(orig *[]*otlpprofiles.ResourceProfiles, state *internal.State) ResourceProfilesSlice { + return ResourceProfilesSlice{orig: orig, state: state} +} + +// NewResourceProfilesSlice creates a ResourceProfilesSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewResourceProfilesSlice() ResourceProfilesSlice { + orig := []*otlpprofiles.ResourceProfiles(nil) + state := internal.StateMutable + return newResourceProfilesSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewResourceProfilesSlice()". +func (es ResourceProfilesSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es ResourceProfilesSlice) At(i int) ResourceProfiles { + return newResourceProfiles((*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new ResourceProfilesSlice can be initialized: +// +// es := NewResourceProfilesSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es ResourceProfilesSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]*otlpprofiles.ResourceProfiles, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty ResourceProfiles. +// It returns the newly added ResourceProfiles. +func (es ResourceProfilesSlice) AppendEmpty() ResourceProfiles { + es.state.AssertMutable() + *es.orig = append(*es.orig, &otlpprofiles.ResourceProfiles{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es ResourceProfilesSlice) MoveAndAppendTo(dest ResourceProfilesSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es ResourceProfilesSlice) RemoveIf(f func(ResourceProfiles) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es ResourceProfilesSlice) CopyTo(dest ResourceProfilesSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + for i := range *es.orig { + newResourceProfiles((*es.orig)[i], es.state).CopyTo(newResourceProfiles((*dest.orig)[i], dest.state)) + } + return + } + origs := make([]otlpprofiles.ResourceProfiles, srcLen) + wrappers := make([]*otlpprofiles.ResourceProfiles, srcLen) + for i := range *es.orig { + wrappers[i] = &origs[i] + newResourceProfiles((*es.orig)[i], es.state).CopyTo(newResourceProfiles(wrappers[i], dest.state)) + } + *dest.orig = wrappers +} + +// Sort sorts the ResourceProfiles elements within ResourceProfilesSlice given the +// provided less function so that two instances of ResourceProfilesSlice +// can be compared. +func (es ResourceProfilesSlice) Sort(less func(a, b ResourceProfiles) bool) { + es.state.AssertMutable() + sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) }) +} diff --git a/pdata/pprofile/generated_resourceprofilesslice_test.go b/pdata/pprofile/generated_resourceprofilesslice_test.go new file mode 100644 index 00000000000..3d319fc8b16 --- /dev/null +++ b/pdata/pprofile/generated_resourceprofilesslice_test.go @@ -0,0 +1,154 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + "unsafe" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestResourceProfilesSlice(t *testing.T) { + es := NewResourceProfilesSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newResourceProfilesSlice(&[]*otlpprofiles.ResourceProfiles{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewResourceProfiles() + testVal := generateTestResourceProfiles() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestResourceProfiles(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestResourceProfilesSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newResourceProfilesSlice(&[]*otlpprofiles.ResourceProfiles{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewResourceProfilesSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestResourceProfilesSlice_CopyTo(t *testing.T) { + dest := NewResourceProfilesSlice() + // Test CopyTo to empty + NewResourceProfilesSlice().CopyTo(dest) + assert.Equal(t, NewResourceProfilesSlice(), dest) + + // Test CopyTo larger slice + generateTestResourceProfilesSlice().CopyTo(dest) + assert.Equal(t, generateTestResourceProfilesSlice(), dest) + + // Test CopyTo same size slice + generateTestResourceProfilesSlice().CopyTo(dest) + assert.Equal(t, generateTestResourceProfilesSlice(), dest) +} + +func TestResourceProfilesSlice_EnsureCapacity(t *testing.T) { + es := generateTestResourceProfilesSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestResourceProfilesSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestResourceProfilesSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestResourceProfilesSlice(), es) +} + +func TestResourceProfilesSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestResourceProfilesSlice() + dest := NewResourceProfilesSlice() + src := generateTestResourceProfilesSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestResourceProfilesSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestResourceProfilesSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestResourceProfilesSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestResourceProfilesSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewResourceProfilesSlice() + emptySlice.RemoveIf(func(el ResourceProfiles) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestResourceProfilesSlice() + pos := 0 + filtered.RemoveIf(func(el ResourceProfiles) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func TestResourceProfilesSlice_Sort(t *testing.T) { + es := generateTestResourceProfilesSlice() + es.Sort(func(a, b ResourceProfiles) bool { + return uintptr(unsafe.Pointer(a.orig)) < uintptr(unsafe.Pointer(b.orig)) + }) + for i := 1; i < es.Len(); i++ { + assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) < uintptr(unsafe.Pointer(es.At(i).orig))) + } + es.Sort(func(a, b ResourceProfiles) bool { + return uintptr(unsafe.Pointer(a.orig)) > uintptr(unsafe.Pointer(b.orig)) + }) + for i := 1; i < es.Len(); i++ { + assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) > uintptr(unsafe.Pointer(es.At(i).orig))) + } +} + +func generateTestResourceProfilesSlice() ResourceProfilesSlice { + es := NewResourceProfilesSlice() + fillTestResourceProfilesSlice(es) + return es +} + +func fillTestResourceProfilesSlice(es ResourceProfilesSlice) { + *es.orig = make([]*otlpprofiles.ResourceProfiles, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = &otlpprofiles.ResourceProfiles{} + fillTestResourceProfiles(newResourceProfiles((*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_sample.go b/pdata/pprofile/generated_sample.go new file mode 100644 index 00000000000..3627b60f7e2 --- /dev/null +++ b/pdata/pprofile/generated_sample.go @@ -0,0 +1,130 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// Sample represents each record value encountered within a profiled program. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewSample function to create new instances. +// Important: zero-initialized instance is not valid for use. +type Sample struct { + orig *otlpprofiles.Sample + state *internal.State +} + +func newSample(orig *otlpprofiles.Sample, state *internal.State) Sample { + return Sample{orig: orig, state: state} +} + +// NewSample creates a new empty Sample. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewSample() Sample { + state := internal.StateMutable + return newSample(&otlpprofiles.Sample{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms Sample) MoveTo(dest Sample) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.Sample{} +} + +// LocationIndex returns the LocationIndex associated with this Sample. +func (ms Sample) LocationIndex() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.orig.LocationIndex, ms.state)) +} + +// LocationsStartIndex returns the locationsstartindex associated with this Sample. +func (ms Sample) LocationsStartIndex() uint64 { + return ms.orig.LocationsStartIndex +} + +// SetLocationsStartIndex replaces the locationsstartindex associated with this Sample. +func (ms Sample) SetLocationsStartIndex(v uint64) { + ms.state.AssertMutable() + ms.orig.LocationsStartIndex = v +} + +// LocationsLength returns the locationslength associated with this Sample. +func (ms Sample) LocationsLength() uint64 { + return ms.orig.LocationsLength +} + +// SetLocationsLength replaces the locationslength associated with this Sample. +func (ms Sample) SetLocationsLength(v uint64) { + ms.state.AssertMutable() + ms.orig.LocationsLength = v +} + +// StacktraceIdIndex returns the stacktraceidindex associated with this Sample. +func (ms Sample) StacktraceIdIndex() uint32 { + return ms.orig.StacktraceIdIndex +} + +// SetStacktraceIdIndex replaces the stacktraceidindex associated with this Sample. +func (ms Sample) SetStacktraceIdIndex(v uint32) { + ms.state.AssertMutable() + ms.orig.StacktraceIdIndex = v +} + +// Value returns the Value associated with this Sample. +func (ms Sample) Value() pcommon.Int64Slice { + return pcommon.Int64Slice(internal.NewInt64Slice(&ms.orig.Value, ms.state)) +} + +// Label returns the Label associated with this Sample. +func (ms Sample) Label() LabelSlice { + return newLabelSlice(&ms.orig.Label, ms.state) +} + +// Attributes returns the Attributes associated with this Sample. +func (ms Sample) Attributes() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.orig.Attributes, ms.state)) +} + +// Link returns the link associated with this Sample. +func (ms Sample) Link() uint64 { + return ms.orig.Link +} + +// SetLink replaces the link associated with this Sample. +func (ms Sample) SetLink(v uint64) { + ms.state.AssertMutable() + ms.orig.Link = v +} + +// TimestampsUnixNano returns the TimestampsUnixNano associated with this Sample. +func (ms Sample) TimestampsUnixNano() pcommon.UInt64Slice { + return pcommon.UInt64Slice(internal.NewUInt64Slice(&ms.orig.TimestampsUnixNano, ms.state)) +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms Sample) CopyTo(dest Sample) { + dest.state.AssertMutable() + ms.LocationIndex().CopyTo(dest.LocationIndex()) + dest.SetLocationsStartIndex(ms.LocationsStartIndex()) + dest.SetLocationsLength(ms.LocationsLength()) + dest.SetStacktraceIdIndex(ms.StacktraceIdIndex()) + ms.Value().CopyTo(dest.Value()) + ms.Label().CopyTo(dest.Label()) + ms.Attributes().CopyTo(dest.Attributes()) + dest.SetLink(ms.Link()) + ms.TimestampsUnixNano().CopyTo(dest.TimestampsUnixNano()) +} diff --git a/pdata/pprofile/generated_sample_test.go b/pdata/pprofile/generated_sample_test.go new file mode 100644 index 00000000000..bebc54b18ea --- /dev/null +++ b/pdata/pprofile/generated_sample_test.go @@ -0,0 +1,129 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestSample_MoveTo(t *testing.T) { + ms := generateTestSample() + dest := NewSample() + ms.MoveTo(dest) + assert.Equal(t, NewSample(), ms) + assert.Equal(t, generateTestSample(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newSample(&otlpprofiles.Sample{}, &sharedState)) }) + assert.Panics(t, func() { newSample(&otlpprofiles.Sample{}, &sharedState).MoveTo(dest) }) +} + +func TestSample_CopyTo(t *testing.T) { + ms := NewSample() + orig := NewSample() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestSample() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newSample(&otlpprofiles.Sample{}, &sharedState)) }) +} + +func TestSample_LocationIndex(t *testing.T) { + ms := NewSample() + assert.Equal(t, pcommon.NewUInt64Slice(), ms.LocationIndex()) + internal.FillTestUInt64Slice(internal.UInt64Slice(ms.LocationIndex())) + assert.Equal(t, pcommon.UInt64Slice(internal.GenerateTestUInt64Slice()), ms.LocationIndex()) +} + +func TestSample_LocationsStartIndex(t *testing.T) { + ms := NewSample() + assert.Equal(t, uint64(0), ms.LocationsStartIndex()) + ms.SetLocationsStartIndex(uint64(1)) + assert.Equal(t, uint64(1), ms.LocationsStartIndex()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newSample(&otlpprofiles.Sample{}, &sharedState).SetLocationsStartIndex(uint64(1)) }) +} + +func TestSample_LocationsLength(t *testing.T) { + ms := NewSample() + assert.Equal(t, uint64(0), ms.LocationsLength()) + ms.SetLocationsLength(uint64(1)) + assert.Equal(t, uint64(1), ms.LocationsLength()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newSample(&otlpprofiles.Sample{}, &sharedState).SetLocationsLength(uint64(1)) }) +} + +func TestSample_StacktraceIdIndex(t *testing.T) { + ms := NewSample() + assert.Equal(t, uint32(0), ms.StacktraceIdIndex()) + ms.SetStacktraceIdIndex(uint32(1)) + assert.Equal(t, uint32(1), ms.StacktraceIdIndex()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newSample(&otlpprofiles.Sample{}, &sharedState).SetStacktraceIdIndex(uint32(1)) }) +} + +func TestSample_Value(t *testing.T) { + ms := NewSample() + assert.Equal(t, pcommon.NewInt64Slice(), ms.Value()) + internal.FillTestInt64Slice(internal.Int64Slice(ms.Value())) + assert.Equal(t, pcommon.Int64Slice(internal.GenerateTestInt64Slice()), ms.Value()) +} + +func TestSample_Label(t *testing.T) { + ms := NewSample() + assert.Equal(t, NewLabelSlice(), ms.Label()) + fillTestLabelSlice(ms.Label()) + assert.Equal(t, generateTestLabelSlice(), ms.Label()) +} + +func TestSample_Attributes(t *testing.T) { + ms := NewSample() + assert.Equal(t, pcommon.NewUInt64Slice(), ms.Attributes()) + internal.FillTestUInt64Slice(internal.UInt64Slice(ms.Attributes())) + assert.Equal(t, pcommon.UInt64Slice(internal.GenerateTestUInt64Slice()), ms.Attributes()) +} + +func TestSample_Link(t *testing.T) { + ms := NewSample() + assert.Equal(t, uint64(0), ms.Link()) + ms.SetLink(uint64(1)) + assert.Equal(t, uint64(1), ms.Link()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newSample(&otlpprofiles.Sample{}, &sharedState).SetLink(uint64(1)) }) +} + +func TestSample_TimestampsUnixNano(t *testing.T) { + ms := NewSample() + assert.Equal(t, pcommon.NewUInt64Slice(), ms.TimestampsUnixNano()) + internal.FillTestUInt64Slice(internal.UInt64Slice(ms.TimestampsUnixNano())) + assert.Equal(t, pcommon.UInt64Slice(internal.GenerateTestUInt64Slice()), ms.TimestampsUnixNano()) +} + +func generateTestSample() Sample { + tv := NewSample() + fillTestSample(tv) + return tv +} + +func fillTestSample(tv Sample) { + internal.FillTestUInt64Slice(internal.NewUInt64Slice(&tv.orig.LocationIndex, tv.state)) + tv.orig.LocationsStartIndex = uint64(1) + tv.orig.LocationsLength = uint64(1) + tv.orig.StacktraceIdIndex = uint32(1) + internal.FillTestInt64Slice(internal.NewInt64Slice(&tv.orig.Value, tv.state)) + fillTestLabelSlice(newLabelSlice(&tv.orig.Label, tv.state)) + internal.FillTestUInt64Slice(internal.NewUInt64Slice(&tv.orig.Attributes, tv.state)) + tv.orig.Link = uint64(1) + internal.FillTestUInt64Slice(internal.NewUInt64Slice(&tv.orig.TimestampsUnixNano, tv.state)) +} diff --git a/pdata/pprofile/generated_sampleslice.go b/pdata/pprofile/generated_sampleslice.go new file mode 100644 index 00000000000..da2a128cd6f --- /dev/null +++ b/pdata/pprofile/generated_sampleslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// SampleSlice logically represents a slice of Sample. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewSampleSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type SampleSlice struct { + orig *[]otlpprofiles.Sample + state *internal.State +} + +func newSampleSlice(orig *[]otlpprofiles.Sample, state *internal.State) SampleSlice { + return SampleSlice{orig: orig, state: state} +} + +// NewSampleSlice creates a SampleSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewSampleSlice() SampleSlice { + orig := []otlpprofiles.Sample(nil) + state := internal.StateMutable + return newSampleSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewSampleSlice()". +func (es SampleSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es SampleSlice) At(i int) Sample { + return newSample(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new SampleSlice can be initialized: +// +// es := NewSampleSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es SampleSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.Sample, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty Sample. +// It returns the newly added Sample. +func (es SampleSlice) AppendEmpty() Sample { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.Sample{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es SampleSlice) MoveAndAppendTo(dest SampleSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es SampleSlice) RemoveIf(f func(Sample) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es SampleSlice) CopyTo(dest SampleSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.Sample, srcLen) + } + for i := range *es.orig { + newSample(&(*es.orig)[i], es.state).CopyTo(newSample(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_sampleslice_test.go b/pdata/pprofile/generated_sampleslice_test.go new file mode 100644 index 00000000000..7533e51cc45 --- /dev/null +++ b/pdata/pprofile/generated_sampleslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestSampleSlice(t *testing.T) { + es := NewSampleSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newSampleSlice(&[]otlpprofiles.Sample{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewSample() + testVal := generateTestSample() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestSample(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestSampleSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newSampleSlice(&[]otlpprofiles.Sample{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewSampleSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestSampleSlice_CopyTo(t *testing.T) { + dest := NewSampleSlice() + // Test CopyTo to empty + NewSampleSlice().CopyTo(dest) + assert.Equal(t, NewSampleSlice(), dest) + + // Test CopyTo larger slice + generateTestSampleSlice().CopyTo(dest) + assert.Equal(t, generateTestSampleSlice(), dest) + + // Test CopyTo same size slice + generateTestSampleSlice().CopyTo(dest) + assert.Equal(t, generateTestSampleSlice(), dest) +} + +func TestSampleSlice_EnsureCapacity(t *testing.T) { + es := generateTestSampleSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestSampleSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestSampleSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestSampleSlice(), es) +} + +func TestSampleSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestSampleSlice() + dest := NewSampleSlice() + src := generateTestSampleSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestSampleSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestSampleSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestSampleSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestSampleSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewSampleSlice() + emptySlice.RemoveIf(func(el Sample) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestSampleSlice() + pos := 0 + filtered.RemoveIf(func(el Sample) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestSampleSlice() SampleSlice { + es := NewSampleSlice() + fillTestSampleSlice(es) + return es +} + +func fillTestSampleSlice(es SampleSlice) { + *es.orig = make([]otlpprofiles.Sample, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.Sample{} + fillTestSample(newSample(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_scopeprofiles.go b/pdata/pprofile/generated_scopeprofiles.go new file mode 100644 index 00000000000..e213b2660ba --- /dev/null +++ b/pdata/pprofile/generated_scopeprofiles.go @@ -0,0 +1,76 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +// ScopeProfiles is a collection of profiles from a LibraryInstrumentation. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewScopeProfiles function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ScopeProfiles struct { + orig *otlpprofiles.ScopeProfiles + state *internal.State +} + +func newScopeProfiles(orig *otlpprofiles.ScopeProfiles, state *internal.State) ScopeProfiles { + return ScopeProfiles{orig: orig, state: state} +} + +// NewScopeProfiles creates a new empty ScopeProfiles. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewScopeProfiles() ScopeProfiles { + state := internal.StateMutable + return newScopeProfiles(&otlpprofiles.ScopeProfiles{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms ScopeProfiles) MoveTo(dest ScopeProfiles) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.ScopeProfiles{} +} + +// Scope returns the scope associated with this ScopeProfiles. +func (ms ScopeProfiles) Scope() pcommon.InstrumentationScope { + return pcommon.InstrumentationScope(internal.NewInstrumentationScope(&ms.orig.Scope, ms.state)) +} + +// SchemaUrl returns the schemaurl associated with this ScopeProfiles. +func (ms ScopeProfiles) SchemaUrl() string { + return ms.orig.SchemaUrl +} + +// SetSchemaUrl replaces the schemaurl associated with this ScopeProfiles. +func (ms ScopeProfiles) SetSchemaUrl(v string) { + ms.state.AssertMutable() + ms.orig.SchemaUrl = v +} + +// Profiles returns the Profiles associated with this ScopeProfiles. +func (ms ScopeProfiles) Profiles() ProfilesContainersSlice { + return newProfilesContainersSlice(&ms.orig.Profiles, ms.state) +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms ScopeProfiles) CopyTo(dest ScopeProfiles) { + dest.state.AssertMutable() + ms.Scope().CopyTo(dest.Scope()) + dest.SetSchemaUrl(ms.SchemaUrl()) + ms.Profiles().CopyTo(dest.Profiles()) +} diff --git a/pdata/pprofile/generated_scopeprofiles_test.go b/pdata/pprofile/generated_scopeprofiles_test.go new file mode 100644 index 00000000000..42fa23907f6 --- /dev/null +++ b/pdata/pprofile/generated_scopeprofiles_test.go @@ -0,0 +1,76 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestScopeProfiles_MoveTo(t *testing.T) { + ms := generateTestScopeProfiles() + dest := NewScopeProfiles() + ms.MoveTo(dest) + assert.Equal(t, NewScopeProfiles(), ms) + assert.Equal(t, generateTestScopeProfiles(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newScopeProfiles(&otlpprofiles.ScopeProfiles{}, &sharedState)) }) + assert.Panics(t, func() { newScopeProfiles(&otlpprofiles.ScopeProfiles{}, &sharedState).MoveTo(dest) }) +} + +func TestScopeProfiles_CopyTo(t *testing.T) { + ms := NewScopeProfiles() + orig := NewScopeProfiles() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestScopeProfiles() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newScopeProfiles(&otlpprofiles.ScopeProfiles{}, &sharedState)) }) +} + +func TestScopeProfiles_Scope(t *testing.T) { + ms := NewScopeProfiles() + internal.FillTestInstrumentationScope(internal.InstrumentationScope(ms.Scope())) + assert.Equal(t, pcommon.InstrumentationScope(internal.GenerateTestInstrumentationScope()), ms.Scope()) +} + +func TestScopeProfiles_SchemaUrl(t *testing.T) { + ms := NewScopeProfiles() + assert.Equal(t, "", ms.SchemaUrl()) + ms.SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") + assert.Equal(t, "https://opentelemetry.io/schemas/1.5.0", ms.SchemaUrl()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + newScopeProfiles(&otlpprofiles.ScopeProfiles{}, &sharedState).SetSchemaUrl("https://opentelemetry.io/schemas/1.5.0") + }) +} + +func TestScopeProfiles_Profiles(t *testing.T) { + ms := NewScopeProfiles() + assert.Equal(t, NewProfilesContainersSlice(), ms.Profiles()) + fillTestProfilesContainersSlice(ms.Profiles()) + assert.Equal(t, generateTestProfilesContainersSlice(), ms.Profiles()) +} + +func generateTestScopeProfiles() ScopeProfiles { + tv := NewScopeProfiles() + fillTestScopeProfiles(tv) + return tv +} + +func fillTestScopeProfiles(tv ScopeProfiles) { + internal.FillTestInstrumentationScope(internal.NewInstrumentationScope(&tv.orig.Scope, tv.state)) + tv.orig.SchemaUrl = "https://opentelemetry.io/schemas/1.5.0" + fillTestProfilesContainersSlice(newProfilesContainersSlice(&tv.orig.Profiles, tv.state)) +} diff --git a/pdata/pprofile/generated_scopeprofilesslice.go b/pdata/pprofile/generated_scopeprofilesslice.go new file mode 100644 index 00000000000..766ee38310b --- /dev/null +++ b/pdata/pprofile/generated_scopeprofilesslice.go @@ -0,0 +1,152 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "sort" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// ScopeProfilesSlice logically represents a slice of ScopeProfiles. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewScopeProfilesSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ScopeProfilesSlice struct { + orig *[]*otlpprofiles.ScopeProfiles + state *internal.State +} + +func newScopeProfilesSlice(orig *[]*otlpprofiles.ScopeProfiles, state *internal.State) ScopeProfilesSlice { + return ScopeProfilesSlice{orig: orig, state: state} +} + +// NewScopeProfilesSlice creates a ScopeProfilesSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewScopeProfilesSlice() ScopeProfilesSlice { + orig := []*otlpprofiles.ScopeProfiles(nil) + state := internal.StateMutable + return newScopeProfilesSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewScopeProfilesSlice()". +func (es ScopeProfilesSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es ScopeProfilesSlice) At(i int) ScopeProfiles { + return newScopeProfiles((*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new ScopeProfilesSlice can be initialized: +// +// es := NewScopeProfilesSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es ScopeProfilesSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]*otlpprofiles.ScopeProfiles, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty ScopeProfiles. +// It returns the newly added ScopeProfiles. +func (es ScopeProfilesSlice) AppendEmpty() ScopeProfiles { + es.state.AssertMutable() + *es.orig = append(*es.orig, &otlpprofiles.ScopeProfiles{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es ScopeProfilesSlice) MoveAndAppendTo(dest ScopeProfilesSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es ScopeProfilesSlice) RemoveIf(f func(ScopeProfiles) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es ScopeProfilesSlice) CopyTo(dest ScopeProfilesSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + for i := range *es.orig { + newScopeProfiles((*es.orig)[i], es.state).CopyTo(newScopeProfiles((*dest.orig)[i], dest.state)) + } + return + } + origs := make([]otlpprofiles.ScopeProfiles, srcLen) + wrappers := make([]*otlpprofiles.ScopeProfiles, srcLen) + for i := range *es.orig { + wrappers[i] = &origs[i] + newScopeProfiles((*es.orig)[i], es.state).CopyTo(newScopeProfiles(wrappers[i], dest.state)) + } + *dest.orig = wrappers +} + +// Sort sorts the ScopeProfiles elements within ScopeProfilesSlice given the +// provided less function so that two instances of ScopeProfilesSlice +// can be compared. +func (es ScopeProfilesSlice) Sort(less func(a, b ScopeProfiles) bool) { + es.state.AssertMutable() + sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) }) +} diff --git a/pdata/pprofile/generated_scopeprofilesslice_test.go b/pdata/pprofile/generated_scopeprofilesslice_test.go new file mode 100644 index 00000000000..831dafdec09 --- /dev/null +++ b/pdata/pprofile/generated_scopeprofilesslice_test.go @@ -0,0 +1,154 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + "unsafe" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestScopeProfilesSlice(t *testing.T) { + es := NewScopeProfilesSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newScopeProfilesSlice(&[]*otlpprofiles.ScopeProfiles{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewScopeProfiles() + testVal := generateTestScopeProfiles() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestScopeProfiles(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestScopeProfilesSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newScopeProfilesSlice(&[]*otlpprofiles.ScopeProfiles{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewScopeProfilesSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestScopeProfilesSlice_CopyTo(t *testing.T) { + dest := NewScopeProfilesSlice() + // Test CopyTo to empty + NewScopeProfilesSlice().CopyTo(dest) + assert.Equal(t, NewScopeProfilesSlice(), dest) + + // Test CopyTo larger slice + generateTestScopeProfilesSlice().CopyTo(dest) + assert.Equal(t, generateTestScopeProfilesSlice(), dest) + + // Test CopyTo same size slice + generateTestScopeProfilesSlice().CopyTo(dest) + assert.Equal(t, generateTestScopeProfilesSlice(), dest) +} + +func TestScopeProfilesSlice_EnsureCapacity(t *testing.T) { + es := generateTestScopeProfilesSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestScopeProfilesSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestScopeProfilesSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestScopeProfilesSlice(), es) +} + +func TestScopeProfilesSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestScopeProfilesSlice() + dest := NewScopeProfilesSlice() + src := generateTestScopeProfilesSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestScopeProfilesSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestScopeProfilesSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestScopeProfilesSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestScopeProfilesSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewScopeProfilesSlice() + emptySlice.RemoveIf(func(el ScopeProfiles) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestScopeProfilesSlice() + pos := 0 + filtered.RemoveIf(func(el ScopeProfiles) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func TestScopeProfilesSlice_Sort(t *testing.T) { + es := generateTestScopeProfilesSlice() + es.Sort(func(a, b ScopeProfiles) bool { + return uintptr(unsafe.Pointer(a.orig)) < uintptr(unsafe.Pointer(b.orig)) + }) + for i := 1; i < es.Len(); i++ { + assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) < uintptr(unsafe.Pointer(es.At(i).orig))) + } + es.Sort(func(a, b ScopeProfiles) bool { + return uintptr(unsafe.Pointer(a.orig)) > uintptr(unsafe.Pointer(b.orig)) + }) + for i := 1; i < es.Len(); i++ { + assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) > uintptr(unsafe.Pointer(es.At(i).orig))) + } +} + +func generateTestScopeProfilesSlice() ScopeProfilesSlice { + es := NewScopeProfilesSlice() + fillTestScopeProfilesSlice(es) + return es +} + +func fillTestScopeProfilesSlice(es ScopeProfilesSlice) { + *es.orig = make([]*otlpprofiles.ScopeProfiles, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = &otlpprofiles.ScopeProfiles{} + fillTestScopeProfiles(newScopeProfiles((*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/generated_valuetype.go b/pdata/pprofile/generated_valuetype.go new file mode 100644 index 00000000000..32280993a2d --- /dev/null +++ b/pdata/pprofile/generated_valuetype.go @@ -0,0 +1,87 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// ValueType describes the type and units of a value, with an optional aggregation temporality. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewValueType function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ValueType struct { + orig *otlpprofiles.ValueType + state *internal.State +} + +func newValueType(orig *otlpprofiles.ValueType, state *internal.State) ValueType { + return ValueType{orig: orig, state: state} +} + +// NewValueType creates a new empty ValueType. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewValueType() ValueType { + state := internal.StateMutable + return newValueType(&otlpprofiles.ValueType{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms ValueType) MoveTo(dest ValueType) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpprofiles.ValueType{} +} + +// Type returns the type associated with this ValueType. +func (ms ValueType) Type() int64 { + return ms.orig.Type +} + +// SetType replaces the type associated with this ValueType. +func (ms ValueType) SetType(v int64) { + ms.state.AssertMutable() + ms.orig.Type = v +} + +// Unit returns the unit associated with this ValueType. +func (ms ValueType) Unit() int64 { + return ms.orig.Unit +} + +// SetUnit replaces the unit associated with this ValueType. +func (ms ValueType) SetUnit(v int64) { + ms.state.AssertMutable() + ms.orig.Unit = v +} + +// AggregationTemporality returns the aggregationtemporality associated with this ValueType. +func (ms ValueType) AggregationTemporality() otlpprofiles.AggregationTemporality { + return ms.orig.AggregationTemporality +} + +// SetAggregationTemporality replaces the aggregationtemporality associated with this ValueType. +func (ms ValueType) SetAggregationTemporality(v otlpprofiles.AggregationTemporality) { + ms.state.AssertMutable() + ms.orig.AggregationTemporality = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms ValueType) CopyTo(dest ValueType) { + dest.state.AssertMutable() + dest.SetType(ms.Type()) + dest.SetUnit(ms.Unit()) + dest.SetAggregationTemporality(ms.AggregationTemporality()) +} diff --git a/pdata/pprofile/generated_valuetype_test.go b/pdata/pprofile/generated_valuetype_test.go new file mode 100644 index 00000000000..d2b557f2294 --- /dev/null +++ b/pdata/pprofile/generated_valuetype_test.go @@ -0,0 +1,80 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestValueType_MoveTo(t *testing.T) { + ms := generateTestValueType() + dest := NewValueType() + ms.MoveTo(dest) + assert.Equal(t, NewValueType(), ms) + assert.Equal(t, generateTestValueType(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.MoveTo(newValueType(&otlpprofiles.ValueType{}, &sharedState)) }) + assert.Panics(t, func() { newValueType(&otlpprofiles.ValueType{}, &sharedState).MoveTo(dest) }) +} + +func TestValueType_CopyTo(t *testing.T) { + ms := NewValueType() + orig := NewValueType() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestValueType() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { ms.CopyTo(newValueType(&otlpprofiles.ValueType{}, &sharedState)) }) +} + +func TestValueType_Type(t *testing.T) { + ms := NewValueType() + assert.Equal(t, int64(0), ms.Type()) + ms.SetType(int64(1)) + assert.Equal(t, int64(1), ms.Type()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newValueType(&otlpprofiles.ValueType{}, &sharedState).SetType(int64(1)) }) +} + +func TestValueType_Unit(t *testing.T) { + ms := NewValueType() + assert.Equal(t, int64(0), ms.Unit()) + ms.SetUnit(int64(1)) + assert.Equal(t, int64(1), ms.Unit()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { newValueType(&otlpprofiles.ValueType{}, &sharedState).SetUnit(int64(1)) }) +} + +func TestValueType_AggregationTemporality(t *testing.T) { + ms := NewValueType() + assert.Equal(t, otlpprofiles.AggregationTemporality(0), ms.AggregationTemporality()) + ms.SetAggregationTemporality(otlpprofiles.AggregationTemporality(1)) + assert.Equal(t, otlpprofiles.AggregationTemporality(1), ms.AggregationTemporality()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + newValueType(&otlpprofiles.ValueType{}, &sharedState).SetAggregationTemporality(otlpprofiles.AggregationTemporality(1)) + }) +} + +func generateTestValueType() ValueType { + tv := NewValueType() + fillTestValueType(tv) + return tv +} + +func fillTestValueType(tv ValueType) { + tv.orig.Type = int64(1) + tv.orig.Unit = int64(1) + tv.orig.AggregationTemporality = otlpprofiles.AggregationTemporality(1) +} diff --git a/pdata/pprofile/generated_valuetypeslice.go b/pdata/pprofile/generated_valuetypeslice.go new file mode 100644 index 00000000000..4049bbd2e16 --- /dev/null +++ b/pdata/pprofile/generated_valuetypeslice.go @@ -0,0 +1,136 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +// ValueTypeSlice logically represents a slice of ValueType. +// +// This is a reference type. If passed by value and callee modifies it, the +// caller will see the modification. +// +// Must use NewValueTypeSlice function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ValueTypeSlice struct { + orig *[]otlpprofiles.ValueType + state *internal.State +} + +func newValueTypeSlice(orig *[]otlpprofiles.ValueType, state *internal.State) ValueTypeSlice { + return ValueTypeSlice{orig: orig, state: state} +} + +// NewValueTypeSlice creates a ValueTypeSlice with 0 elements. +// Can use "EnsureCapacity" to initialize with a given capacity. +func NewValueTypeSlice() ValueTypeSlice { + orig := []otlpprofiles.ValueType(nil) + state := internal.StateMutable + return newValueTypeSlice(&orig, &state) +} + +// Len returns the number of elements in the slice. +// +// Returns "0" for a newly instance created with "NewValueTypeSlice()". +func (es ValueTypeSlice) Len() int { + return len(*es.orig) +} + +// At returns the element at the given index. +// +// This function is used mostly for iterating over all the values in the slice: +// +// for i := 0; i < es.Len(); i++ { +// e := es.At(i) +// ... // Do something with the element +// } +func (es ValueTypeSlice) At(i int) ValueType { + return newValueType(&(*es.orig)[i], es.state) +} + +// EnsureCapacity is an operation that ensures the slice has at least the specified capacity. +// 1. If the newCap <= cap then no change in capacity. +// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap. +// +// Here is how a new ValueTypeSlice can be initialized: +// +// es := NewValueTypeSlice() +// es.EnsureCapacity(4) +// for i := 0; i < 4; i++ { +// e := es.AppendEmpty() +// // Here should set all the values for e. +// } +func (es ValueTypeSlice) EnsureCapacity(newCap int) { + es.state.AssertMutable() + oldCap := cap(*es.orig) + if newCap <= oldCap { + return + } + + newOrig := make([]otlpprofiles.ValueType, len(*es.orig), newCap) + copy(newOrig, *es.orig) + *es.orig = newOrig +} + +// AppendEmpty will append to the end of the slice an empty ValueType. +// It returns the newly added ValueType. +func (es ValueTypeSlice) AppendEmpty() ValueType { + es.state.AssertMutable() + *es.orig = append(*es.orig, otlpprofiles.ValueType{}) + return es.At(es.Len() - 1) +} + +// MoveAndAppendTo moves all elements from the current slice and appends them to the dest. +// The current slice will be cleared. +func (es ValueTypeSlice) MoveAndAppendTo(dest ValueTypeSlice) { + es.state.AssertMutable() + dest.state.AssertMutable() + if *dest.orig == nil { + // We can simply move the entire vector and avoid any allocations. + *dest.orig = *es.orig + } else { + *dest.orig = append(*dest.orig, *es.orig...) + } + *es.orig = nil +} + +// RemoveIf calls f sequentially for each element present in the slice. +// If f returns true, the element is removed from the slice. +func (es ValueTypeSlice) RemoveIf(f func(ValueType) bool) { + es.state.AssertMutable() + newLen := 0 + for i := 0; i < len(*es.orig); i++ { + if f(es.At(i)) { + continue + } + if newLen == i { + // Nothing to move, element is at the right place. + newLen++ + continue + } + (*es.orig)[newLen] = (*es.orig)[i] + newLen++ + } + *es.orig = (*es.orig)[:newLen] +} + +// CopyTo copies all elements from the current slice overriding the destination. +func (es ValueTypeSlice) CopyTo(dest ValueTypeSlice) { + dest.state.AssertMutable() + srcLen := es.Len() + destCap := cap(*dest.orig) + if srcLen <= destCap { + (*dest.orig) = (*dest.orig)[:srcLen:destCap] + } else { + (*dest.orig) = make([]otlpprofiles.ValueType, srcLen) + } + for i := range *es.orig { + newValueType(&(*es.orig)[i], es.state).CopyTo(newValueType(&(*dest.orig)[i], dest.state)) + } +} diff --git a/pdata/pprofile/generated_valuetypeslice_test.go b/pdata/pprofile/generated_valuetypeslice_test.go new file mode 100644 index 00000000000..ac4ab1d7dab --- /dev/null +++ b/pdata/pprofile/generated_valuetypeslice_test.go @@ -0,0 +1,137 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofile + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpprofiles "go.opentelemetry.io/collector/pdata/internal/data/protogen/profiles/v1experimental" +) + +func TestValueTypeSlice(t *testing.T) { + es := NewValueTypeSlice() + assert.Equal(t, 0, es.Len()) + state := internal.StateMutable + es = newValueTypeSlice(&[]otlpprofiles.ValueType{}, &state) + assert.Equal(t, 0, es.Len()) + + emptyVal := NewValueType() + testVal := generateTestValueType() + for i := 0; i < 7; i++ { + el := es.AppendEmpty() + assert.Equal(t, emptyVal, es.At(i)) + fillTestValueType(el) + assert.Equal(t, testVal, es.At(i)) + } + assert.Equal(t, 7, es.Len()) +} + +func TestValueTypeSliceReadOnly(t *testing.T) { + sharedState := internal.StateReadOnly + es := newValueTypeSlice(&[]otlpprofiles.ValueType{}, &sharedState) + assert.Equal(t, 0, es.Len()) + assert.Panics(t, func() { es.AppendEmpty() }) + assert.Panics(t, func() { es.EnsureCapacity(2) }) + es2 := NewValueTypeSlice() + es.CopyTo(es2) + assert.Panics(t, func() { es2.CopyTo(es) }) + assert.Panics(t, func() { es.MoveAndAppendTo(es2) }) + assert.Panics(t, func() { es2.MoveAndAppendTo(es) }) +} + +func TestValueTypeSlice_CopyTo(t *testing.T) { + dest := NewValueTypeSlice() + // Test CopyTo to empty + NewValueTypeSlice().CopyTo(dest) + assert.Equal(t, NewValueTypeSlice(), dest) + + // Test CopyTo larger slice + generateTestValueTypeSlice().CopyTo(dest) + assert.Equal(t, generateTestValueTypeSlice(), dest) + + // Test CopyTo same size slice + generateTestValueTypeSlice().CopyTo(dest) + assert.Equal(t, generateTestValueTypeSlice(), dest) +} + +func TestValueTypeSlice_EnsureCapacity(t *testing.T) { + es := generateTestValueTypeSlice() + + // Test ensure smaller capacity. + const ensureSmallLen = 4 + es.EnsureCapacity(ensureSmallLen) + assert.Less(t, ensureSmallLen, es.Len()) + assert.Equal(t, es.Len(), cap(*es.orig)) + assert.Equal(t, generateTestValueTypeSlice(), es) + + // Test ensure larger capacity + const ensureLargeLen = 9 + es.EnsureCapacity(ensureLargeLen) + assert.Less(t, generateTestValueTypeSlice().Len(), ensureLargeLen) + assert.Equal(t, ensureLargeLen, cap(*es.orig)) + assert.Equal(t, generateTestValueTypeSlice(), es) +} + +func TestValueTypeSlice_MoveAndAppendTo(t *testing.T) { + // Test MoveAndAppendTo to empty + expectedSlice := generateTestValueTypeSlice() + dest := NewValueTypeSlice() + src := generateTestValueTypeSlice() + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestValueTypeSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo empty slice + src.MoveAndAppendTo(dest) + assert.Equal(t, generateTestValueTypeSlice(), dest) + assert.Equal(t, 0, src.Len()) + assert.Equal(t, expectedSlice.Len(), dest.Len()) + + // Test MoveAndAppendTo not empty slice + generateTestValueTypeSlice().MoveAndAppendTo(dest) + assert.Equal(t, 2*expectedSlice.Len(), dest.Len()) + for i := 0; i < expectedSlice.Len(); i++ { + assert.Equal(t, expectedSlice.At(i), dest.At(i)) + assert.Equal(t, expectedSlice.At(i), dest.At(i+expectedSlice.Len())) + } +} + +func TestValueTypeSlice_RemoveIf(t *testing.T) { + // Test RemoveIf on empty slice + emptySlice := NewValueTypeSlice() + emptySlice.RemoveIf(func(el ValueType) bool { + t.Fail() + return false + }) + + // Test RemoveIf + filtered := generateTestValueTypeSlice() + pos := 0 + filtered.RemoveIf(func(el ValueType) bool { + pos++ + return pos%3 == 0 + }) + assert.Equal(t, 5, filtered.Len()) +} + +func generateTestValueTypeSlice() ValueTypeSlice { + es := NewValueTypeSlice() + fillTestValueTypeSlice(es) + return es +} + +func fillTestValueTypeSlice(es ValueTypeSlice) { + *es.orig = make([]otlpprofiles.ValueType, 7) + for i := 0; i < 7; i++ { + (*es.orig)[i] = otlpprofiles.ValueType{} + fillTestValueType(newValueType(&(*es.orig)[i], es.state)) + } +} diff --git a/pdata/pprofile/go.mod b/pdata/pprofile/go.mod new file mode 100644 index 00000000000..570b7439844 --- /dev/null +++ b/pdata/pprofile/go.mod @@ -0,0 +1,27 @@ +module go.opentelemetry.io/collector/pdata/pprofile + +go 1.21.0 + +toolchain go1.21.10 + +require ( + github.com/stretchr/testify v1.9.0 + go.opentelemetry.io/collector/pdata v1.8.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect + google.golang.org/grpc v1.64.0 // indirect + google.golang.org/protobuf v1.34.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace go.opentelemetry.io/collector/pdata => ../ diff --git a/pdata/pprofile/go.sum b/pdata/pprofile/go.sum new file mode 100644 index 00000000000..d4ff00537b4 --- /dev/null +++ b/pdata/pprofile/go.sum @@ -0,0 +1,67 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +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/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +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/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= +google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pdata/pprofile/pprofileotlp/generated_exportpartialsuccess.go b/pdata/pprofile/pprofileotlp/generated_exportpartialsuccess.go new file mode 100644 index 00000000000..2bc9e3dab81 --- /dev/null +++ b/pdata/pprofile/pprofileotlp/generated_exportpartialsuccess.go @@ -0,0 +1,75 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofileotlp + +import ( + "go.opentelemetry.io/collector/pdata/internal" + otlpcollectorprofile "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/profiles/v1experimental" +) + +// ExportPartialSuccess represents the details of a partially successful export request. +// +// This is a reference type, if passed by value and callee modifies it the +// caller will see the modification. +// +// Must use NewExportPartialSuccess function to create new instances. +// Important: zero-initialized instance is not valid for use. +type ExportPartialSuccess struct { + orig *otlpcollectorprofile.ExportProfilesPartialSuccess + state *internal.State +} + +func newExportPartialSuccess(orig *otlpcollectorprofile.ExportProfilesPartialSuccess, state *internal.State) ExportPartialSuccess { + return ExportPartialSuccess{orig: orig, state: state} +} + +// NewExportPartialSuccess creates a new empty ExportPartialSuccess. +// +// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, +// OR directly access the member if this is embedded in another struct. +func NewExportPartialSuccess() ExportPartialSuccess { + state := internal.StateMutable + return newExportPartialSuccess(&otlpcollectorprofile.ExportProfilesPartialSuccess{}, &state) +} + +// MoveTo moves all properties from the current struct overriding the destination and +// resetting the current instance to its zero value +func (ms ExportPartialSuccess) MoveTo(dest ExportPartialSuccess) { + ms.state.AssertMutable() + dest.state.AssertMutable() + *dest.orig = *ms.orig + *ms.orig = otlpcollectorprofile.ExportProfilesPartialSuccess{} +} + +// RejectedProfiles returns the rejectedprofiles associated with this ExportPartialSuccess. +func (ms ExportPartialSuccess) RejectedProfiles() int64 { + return ms.orig.RejectedProfiles +} + +// SetRejectedProfiles replaces the rejectedprofiles associated with this ExportPartialSuccess. +func (ms ExportPartialSuccess) SetRejectedProfiles(v int64) { + ms.state.AssertMutable() + ms.orig.RejectedProfiles = v +} + +// ErrorMessage returns the errormessage associated with this ExportPartialSuccess. +func (ms ExportPartialSuccess) ErrorMessage() string { + return ms.orig.ErrorMessage +} + +// SetErrorMessage replaces the errormessage associated with this ExportPartialSuccess. +func (ms ExportPartialSuccess) SetErrorMessage(v string) { + ms.state.AssertMutable() + ms.orig.ErrorMessage = v +} + +// CopyTo copies all properties from the current struct overriding the destination. +func (ms ExportPartialSuccess) CopyTo(dest ExportPartialSuccess) { + dest.state.AssertMutable() + dest.SetRejectedProfiles(ms.RejectedProfiles()) + dest.SetErrorMessage(ms.ErrorMessage()) +} diff --git a/pdata/pprofile/pprofileotlp/generated_exportpartialsuccess_test.go b/pdata/pprofile/pprofileotlp/generated_exportpartialsuccess_test.go new file mode 100644 index 00000000000..73eb11b3026 --- /dev/null +++ b/pdata/pprofile/pprofileotlp/generated_exportpartialsuccess_test.go @@ -0,0 +1,78 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// Code generated by "pdata/internal/cmd/pdatagen/main.go". DO NOT EDIT. +// To regenerate this file run "make genpdata". + +package pprofileotlp + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/internal" + otlpcollectorprofile "go.opentelemetry.io/collector/pdata/internal/data/protogen/collector/profiles/v1experimental" +) + +func TestExportPartialSuccess_MoveTo(t *testing.T) { + ms := generateTestExportPartialSuccess() + dest := NewExportPartialSuccess() + ms.MoveTo(dest) + assert.Equal(t, NewExportPartialSuccess(), ms) + assert.Equal(t, generateTestExportPartialSuccess(), dest) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + ms.MoveTo(newExportPartialSuccess(&otlpcollectorprofile.ExportProfilesPartialSuccess{}, &sharedState)) + }) + assert.Panics(t, func() { + newExportPartialSuccess(&otlpcollectorprofile.ExportProfilesPartialSuccess{}, &sharedState).MoveTo(dest) + }) +} + +func TestExportPartialSuccess_CopyTo(t *testing.T) { + ms := NewExportPartialSuccess() + orig := NewExportPartialSuccess() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + orig = generateTestExportPartialSuccess() + orig.CopyTo(ms) + assert.Equal(t, orig, ms) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + ms.CopyTo(newExportPartialSuccess(&otlpcollectorprofile.ExportProfilesPartialSuccess{}, &sharedState)) + }) +} + +func TestExportPartialSuccess_RejectedProfiles(t *testing.T) { + ms := NewExportPartialSuccess() + assert.Equal(t, int64(0), ms.RejectedProfiles()) + ms.SetRejectedProfiles(int64(13)) + assert.Equal(t, int64(13), ms.RejectedProfiles()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + newExportPartialSuccess(&otlpcollectorprofile.ExportProfilesPartialSuccess{}, &sharedState).SetRejectedProfiles(int64(13)) + }) +} + +func TestExportPartialSuccess_ErrorMessage(t *testing.T) { + ms := NewExportPartialSuccess() + assert.Equal(t, "", ms.ErrorMessage()) + ms.SetErrorMessage("error message") + assert.Equal(t, "error message", ms.ErrorMessage()) + sharedState := internal.StateReadOnly + assert.Panics(t, func() { + newExportPartialSuccess(&otlpcollectorprofile.ExportProfilesPartialSuccess{}, &sharedState).SetErrorMessage("error message") + }) +} + +func generateTestExportPartialSuccess() ExportPartialSuccess { + tv := NewExportPartialSuccess() + fillTestExportPartialSuccess(tv) + return tv +} + +func fillTestExportPartialSuccess(tv ExportPartialSuccess) { + tv.orig.RejectedProfiles = int64(13) + tv.orig.ErrorMessage = "error message" +} diff --git a/versions.yaml b/versions.yaml index 16aea9585ee..53adcb6b4f8 100644 --- a/versions.yaml +++ b/versions.yaml @@ -46,6 +46,7 @@ module-sets: - go.opentelemetry.io/collector/extension/zpagesextension - go.opentelemetry.io/collector/extension/memorylimiterextension - go.opentelemetry.io/collector/otelcol + - go.opentelemetry.io/collector/pdata/pprofile - go.opentelemetry.io/collector/pdata/testdata - go.opentelemetry.io/collector/processor - go.opentelemetry.io/collector/processor/batchprocessor