-
Notifications
You must be signed in to change notification settings - Fork 994
Expand file tree
/
Copy pathsdk.md
More file actions
268 lines (203 loc) · 12.4 KB
/
Copy pathsdk.md
File metadata and controls
268 lines (203 loc) · 12.4 KB
Edit and raw actions
OlderNewer
1
<!--- Hugo front matter used to generate the website version of this page:
2
linkTitle: SDK
3
weight: 3
4
--->
5
6
# Resource SDK
7
8
**Status**: [Stable](../document-status.md) except where otherwise specified
9
10
A [Resource](../overview.md#resources) is an immutable representation of the
11
observed entity for which telemetry is being produced, expressed as
12
[Attributes](../common/README.md#attribute).
13
For example, a process running in a container on Kubernetes has a Pod name, it
14
is in a namespace and possibly is part of a Deployment which also has a name.
15
All three of these attributes can be included in the `Resource`. Note that there
16
are certain
17
[attributes](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md)
18
that have prescribed meanings.
19
20
The primary purpose of resources as a first-class concept in the SDK is
21
decoupling of discovery of resource information from exporters. This allows for
22
independent development and easy customization for users that need to integrate
23
with closed source environments. The SDK MUST allow for creation of `Resources` and
24
for associating them with telemetry.
25
26
When used with distributed tracing, a resource can be associated with the
27
[TracerProvider](../trace/api.md#tracerprovider) when the TracerProvider is created.
28
That association cannot be changed later.
29
When associated with a `TracerProvider`,
30
all `Span`s produced by any `Tracer` from the provider MUST be associated with this `Resource`.
31
32
Analogous to distributed tracing, when used with metrics,
33
a resource can be associated with a `MeterProvider`.
34
When associated with a [`MeterProvider`](../metrics/api.md#meterprovider),
35
all metrics produced by any `Meter` from the provider will be
36
associated with this `Resource`.
37
38
Similarly, when used with logs,
39
a resource can be associated with a `LoggerProvider`.
40
When associated with a [`LoggerProvider`](../logs/api.md#loggerprovider),
41
all log records produced by any `Logger` from the provider will be
42
associated with this `Resource`.
43
44
## SDK-provided resource attributes
45
46
The SDK MUST provide access to a Resource with at least the attributes listed at
47
[Semantic Attributes with SDK-provided Default Value](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/README.md#semantic-attributes-with-sdk-provided-default-value).
48
This resource MUST be associated with a `TracerProvider`, `MeterProvider`,
49
or `LoggerProvider` if another resource was not explicitly specified.
50
51
Note: This means that it is possible to create and associate a resource that
52
does not have all or any of the SDK-provided attributes present. However, that
53
does not happen by default. If a user wants to combine custom attributes with
54
the default resource, they can use [`Merge`](#merge) with their custom resource
55
or specify their attributes by implementing
56
[Custom resource detectors](#detecting-resource-information-from-the-environment)
57
instead of explicitly associating a resource.
58
59
## Resource creation
60
61
The SDK must support two ways to instantiate new resources. Those are:
62
63
### Create
64
65
The interface MUST provide a way to create a new resource.
66
Examples include a factory method or a constructor for a resource
67
object. A factory method is recommended to enable support for cached objects.
68
69
Parameters:
70
71
- [`Attributes`](../common/README.md#attribute)
72
- [since 1.4.0] `schema_url` (optional): Specifies the Schema URL that should be
73
recorded in the emitted resource. If the `schema_url` parameter is unspecified
74
then the created resource will have an empty Schema URL.
75
- **Status**: [Development](../document-status.md) since 1.60.0 - `Entities` (optional):
76
Specifies the [entities](data-model.md) that
77
should be recorded in the emitted resource. If the `entities` parameter is
78
unspecified then the created resource will have no entities.
79
80
When both `Entities` and `Attributes` are provided in the create method,
81
the system MUST behave as if a Resource is created with just `Attributes`
82
and then merges with another Resource created with just `Entities`.
83
84
### Merge
85
86
The interface MUST provide a way for an old resource and an
87
updating resource to be merged into a new resource.
88
89
Note: This is intended to be utilized for merging of resources whose attributes
90
come from different sources,
91
such as environment variables, or metadata extracted from the host or container.
92
93
Required parameters:
94
95
- the old resource
96
- the updating resource whose attributes take precedence
97
98
If either resource contains `Entities` then
99
[merge behavior with Entities](#merge-behavior-with-entities) MUST be used,
100
otherwise [merge behavior without Entities](#merge-behavior-without-entities)
101
MUST be used.
102
103
#### Merge behavior without Entities
104
105
The resulting resource MUST have all attributes that are on any of the two input resources.
106
If a key exists on both the old and updating resource, the value of the updating
107
resource MUST be picked (even if the updated value is empty).
108
109
The resulting resource will have the Schema URL calculated as follows:
110
111
- If the old resource's Schema URL is empty then the resulting resource's Schema
112
URL will be set to the Schema URL of the updating resource,
113
- Else if the updating resource's Schema URL is empty then the resulting
114
resource's Schema URL will be set to the Schema URL of the old resource,
115
- Else if the Schema URLs of the old and updating resources are the same then
116
that will be the Schema URL of the resulting resource,
117
- Else this is a merging error (this is the case when the Schema URL of the old
118
and updating resources are not empty and are different). The resulting resource is
119
undefined, and its contents are implementation-specific.
120
121
#### Merge behavior with entities
122
123
**Status**: [Development](../document-status.md)
124
125
When either Resource contains entities, the merge operation MUST follow the
126
[resource data model's merge algorithm](./data-model.md#merging-resources).
127
128
The resulting `SchemaURL` MUST match the behavior defined in the merge
129
algorithm.
130
131
> [!NOTE]
132
> `SchemaURL` on Resource is preserved as a backwards-compatibility measure.
133
> It is not used in entity-aware systems, where multiple `SchemaURL`s will
134
> apply to `Resource`.
135
136
### The empty resource
137
138
It is recommended, but not required, to provide a way to quickly create an empty
139
resource.
140
141
### Detecting resource information from the environment
142
143
Custom resource detectors related to generic platforms (e.g. Docker, Kubernetes)
144
or vendor specific environments (e.g. EKS, AKS, GKE) MUST be implemented as
145
packages separate from the SDK.
146
147
Resource detector packages MUST provide a method that returns a resource. This
148
can then be associated with `TracerProvider`, `MeterProvider`, or
149
`LoggerProvider` instances as described above.
150
151
Resource detector packages MAY detect resource information from multiple
152
possible sources and merge the result using the `Merge` operation described
153
above.
154
155
Resource detection logic is expected to complete quickly since this code will be
156
run during application initialization. Errors should be handled as specified in
157
the [Error Handling
158
principles](../error-handling.md#basic-error-handling-principles). Note the
159
failure to detect any resource information MUST NOT be considered an error,
160
whereas an error that occurs during an attempt to detect resource information
161
SHOULD be considered an error.
162
163
Resource detectors that populate resource attributes according to OpenTelemetry
164
semantic conventions MUST ensure that the resource has a Schema URL set to a
165
value that matches the semantic conventions. Empty Schema URL SHOULD be used if
166
the detector does not populate the resource with any known attributes that have
167
a semantic convention or if the detector does not know what attributes it will
168
populate (e.g. the detector that reads the attributes from environment values
169
will not know what Schema URL to use). If multiple detectors are combined and
170
the detectors use different non-empty Schema URL it MUST be an error since it is
171
impossible to merge such resources. The resulting resource is undefined, and its
172
contents are implementation specific.
173
174
#### Resource detector name
175
176
**Status**: [Development](../document-status.md)
177
178
Resource detectors SHOULD have a unique name for reference in configuration. For
179
example, users list and configure individual resource detectors by name
180
in [declarative configuration](../configuration/README.md#declarative-configuration).
181
Names SHOULD be [snake case](https://en.wikipedia.org/wiki/Snake_case) and
182
consist of lowercase alphanumeric and `_` characters, which ensures they conform
183
to declarative
184
configuration [property name requirements](https://github.com/open-telemetry/opentelemetry-configuration/blob/main/CONTRIBUTING.md#property-name-case).
185
186
Resource detector names SHOULD reflect
187
the [root namespace](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/naming.md#general-naming-considerations)
188
of attributes they populate. For example, a resource detector named `os`
189
populates `os.*` attributes. Resource detectors which populate attributes from
190
multiple root namespaces SHOULD choose a name which appropriately conveys their
191
purpose.
192
193
An SDK which identifies multiple resource detectors with the same name SHOULD
194
report an error. In order to limit collisions, resource detectors SHOULD
195
document their name in a manner which is easily discoverable. Authors of
196
resource detectors should check existing resource detectors to ensure their
197
target name isn't already in use. Additionally, the following detector names are
198
reserved for built-in resource detectors published with language SDKs:
199
200
* `container`:
201
Populates [container.*](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/container.md)
202
attributes.
203
* `host`:
204
Populates [host.*](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/host.md) and [os.*](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/os.md)
205
attributes.
206
* `process`:
207
Populates [process.*](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/process.md)
208
attributes.
209
* `service`: Populates `service.name` based
210
on [OTEL_SERVICE_NAME](../configuration/sdk-environment-variables.md#general-sdk-configuration)
211
environment variable; populates `service.instance.id`
212
as [defined here](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/service.md#service-attributes).
213
214
### Specifying resource information via an environment variable
215
216
The SDK MUST extract information from the `OTEL_RESOURCE_ATTRIBUTES` environment
217
variable and [merge](#merge) this, as the secondary resource, with any resource
218
information provided by the user, i.e. the user provided resource information
219
has higher priority.
220
221
The `OTEL_RESOURCE_ATTRIBUTES` environment variable will contain of a list of
222
key value pairs, represented as `key1=value1,key2=value2`.
223
All attribute values MUST be considered strings. The `,` and `=` characters
224
in keys and values MUST be percent encoded. Other characters MAY be
225
[percent-encoded](https://datatracker.ietf.org/doc/html/rfc3986#section-2.1),
226
e.g. values outside the ANSI characters set.
227
228
In case of any error, e.g. failure during the decoding process, the entire environment
229
variable value SHOULD be discarded and an error SHOULD be reported following the
230
[Error Handling principles](../error-handling.md#basic-error-handling-principles).
231
232
## Resource operations
233
234
Resources are immutable. Thus, in addition to resource creation,
235
only the following operations should be provided:
236
237
### Retrieve attributes
238
239
The SDK should provide a way to retrieve a read only collection of attributes
240
associated with a resource.
241
242
There is no need to guarantee the order of the attributes.
243
244
When entities are enabled and present for the Resource, this list MUST
245
include all attributes, including those associated with entities.
246
247
The most common operation when retrieving attributes is to enumerate over them. As
248
such, it is recommended to optimize the resulting collection for fast
249
enumeration over other considerations such as a way to quickly retrieve a value
250
for a attribute with a specific key.
251
252
### Retrieve entities
253
254
**Status**: [Development](../document-status.md)
255
256
The SDK SHOULD provide a way to retrieve the entities associated with a
257
resource.
258
259
There is no need to guarantee the order of entities.
260
261
### Retrieve unassociated attributes
262
263
**Status**: [Development](../document-status.md)
264
265
The SDK SHOULD provide a way to retrieve attributes which are NOT associated
266
with an entity in the resource.
267
268
There is no need to guarantee the order of attributes.