opentelemetry-collector icon indicating copy to clipboard operation
opentelemetry-collector copied to clipboard

Allow users to create new datapoints using initializer

Open ridwanmsharif opened this issue 6 months ago • 2 comments
trafficstars

Component(s)

pdata

Describe the issue you're reporting

The New... methods mention that it should only be used in testing code: https://github.com/open-telemetry/opentelemetry-collector/blob/727ae96d621450fc5afd3a81b4669b48756a760d/pdata/internal/cmd/pdatagen/internal/templates/message.go.tmpl#L41-L46

However, I think its useful to loosen this language/restriction. See some examples of this used right now in the contrib collector (I would argue these are reasonable uses too):

  • https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/51bb586a2ded135ff23a8a28d2e920fc13d25b6a/processor/deltatocumulativeprocessor/processor.go#L82-L86
  • https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/51bb586a2ded135ff23a8a28d2e920fc13d25b6a/processor/metricstarttimeprocessor/internal/truereset/adjuster.go#L169
  • https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/905591168dc3730f7dedc1fc0d9286ab70ee8c01/exporter/collector/internal/normalization/standard_normalizer.go#L278 (library used int he googlecloudexporter and googlemanagedprometheusexporter)

ridwanmsharif avatar May 02 '25 04:05 ridwanmsharif

Pinging code owners:

  • pdata: @BogdanDrutu @dmitryax

See Adding Labels via Comments if you do not have permissions to add labels yourself.

github-actions[bot] avatar May 02 '25 04:05 github-actions[bot]

Beyond that, it's being applied to what are (afaict) the top-level struct creators like NewTrace and NewMetrics. Based on my limited understanding, these methods are the only valid way to make these structs.

hibachrach avatar Nov 04 '25 20:11 hibachrach

They are the only valid way to make an unattached struct. As the comment mention, you should use AppentEmpty() from the parent slice, or set values within the default object of the parent struct.

I have also seen some cases where doing it that way is a valid use within profiling (https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7a90970aace7bf308feb788973254388de624779/pkg/ottl/contexts/internal/ctxprofilecommon/attributes.go#L37). How about something like:

// Using this should outside of testing code must be avoided as much as possible. Users should use "AppendEmpty" // when part of a Slice OR directly access the member if this is embedded in another struct.

dmathieu avatar Nov 05 '25 09:11 dmathieu