|
| 1 | +import { Attributes } from '@opentelemetry/api' |
| 2 | + |
| 3 | +export type ResourceAttributes = Attributes |
| 4 | + |
| 5 | +/** |
| 6 | + * An interface that represents a resource. A Resource describes the entity for which signals (metrics or trace) are |
| 7 | + * collected. |
| 8 | + * |
| 9 | + */ |
| 10 | +export interface IResource { |
| 11 | + /** |
| 12 | + * Check if async attributes have resolved. This is useful to avoid awaiting |
| 13 | + * waitForAsyncAttributes (which will introduce asynchronous behavior) when not necessary. |
| 14 | + * |
| 15 | + * @returns true if the resource "attributes" property is not yet settled to its final value |
| 16 | + */ |
| 17 | + asyncAttributesPending?: boolean |
| 18 | + |
| 19 | + /** |
| 20 | + * @returns the Resource's attributes. |
| 21 | + */ |
| 22 | + readonly attributes: ResourceAttributes |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to |
| 26 | + * this Resource's attributes. This is useful in exporters to block until resource detection |
| 27 | + * has finished. |
| 28 | + */ |
| 29 | + waitForAsyncAttributes?(): Promise<void> |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns a new, merged {@link Resource} by merging the current Resource |
| 33 | + * with the other Resource. In case of a collision, other Resource takes |
| 34 | + * precedence. |
| 35 | + * |
| 36 | + * @param other the Resource that will be merged with this. |
| 37 | + * @returns the newly merged Resource. |
| 38 | + */ |
| 39 | + merge(other: IResource | null): IResource |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * A Resource describes the entity for which a signals (metrics or trace) are |
| 44 | + * collected. |
| 45 | + */ |
| 46 | +export class Resource implements IResource { |
| 47 | + private _attributes?: ResourceAttributes |
| 48 | + private _syncAttributes?: ResourceAttributes |
| 49 | + |
| 50 | + constructor( |
| 51 | + /** |
| 52 | + * A dictionary of attributes with string keys and values that provide |
| 53 | + * information about the entity as numbers, strings or booleans |
| 54 | + */ |
| 55 | + attributes: ResourceAttributes, |
| 56 | + ) { |
| 57 | + this._attributes = attributes |
| 58 | + this._syncAttributes = this._attributes ?? {} |
| 59 | + } |
| 60 | + |
| 61 | + get attributes(): ResourceAttributes { |
| 62 | + return this._attributes ?? {} |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to |
| 67 | + * this Resource's attributes. This is useful in exporters to block until resource detection |
| 68 | + * has finished. |
| 69 | + */ |
| 70 | + async waitForAsyncAttributes?(): Promise<void> {} |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns a new, merged {@link Resource} by merging the current Resource |
| 74 | + * with the other Resource. In case of a collision, other Resource takes |
| 75 | + * precedence. |
| 76 | + * |
| 77 | + * @param other the Resource that will be merged with this. |
| 78 | + * @returns the newly merged Resource. |
| 79 | + */ |
| 80 | + merge(other: IResource | null): IResource { |
| 81 | + if (!other) return this |
| 82 | + |
| 83 | + // SpanAttributes from other resource overwrite attributes from this resource. |
| 84 | + return new Resource({ |
| 85 | + ...this._syncAttributes, |
| 86 | + ...(other as Resource)._syncAttributes, |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments