|
| 1 | +# Introduce Scope Attributes |
| 2 | + |
| 3 | +This OTEP adds attributes to the Scope of a telemetry emitter (e.g. Tracer, Meter, LogEmitter). |
| 4 | + |
| 5 | +## Motivation |
| 6 | + |
| 7 | +There are a few reasons why adding Scope attributes is a good idea: |
| 8 | + |
| 9 | +- There are 2 known use cases where Scope attributes can solve specific problems: |
| 10 | + - Add support for [Meter "short_name"](https://github.com/open-telemetry/opentelemetry-specification/pull/2422), |
| 11 | + represented as an attribute of Meter's Scope. |
| 12 | + - Add support for differentiating the type of data emitted from the scopes that belong |
| 13 | + to different data domains, e.g. profiling data emitted as log records or client-side |
| 14 | + data emitted as log records needs to be differentiated so that it can be easily |
| 15 | + routed and processed differently in the backends. We don't have a good way to handle |
| 16 | + this today. The type of the data can be recorded as an attribute Logger's Scope. |
| 17 | +- It makes Scope consistent with the other primary data types: Resource, Span, Metric, |
| 18 | + LogRecord. |
| 19 | + |
| 20 | +See additional [discussion here](https://github.com/open-telemetry/opentelemetry-specification/issues/2450). |
| 21 | + |
| 22 | +## Summary |
| 23 | + |
| 24 | +The following is the summary of proposed changes: |
| 25 | + |
| 26 | +- We will extend OpenTelemetry API to allow specifying Scope attributes when obtaining a |
| 27 | + Tracer, Meter or LogEmitter. Scope attributes will be optional. |
| 28 | +- We will add `attributes` field to the [InstrumentationScope](https://github.com/open-telemetry/opentelemetry-proto/blob/88faab1197a2a105c7da659951e94bc951d37ab9/opentelemetry/proto/common/v1/common.proto#L83) |
| 29 | + message of OTLP. |
| 30 | +- We will specify that Telemetry emitted via a Scope-ed Tracer, Meter or LogEmitter will |
| 31 | + be associated with the Scope's attributes. |
| 32 | +- We will specify that OTLP Exporter will record the attributes in the |
| 33 | + InstrumentationScope message. |
| 34 | +- We will create a section for Scope attributes' semantic conventions in |
| 35 | + the specification. |
| 36 | + |
| 37 | +## Internal details |
| 38 | + |
| 39 | +### API Changes |
| 40 | + |
| 41 | +#### Tracer |
| 42 | + |
| 43 | +`Get a Tracer` API will be extended to add the following parameter: |
| 44 | + |
| 45 | +``` |
| 46 | +- `attributes` (optional): Specifies the instrumentation scope attributes to associate |
| 47 | + with emitted telemetry. |
| 48 | +``` |
| 49 | + |
| 50 | +Since the attributes are optional this is a backwards compatible change. |
| 51 | + |
| 52 | +We will modify the following clause: |
| 53 | + |
| 54 | +``` |
| 55 | +It is unspecified whether or under which conditions the same or different |
| 56 | +`Tracer` instances are returned from this functions. |
| 57 | +``` |
| 58 | + |
| 59 | +and replace it by: |
| 60 | + |
| 61 | +``` |
| 62 | +The implementation MUST NOT return the same `Tracer` when called repeatedly with |
| 63 | +different values of parameters. The only exception to this rule is no-op `Tracer`, the |
| 64 | +implementation MAY return the same instance regardless of parameter values. |
| 65 | +
|
| 66 | +It is unspecified whether or under which conditions the same or different |
| 67 | +`Tracer` instances are returned from this functions when the same |
| 68 | +(name,version,schema_url,attributes) parameters are used. |
| 69 | +``` |
| 70 | + |
| 71 | +Since we are defining more precisely previously undefined behavior this is a |
| 72 | +backwards compatible change. |
| 73 | + |
| 74 | +#### Meter |
| 75 | + |
| 76 | +`Get a Meter` API will be extended to add the following parameter: |
| 77 | + |
| 78 | +``` |
| 79 | +- `attributes` (optional): Specifies the instrumentation scope attributes to associate |
| 80 | + with emitted telemetry. |
| 81 | +``` |
| 82 | + |
| 83 | +We will modify the following clause: |
| 84 | + |
| 85 | +``` |
| 86 | +It is unspecified whether or under which conditions the same or different |
| 87 | +`Meter` instances are returned from this functions. |
| 88 | +``` |
| 89 | + |
| 90 | +and replace it by: |
| 91 | + |
| 92 | +``` |
| 93 | +The implementation MUST NOT return the same `Meter` when called repeatedly with |
| 94 | +different values of parameters. The only exception to this rule is no-op `Meter`, the |
| 95 | +implementation MAY return the same instance regardless of parameter values. |
| 96 | +
|
| 97 | +It is unspecified whether or under which conditions the same or different |
| 98 | +`Meter` instances are returned from this functions when the same |
| 99 | +(name,version,schema_url,attributes) parameters are used. |
| 100 | +``` |
| 101 | + |
| 102 | +#### LogEmitter |
| 103 | + |
| 104 | +`Get LogEmitter` SDK call will be altered to the following: |
| 105 | + |
| 106 | +``` |
| 107 | +Accepts the instrumentation scope name and optional version and attributes and |
| 108 | +returns a LogEmitter associated with the instrumentation scope. |
| 109 | +
|
| 110 | +The implementation MUST NOT return the same `LogEmitter` when called repeatedly with |
| 111 | +different values of parameters. The only exception to this rule is no-op `LogEmitter`, the |
| 112 | +implementation MAY return the same instance regardless of parameter values. |
| 113 | +
|
| 114 | +It is unspecified whether or under which conditions the same or different |
| 115 | +`LogEmitter` instances are returned from this functions when the same |
| 116 | +(name,version,attributes) parameters are used. |
| 117 | +``` |
| 118 | + |
| 119 | +### OTLP Changes |
| 120 | + |
| 121 | +The InstrumentationScope message in OTLP will be modified to add 2 new fields: |
| 122 | +attributes and dropped_attributes_count: |
| 123 | + |
| 124 | +```protobuf |
| 125 | +message InstrumentationScope { |
| 126 | + string name = 1; |
| 127 | + string version = 2; |
| 128 | + repeated KeyValue attributes = 3; |
| 129 | + uint32 dropped_attributes_count = 4; |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +This change is backwards compatible from OTLP's interoperability perspective. Recipients |
| 134 | +of old OTLP versions will not see the Scope attributes and will ignore them, which we |
| 135 | +consider acceptable from interoperability perspective. This is aligned with our general |
| 136 | +stance on what happens when telemetry sources _add_ new data which old recipients |
| 137 | +don't understand: we expect the new data to be safely ignored. |
| 138 | + |
| 139 | +## Attribute Value Precedence |
| 140 | + |
| 141 | +If the same attribute is specified both at the Span/Metric/LogRecord and at the Scope |
| 142 | +then the attribute value at Span/Metric/LogRecord takes precedence. |
| 143 | + |
| 144 | +This rule applies to non-OTLP exporters in SDKs, to conversions from OTLP to non-OTLP |
| 145 | +formats in the Collector and to OTLP recipients of data that need to interpret the |
| 146 | +attributes in the received data. |
| 147 | + |
| 148 | +## Exporting to non-OTLP |
| 149 | + |
| 150 | +SDK's non-OTLP Exporters and Collector's exporter to formats that don't have a concept |
| 151 | +that is equivalent to the Scope will record the attributes at the most suitable place |
| 152 | +in their corresponding format, typically at the Span, Metric or LogRecord equivalent. |
| 153 | + |
| 154 | +## Prior art and alternatives |
| 155 | + |
| 156 | +The [Meter "short_name" PR](https://github.com/open-telemetry/opentelemetry-specification/pull/2422) |
| 157 | +had an alternate approach where the "short_name" was added as the only attribute to the |
| 158 | +InstrumentationScope. This OTEP's proposal generalizes this and allows arbitrary |
| 159 | +attributes which allows them to be used for use cases. |
| 160 | + |
| 161 | +Differentiating the type of data emitted from the scopes that belong to different data |
| 162 | +domains can be alternatively done by recording attributes on the Span, Metric or LogRecord. |
| 163 | +However, this will be less efficient since it will require the same attributes to be |
| 164 | +specified repeatedly on the wire. It will be also cumbersome to require the callers |
| 165 | +to always specify such attributes when creating a Span, Metric or a LogRecord as |
| 166 | +opposed to specifying them once when obtaining the Trace, Meter or LogEmitter. |
| 167 | + |
| 168 | +## Examples |
| 169 | + |
| 170 | +### Usage in Code |
| 171 | + |
| 172 | +The following is an example usage where LogEmitter is used to emit client-side |
| 173 | +log records (pseudocode follows): |
| 174 | + |
| 175 | +``` |
| 176 | +// Obtain loggers once, at startup. |
| 177 | +appLogger = LogEmitterProvider.GetLogEmitter("mylibrary", "1.0.0") |
| 178 | +loggerForUserEvents = LogEmitterProvider.GetLogEmitter("mylibrary", "1.0.0", KeyValue("otel.clientside", true)) |
| 179 | +
|
| 180 | +// Somewhere later in the code when the user clicks a UI element. This should |
| 181 | +// export telemetry with otel.clientside=true Scope attribute set. |
| 182 | +loggerForUserEvents.emit(LogRecord{Body:"click", Attributes:...}) |
| 183 | +
|
| 184 | +// Somewhere else in the code, not related to user interactions. This should |
| 185 | +// export telemetry without any Scope attributes. |
| 186 | +appLogger.emit(LogRecord{Body:"Error occurred while processing the file", Attributes:...}) |
| 187 | +``` |
| 188 | + |
| 189 | +### LogRecord Multiplexing |
| 190 | + |
| 191 | +Here is an example usage where LogRecords are used to represent profiling data, |
| 192 | +client-side events and regular logs. The Scope attribute is used for multiplexing |
| 193 | +and routing the LogRecords: |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +## Open questions |
| 198 | + |
| 199 | +- Should we allow/encourage recording Span/Metric/LogRecord attributes at the Scope level? |
| 200 | + The alternate is to disallow this and have completely separate set of semantic |
| 201 | + conventions that are allowed for Scope attributes only. |
| 202 | +- Can all existing APIs in all languages be safely modified to ensure the addition |
| 203 | + of the optional attributes is not a breaking change? (It should be safe, since we did |
| 204 | + a very similar change when we [introduced the Scope](https://github.com/open-telemetry/opentelemetry-specification/pull/2276)) |
| 205 | + |
| 206 | +## Future possibilities |
| 207 | + |
| 208 | +If this OTEP is accepted we need to then introduce the relevant semantic conventions |
| 209 | +that will make the 2 use cases [described earlier](#motivation) possible. |
0 commit comments