|
| 1 | +# Multiple Resource in SDK |
| 2 | + |
| 3 | +Allow multiple `Resource` instances per SDK. |
| 4 | + |
| 5 | +## Motivation |
| 6 | + |
| 7 | +OpenTelemetry needs to address two fundamental problems: |
| 8 | + |
| 9 | +- Reporting data against "mutable" or "changing" entities, where currently an |
| 10 | + SDK is allowed a single `Resource`, whose lifetime must match the lifetime of |
| 11 | + the SDK itself. |
| 12 | +- Providing true multi-tenant capabilities, where, e.g. metrics about one tenant |
| 13 | + will be implicitly separated from metrics about another tenant. |
| 14 | + |
| 15 | +The first problem is well outlined in (not accepted) [OTEP 4316](https://github.com/open-telemetry/opentelemetry-specification/pull/4316). |
| 16 | +Fundamentally, while we need an immutable identity, the reality is that `Resource` |
| 17 | +in today's OpenTelemetry usage is not strong enough to support key use cases. For example, |
| 18 | +OpenTelemetry JS, in the node.js environment, cannot guarantee that all identifying |
| 19 | +attributes for Resource are discovered prior to SDK startup, leading to an "eventual identity" situation |
| 20 | +that must be addressed in the Specification. Additionally, our Client/Browser SIG has been |
| 21 | +trying to model the notion of "User Session" which has a much shorter lifespan than the SDK itself, so |
| 22 | +requiring a single identity that is both immutable and matches the SDK lifetime prevents any good mechanism of reporting user session. |
| 23 | + |
| 24 | +However, [OTEP 4316](https://github.com/open-telemetry/opentelemetry-specification/pull/4316) explores |
| 25 | +relaxing the immutability restriction vs. providing a new mechanism. During prototyping, |
| 26 | +initially this seemed to be easily accomplished, but ran into major complications both in interactions |
| 27 | +with OpAmp (where a stable identity for the SDK is desired), and in designing a Metrics SDK, where |
| 28 | +changes in Resource mean a dynamic and divergent storage strategy, without a priori knowledge of whether these resource mutations are |
| 29 | +relevant to the metric or not. |
| 30 | + |
| 31 | +Additionally, today when reporting data from one "process" about multiple resources, the only recourse available is to instantiate |
| 32 | +multiple SDKs and define different resources in each SDK. This absolute separation can be highly problematic with the notion of |
| 33 | +"built-in" instrumentation, where libraries (e.g. gRPC) come with an out-of-the-box OpenTelemetry support and it's unclear how |
| 34 | +to ensure this instrumentation is use correctly in the presence of multiple SDKs. |
| 35 | + |
| 36 | +## Explanation |
| 37 | + |
| 38 | +We proposes these new fundamental concepts in OpenTelemetry: |
| 39 | + |
| 40 | +- `Resource` *remains immutable* |
| 41 | + - Building on [OTEP 264](0264-resource-and-entities.md), identifying attributes |
| 42 | + are clearly outlined in Resource going forward, addressing unclear real world usage of Resource attributes ([e,g, identifying attributes in OpAMP](https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#agentdescriptionidentifying_attributes)). |
| 43 | + - SDK will be given an explicit initialization stage where `Resource` is not in a complete state, addressing OpenTelemetry JS concerns around async resource detection. |
| 44 | +- The SDK will be identified by a single `Resource` provided during SDK startup. |
| 45 | + - ResourceDetection will be expanded, as described in [OTEP 264](0264-resource-and-entities.md). |
| 46 | + - An explicit section about SDK initialization will be created. |
| 47 | +- Signal Providers in the SDK will allow "specialization" of the default SDK |
| 48 | + resource. We create new `{Signal}Provider` instances by providing a new |
| 49 | + `Entity` on the existing provider. |
| 50 | + - This will construct a new `Resource` specific to that provider. |
| 51 | + - The new provider will re-use all configuration (e.g. export pipeline) |
| 52 | + defined from the base provider. |
| 53 | + |
| 54 | +## Internal details |
| 55 | + |
| 56 | +This proposal splits between an instrumentation facing API, and required |
| 57 | +behavior of that API in the SDK. |
| 58 | + |
| 59 | +### API Details |
| 60 | + |
| 61 | +Previously, every `{Signal}Provider` API defined a single |
| 62 | +`Get a {Signal}` operation. These will be expanded with a new |
| 63 | +`For Entity` operation, which will construct a new `{SignalProvider}` |
| 64 | +API component for reporting against a specific `Entity`. |
| 65 | + |
| 66 | +#### For Entity |
| 67 | + |
| 68 | +This API MUST accept the following parameters: |
| 69 | + |
| 70 | +* `entities`: Specifies the `Entity` set to associate with |
| 71 | + emitted telemetry. |
| 72 | + |
| 73 | +Any `entities` provided which conflict with those entities already provided in |
| 74 | +the SDK `Resource` represent an *override* of identity. The SDK MUST resolve the |
| 75 | +conflict without causing a fatal error. |
| 76 | + |
| 77 | +The set of `Entity` provided to these operations MUST only include one `Entity` |
| 78 | +per `type`. |
| 79 | + |
| 80 | +#### Entity |
| 81 | + |
| 82 | +An `Entity` is a collection of the following values: |
| 83 | + |
| 84 | +- `type`: A string describing the class of the Entity. |
| 85 | +- `id`: An attribute collection that identifies an instance of the Entity. |
| 86 | +- (optional) `description`: An attribute collection that describes the instance |
| 87 | + of the Entity. |
| 88 | +- (optional) `schema_url`: Specifies the Schema URL that should be recorded for |
| 89 | + this Entity. |
| 90 | + |
| 91 | +An `Entity` is uniquely identified by the combination of its `type` and `id`. |
| 92 | + |
| 93 | +`schema_url` defines version of the schema used to describe an `Entity`. If |
| 94 | +two entities exist with the same identity and different `schema_url`s, they |
| 95 | +MUST be considered in conflict with each other. |
| 96 | + |
| 97 | +### SDK Details |
| 98 | + |
| 99 | +When `For Entity` operation is received by a provider, A new child |
| 100 | +`Entity Bound Provider` of the same type MUST be created and returned with the |
| 101 | +following restrictions: |
| 102 | + |
| 103 | +- `Entity Bound Provider` MUST be associated with a newly created `Resource` |
| 104 | + which is the result of the incoming `Entity` set merged into the original |
| 105 | + `Provider`'s resource following the existing `Resource` merging algorithm. |
| 106 | + Telemetry created by the parent MUST continue to be associated with the |
| 107 | + original unmodified resource. |
| 108 | +- The `Bound Provider` MUST share an export pipeline with its parent. The export |
| 109 | + component (`SpanProcessor`, `MetricReader`, `LogsProcessor`, etc) MUST not be |
| 110 | + `Shutdown` by the `Bound Provider`. This MAY be achieved by wrapping the export |
| 111 | + component in a proxy component which ignores calls to `Shutdown` or translates |
| 112 | + them into `Force Flush`. |
| 113 | +- The `Bound Provider` MUST be configured exactly the same as its parent. |
| 114 | + A configuration change on a parent `Provider` MUST be reflected in all of its |
| 115 | + child `Entity Bound Providers`. This MAY be achieved by directly sharing |
| 116 | + the configuration object between `Providers`. |
| 117 | +- A `Bound Provider` MUST NOT be directly configurable. |
| 118 | + All configuration comes from its parent. |
| 119 | +- If `ForceFlush` or `Shutdown` is called on a `Provider` it MUST also flush all |
| 120 | + of its child `Entity Bound Providers`. |
| 121 | +- If `Shutdown` is called on a `Bound Provider` it MUST be treated as a |
| 122 | + `Force Flush`. It MUST NOT shut down its export pipeline. |
| 123 | + |
| 124 | +## Trade-offs and mitigations |
| 125 | + |
| 126 | +The primary trade-offs to make here are around "breaking changes" and subtle |
| 127 | +differences in generated telemetry for code leveraging Entity vs. code which |
| 128 | +does not. We need give room for consumers of OTLP (vendors, databases, collector) |
| 129 | +time to support the new semantics of Entity prior to data showing up which would |
| 130 | +not be correctly interpreted without understanding these new semantics. As such, |
| 131 | +primarily: |
| 132 | + |
| 133 | +- `Entity`, as defined in OTLP, is an opt-in construct. `Resource` should be |
| 134 | + usable as an identity independent of `Entity`. |
| 135 | +- Consumers should now expect SDKs reporting multiple resources in the same |
| 136 | + batch. Theoretically, this SHOULD already be supported due to how OTLP is |
| 137 | + designed to allow aggregation / batching of data at any point. |
| 138 | + |
| 139 | +## Prior art and alternatives |
| 140 | + |
| 141 | +OpenCensus previously allowed contextual tags to be specified dynamically and |
| 142 | +used everywhere metric measurements were reported. Users were then required to |
| 143 | +select which of these were useful to them via the definition of "Views". |
| 144 | +OpenTelemetry has aimed for a simpler solution where every metric has an |
| 145 | +implicit View definition, and we leverage metric advice to allow sending |
| 146 | +attributes than is naturally used when reporting the metric. |
| 147 | + |
| 148 | +As called out in the description, [OTEP 4316](https://github.com/open-telemetry/opentelemetry-specification/pull/4316) |
| 149 | +proposes making resource fully mutable, which comes with its own set of tradeoffs. |
| 150 | + |
| 151 | +Today, Semantic Conventions already defines `Entity` and uses it to group and |
| 152 | +report `Resource` attributes cohesively. Additionally, Semantic convention only |
| 153 | +models "entity associations", that is requiring a signal (e.g. a metric, event |
| 154 | +or span) to be attached to an entity. For example, the `system.cpu.time` metric |
| 155 | +is expected to be associated with a `host` entity. This association makes no |
| 156 | +assumption about whether that is through `Resource` or some other mechanism, |
| 157 | +and can therefore be extended to support `InstrumentationScope` based entities. |
| 158 | + |
| 159 | +## Open questions |
| 160 | + |
| 161 | +Adding entity in InstrumentationScope has a lot of implications that must be |
| 162 | +resolved. |
| 163 | + |
| 164 | +### What are the SDK safeguards against high-cardinality Entities? |
| 165 | + |
| 166 | +As seen in [Issue #3062](https://github.com/open-telemetry/opentelemetry-specification/issues/3062), |
| 167 | +systems observing multiple tenants need to ensure that tenants which are only observed briefly do not |
| 168 | +continue to consume resources (particularly memory) for long periods of time. There needs to be |
| 169 | +some level of control (either direct or implicit) in allowing new "Scope with Entity" to be created. |
| 170 | + |
| 171 | +### What happens when an entity already exists within Resource? |
| 172 | + |
| 173 | +Should we consider this a failure or a feature? |
| 174 | + |
| 175 | +We currently consider this a feature. Upon conflict, the *new* Entity would be |
| 176 | +used in the resulting `Resource` reported for a new `{SignalProvider}`. |
| 177 | + |
| 178 | +The SDK needs some form of stable identity for itself, however when reporting |
| 179 | +Telemetry, it may be recording data on behalf of some other system. |
| 180 | + |
| 181 | +### Are descriptive attributes allowed to change for Resource? |
| 182 | + |
| 183 | +Its not clear how resource immutability is kept or what is meant by immutable. |
| 184 | +Will the resource emitted on the first export be the same as the one emitted for |
| 185 | +the entire lifetime of the process? Are descriptive attributes on entities |
| 186 | +attached to resource still allowed to change? What about attaching new entities |
| 187 | +to that resource? |
| 188 | + |
| 189 | +For now: |
| 190 | + |
| 191 | +- The set of entities reported on Resource becomes locked. |
| 192 | + All identifying attributes are also locked. |
| 193 | +- Whether we want to allow descriptive attributes to change - this can be |
| 194 | + determined or evolve over time. Until the ecosystem around OTLP is leveraging |
| 195 | + the "identity" attributes of Entity for Resource, we should not allow |
| 196 | + mutation of descriptive attributes. |
| 197 | + |
| 198 | +### What is the expected impact on Collector components? |
| 199 | + |
| 200 | +There should be *no* impact on collector components beyond those defined in |
| 201 | +[OTEP 4316](https://github.com/open-telemetry/opentelemetry-specification/pull/4316). |
| 202 | + |
| 203 | +### How do we guide developers on when to use `For Entity`? |
| 204 | + |
| 205 | +We will have clear guidance on the `For Entity` methods |
| 206 | + |
| 207 | +## Prototypes |
| 208 | + |
| 209 | +- Java: https://github.com/open-telemetry/opentelemetry-java/compare/main...jsuereth:opentelemetry-java:wip-entity-and-providers |
| 210 | +- TypeScript: https://github.com/open-telemetry/opentelemetry-js/pull/5620 |
| 211 | + |
| 212 | +## Future possibilities |
| 213 | + |
| 214 | +This proposal brings strong multi-tenant capabilities to the OpenTelemetry SDK. One possibility |
| 215 | +is to improve the interaction between dynamic `Context` and signals, e.g. allowing |
| 216 | +some level of interaction `Context` and attributes / entities. |
| 217 | + |
| 218 | +For example, rather than a lexical scope: |
| 219 | + |
| 220 | +```js |
| 221 | +const myMeterProvider = globalMeterProvider.forEntity(getCurrentSession()) |
| 222 | +doSomething(myMeterProvider) |
| 223 | +``` |
| 224 | + |
| 225 | +We could allow runtime scope: |
| 226 | + |
| 227 | +```js |
| 228 | +const ctx = api.context.active(); |
| 229 | +api.context.with(ctx.setValue(CURRENT_ENTITY_KEY, getCurrentSession())) |
| 230 | +doSomething() |
| 231 | +``` |
0 commit comments