Skip to content

Commit 97112e6

Browse files
committed
fix: simplify resource implementation
1 parent a18009d commit 97112e6

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/otel/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
"@opentelemetry/api": "1.9.0",
121121
"@opentelemetry/core": "1.30.1",
122122
"@opentelemetry/instrumentation": "^0.203.0",
123-
"@opentelemetry/resources": "1.30.1",
124123
"@opentelemetry/sdk-trace-node": "1.30.1"
125124
}
126125
}

packages/otel/src/bootstrap/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type SpanProcessor } from '@opentelemetry/sdk-trace-node'
22
import type { Instrumentation } from '@opentelemetry/instrumentation'
33
import { GET_TRACER, SHUTDOWN_TRACERS } from '../constants.js'
4+
import { Resource } from '../resource.ts'
45

56
export interface TracerProviderOptions {
67
serviceName: string
@@ -20,7 +21,6 @@ export const createTracerProvider = async (options: TracerProviderOptions) => {
2021
const runtimeVersion = nodeVersion.slice(1)
2122

2223
const { W3CTraceContextPropagator } = await import('@opentelemetry/core')
23-
const { Resource } = await import('@opentelemetry/resources')
2424
const { NodeTracerProvider } = await import('@opentelemetry/sdk-trace-node')
2525
const { registerInstrumentations } = await import('@opentelemetry/instrumentation')
2626

packages/otel/src/resource.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)