Debian Codegraph @

pdata/README.md

raw
1 # Pipeline data (pdata)
2
3 <!-- status autogenerated section -->
4 | Status | |
5 | ------------- |-----------|
6 | Stability | [stable]: traces, metrics, logs |
7 | Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector?query=is%3Aissue%20is%3Aopen%20label%3Apkg%2Fpdata%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector/issues?q=is%3Aopen+is%3Aissue+label%3Apkg%2Fpdata) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector?query=is%3Aissue%20is%3Aclosed%20label%3Apkg%2Fpdata%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector/issues?q=is%3Aclosed+is%3Aissue+label%3Apkg%2Fpdata) |
8 | [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@bogdandrutu](https://www.github.com/bogdandrutu), [@dmitryax](https://www.github.com/dmitryax) |
9
10 [stable]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#stable
11 <!-- end autogenerated section -->
12
13 Pipeline data (pdata) implements data structures that represent telemetry data in-memory. All data received
14 is converted into this format, travels through the pipeline in this format, and is converted from this format by
15 exporters when sending.
16
17 Current implementation primarily uses OTLP protobuf structs as the underlying data structures for many of the
18 declared structs. We keep a pointer to OTLP protobuf in the "orig" member field. This allows efficient translation
19 to/from OTLP wire protocol. The underlying data structure is kept private so that we are free to make changes to it
20 in the future.
21
22 The pdata API is designed to avoid mutable data sharing and bugs that stem from that. Each pdata instance cannot
23 contain a reference to an object that is used in another pdata instance.
24
25 ## API naming convention
26
27 ### Package names
28
29 Names of pdata packages don't follow names of the protobuf packages. The pdata has a package per telemetry type
30 starting with `p`, e.g. `ptrace`, and `pcommon` package which includes pdata API for protobuf definitions from
31 `common` and `resource` protobuf packages.
32
33 ### Protobuf message representation in pdata
34
35 Pipeline data structs SHOULD be based on the names of the underlying OTLP protobuf messages. Data types for
36 protobuf messages defined as part of another message SHOULD include the owner's name as a prefix. The following
37 examples are two pdata structs based on protobuf messages defined at the package level and as part of another
38 message:
39
40 - `pmetric.NumberDataPoint` based on a `NumberDataPoint` protobuf message defined in
41 [metrics.proto](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/metrics/v1/metrics.proto)
42 package.
43 - `pmetric.ExponentialHistogramDataPointBuckets` based on a `Buckets` protobuf message defined in
44 `ExponentialHistogramDataPoint` message of
45 [metrics.proto](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/metrics/v1/metrics.proto)
46 package.
47
48 Exceptions to the naming rules are possible, but not encouraged. Another name can be chosen for brevity or if the
49 struct provides a different interface to manipulate the underlying data. The following exceptions are currently
50 accepted:
51
52 - `plog.Logs` based on `LogsData` protobuf message.
53 - `pmetric.Metrics` based on `MetricsData` protobuf message.
54 - `ptrace.Traces` based on `TracesData` protobuf message.
55 - `pcommon.Slice` based on `ArrayValue` protobuf message.
56 - `pcommon.Map` based on `KeyValueList` protobuf message.
57 - `pcommon.Value` based on `AnyValue` protobuf message.
58
59 Each pdata struct MUST have an initialization function starting with `New` prefix. Usage of zero-initialized values is
60 prohibited and can cause a panic. Each pdata struct MUST provide the following methods:
61
62 - `MoveTo(<type>)`: moves all the data from one struct instance to another. The destination instance is overwritten and the
63 source instance is re-initialized as a new empty instance.
64 - `CopyTo(<type>)`: deep copies all the data from one struct instance to another. The destination instance is
65 overwritten and the source instance is not modified.
66
67 Each pdata struct based on a protobuf message SHOULD have getter methods for every protobuf field. Exceptions to
68 this rule are allowed if exposing a field is not desirable in pdata. For example, a deprecated protobuf field MAY
69 not be exposed in pdata. Also, pdata MAY provide a different interface to manipulate protobuf fields without
70 exposing them explicitly as is done with `pcommon.Map`.
71
72 ### Protobuf fields representation in pdata
73
74 #### Singular fields
75
76 Name of a pdata getter methods representing singular protobuf fields SHOULD match the name the protobuf field but
77 written in CamelCase aligned with Go naming conventions. If a protobuf field is of scalar or enum type, the
78 corresponding pdata struct MUST provide a setter method with `Set` prefix.
79
80 Some fields of scalar type in a protobuf message MAY be represented by another pdata type providing an additional
81 interface, for example `pcommon.Timestamp` is another type that wraps `uint64` value and provides an additional
82 interface to work with timestamps. pdata fields returning `pcommon.Timestamp` don't follow the recommended naming
83 schema, `Timestamp` word is used instead of `TimeUnixNano` to represent protobuf fields that contain
84 `time_unix_nano`, for example `StartTimestamp` pdata field is used for `start_time_unix_nano` protobuf fields.
85
86 #### Optional fields
87
88 Each optional field in a protobuf message exposed in pdata MUST have the following additional methods:
89 - A method starting with `Has` to determine if the field is set. For example,
90 `func (ms HistogramDataPoint) HasMin() bool`.
91 - A method starting with `Remove` prefix to remove a value associated with the optional field. For example,
92 `func (ms HistogramDataPoint) RemoveMin()`.
93
94 #### OneOf fields
95
96 A pdata struct representing a protobuf message with an exposed oneof field MUST provide getter and setter methods for
97 each option of the oneof field. Name of each setter and getter SHOULD be called the same as the protobuf oneof field
98 options. The following exception is adopted in pdata for numeric oneof protobuf fields for brevity:
99
100 ```protobuf
101 oneof value {
102 double as_double = 4;
103 sfixed64 as_int = 6;
104 }
105 ```
106
107 is represented in pdata in a shorter form of the following methods:
108
109 ```golang
110 DoubleValue() float64
111 SetDoubleValue(float64)
112 IntValue() int64
113 SetIntValue(v int64)
114 ```
115
116 If a oneof field option is another protobuf message, the setter name MUST include `Empty` in its name. Such setter
117 MUST set the oneof field to an empty initialized struct and return it. The following conditions define whether the
118 name of the oneof field must be included in the setter and getter method names:
119
120 1. If a oneof field represents the whole protobuf message, name of the oneof field itself MAY be omitted in setter
121 and getter methods. For example:
122
123 ```go
124 func (ms Metric) Sum() Sum
125 func (ms Metric) SetEmptySum() Sum
126 ```
127
128 2. If a oneof field is relevant only to a particular field of the message, the pdata getter and setter methods MUST
129 include name of the oneof field along with name of the option. For example:
130
131 ```go
132 func (ms NumberDataPoint) IntValue() int64
133 func (ms NumberDataPoint) SetIntValue(v int64)
134 ```
135
136 Additionally, the pdata struct MUST provide a type getter for every exposed oneof protobuf field. Name of the getter
137 MUST be `Type` for oneof fields representing the whole protobuf message (1), or `<oneof field>Type` for oneof fields
138 relevant only to a particular field. The function MUST return an `int32` enum type called `<struct name>Type` for (1)
139 and `<struct name><oneof field>Type` for (2). For example:
140
141 - `func (ms Metric) Type() MetricType` (1)
142 - `func (ms NumberDataPoint) ValueType() NumberDataPointValueType` (2)
143
144 The enum constants MUST be called the same as the type with a suffix named after the oneof option, e.g.:
145
146 - `MetricTypeSum` (1)
147 - `NumberDataPointValueTypeString` (2)
148
149 An unset oneof protobuf value MUST be represented by a pdata constant with `Empty` suffix, e.g.:
150
151 - `MetricTypeEmpty` (1)
152 - `NumberDataPointValueTypeEmpty` (2)
153
154 The pdata enum type SHOULD have `String()` method returning a string value of the corresponding oneof field option.
155
156 #### Repeated protobuf fields
157
158 Each repeated protobuf message field exposed in pdata MUST be represented as another pdata struct that SHOULD be
159 called the same as the underlying protobuf field with `Slice` suffix. An exception example is `pdata.Map` that
160 provides a different interface to manipulate the underlying protobuf data.
161
162 #### Repeated scalar fields
163
164 Each repeated scalar protobuf field exposed in pdata MUST be represented as another pdata type wrapping a native go
165 slice. Name of the type SHOULD include name of the primitive data type ending with `Slice` suffix, e.g. `UInt64Slice`.
166
167 #### Enum fields
168
169 Each protobuf enum field exposed in pdata MUST have a type declared with underlying `int64` type. Name of the type
170 SHOULD follow the same rules as for struct type names representing protobuf messages:
171
172 - If a protobuf enum is defined on the package level, pdata type SHOULD have the same name.
173 - If a protobuf enum is defined as part of another message, pdata type SHOULD include the protobuf message name as a
174 prefix.
175
176 Constants defined for the pdata int64 type MUST have the same names and numeric values defined in protobuf. Names
177 of the constants must be translated from ALL_CAPS_SNAKE_CASE to CamelCase.
178
179 The pdata enum type SHOULD have `String()` method returning a string value for each constant.
180
181 Example of a protobuf enum definition in pdata:
182
183 ```golang
184 type AggregationTemporality int32
185 const (
186 AggregationTemporalityUnspecified = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED)
187 AggregationTemporalityDelta = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA)
188 AggregationTemporalityCumulative = AggregationTemporality(otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE)
189 )
190 ```
191
192 ### Flags
193
194 Flags fields are typically defined in protobuf as `uint32` fields representing 32 distinct boolean flags. The
195 exposed protobuf flags fields MUST be defined as new types with `uint32` underlying type and called according to
196 the same rules as struct names representing protobuf messages and other enum types. Each pdata flags type MUST have
197 an empty variable of the fields type with `Default` prefix. Each flag MUST have a getter method returning a
198 particular flag and a setter method with `With` prefix returning a new instance of the pdata flags type. Any instance
199 of the pdata type is immutable since it's just a wrapper over `uint32`.
200
201 The following pdata type is used for log record flags field:
202
203 ```golang
204 type LogRecordFlags uint32
205 var DefaultLogRecordFlags = LogRecordFlags(0)
206 func (ms LogRecordFlags) IsSampled() bool
207 func (ms LogRecordFlags) WithIsSampled(b bool) LogRecordFlags
208 ```
209
210 ### Examples
211
212 The following protobuf message:
213
214 ```protobuf
215 message NumberDataPoint {
216 reserved 1;
217 repeated opentelemetry.proto.common.v1.KeyValue attributes = 7;
218 fixed64 start_time_unix_nano = 2;
219 fixed64 time_unix_nano = 3;
220 oneof value {
221 double as_double = 4;
222 sfixed64 as_int = 6;
223 }
224 repeated Exemplar exemplars = 5;
225 uint32 flags = 8;
226 }
227 ```
228
229 is represented by the following pdata API
230
231 ```go
232 type NumberDataPoint
233 func NewNumberDataPoint() NumberDataPoint
234 func (ms NumberDataPoint) MoveTo(dest NumberDataPoint)
235 func (ms NumberDataPoint) CopyTo(dest NumberDataPoint)
236 func (ms NumberDataPoint) Attributes() pcommon.Map
237 func (ms NumberDataPoint) StartTimestamp() pcommon.Timestamp
238 func (ms NumberDataPoint) SetStartTimestamp(v pcommon.Timestamp)
239 func (ms NumberDataPoint) Timestamp() pcommon.Timestamp
240 func (ms NumberDataPoint) SetTimestamp(v pcommon.Timestamp)
241 func (ms NumberDataPoint) ValueType() NumberDataPointValueType
242 func (ms NumberDataPoint) DoubleValue() float64
243 func (ms NumberDataPoint) SetDoubleValue(v float64)
244 func (ms NumberDataPoint) IntValue() int64
245 func (ms NumberDataPoint) SetIntValue(v int64)
246 func (ms NumberDataPoint) Exemplars() ExemplarSlice
247 func (ms NumberDataPoint) Flags() DataPointFlags
248 func (ms NumberDataPoint) SetFlags(v DataPointFlags)
249 ```