Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0ac450e
WIP sync and async resource merge
dyladan Jan 16, 2025
49ef5ed
Fix isPromiseLike
dyladan Jan 16, 2025
29ccd0f
Fix tests - TODO: attribute fallback order
dyladan Jan 17, 2025
767947c
Fix resource usages for 2.x
dyladan Jan 21, 2025
9aba287
Fix remaining tests
dyladan Jan 22, 2025
9f231aa
Unskip tests
dyladan Jan 22, 2025
13f2064
Remove deep export
dyladan Jan 22, 2025
2fa42c8
Review comments
dyladan Jan 22, 2025
ff5af1a
Remove noop detector export
dyladan Jan 22, 2025
8e513e4
Merge remote-tracking branch 'origin/main' into resource-async-sync
dyladan Jan 22, 2025
8236217
Lint and cleanup
dyladan Jan 22, 2025
36d6b9c
Changelog
dyladan Jan 22, 2025
c1f6d24
Changelog
dyladan Jan 22, 2025
1de27ba
Use shared empty resource
dyladan Jan 23, 2025
20d297d
Make asyncAttributesPending readonly
dyladan Jan 23, 2025
6d1f3a0
Add explicit return type to resource merge
dyladan Jan 23, 2025
f13ba2c
Fix types and lint
dyladan Jan 23, 2025
15f54f0
Create new resource from list
dyladan Jan 23, 2025
e6dce6b
Explict return types
dyladan Jan 27, 2025
7a93c59
revert package lock changes
dyladan Jan 27, 2025
0467bc8
Merge remote-tracking branch 'origin/main' into resource-async-sync
dyladan Jan 27, 2025
fd4bab8
Revert package lock changes
dyladan Jan 27, 2025
9c74732
Use strict equal
dyladan Jan 27, 2025
9e55991
Fix test compilation
dyladan Jan 28, 2025
eba4a63
Do not await non-promise
dyladan Jan 29, 2025
cd8a8f1
Review comment
dyladan Jan 29, 2025
dde176a
Add comment about merge order
dyladan Jan 31, 2025
ee3e732
Merge remote-tracking branch 'origin/main' into resource-async-sync
dyladan Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Lint and cleanup
  • Loading branch information
dyladan committed Jan 22, 2025
commit 8236217f63bc8986aa72379d93e98cb82f72086d
4 changes: 1 addition & 3 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,7 @@ export class NodeSDK {
detectors: this._resourceDetectors,
};

this._resource = this._resource.merge(
detectResources(internalConfig)
);
this._resource = this._resource.merge(detectResources(internalConfig));
}

this._resource =
Expand Down
32 changes: 12 additions & 20 deletions packages/opentelemetry-resources/src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import {
} from '@opentelemetry/semantic-conventions';
import { IResource } from './IResource';
import { defaultServiceName } from './platform';
import {
DetectedResource,
MaybePromise,
} from './types';
import { DetectedResource, MaybePromise } from './types';
import { isPromiseLike } from './utils';

export class Resource implements IResource {
Expand All @@ -36,7 +33,7 @@ export class Resource implements IResource {

private _memoizedAttributes?: Attributes;

public static EMPTY = new Resource({ attributes: {}});
public static EMPTY = new Resource({ attributes: {} });
/**
* Returns a Resource that identifies the SDK in use.
*/
Expand All @@ -51,10 +48,13 @@ export class Resource implements IResource {
});
}

static FromAttributeList(attributes: [string, MaybePromise<AttributeValue | undefined>][]): Resource {
static FromAttributeList(
attributes: [string, MaybePromise<AttributeValue | undefined>][]
): Resource {
const res = new Resource({});
res._rawAttributes = attributes;
res._asyncAttributesPending = attributes.filter(([_,val]) => isPromiseLike(val)).length > 0
res._asyncAttributesPending =
attributes.filter(([_, val]) => isPromiseLike(val)).length > 0;
return res;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ export class Resource implements IResource {
}

for (let i = 0; i < this._rawAttributes.length; i++) {
const [k,v] = this._rawAttributes[i];
const [k, v] = this._rawAttributes[i];
try {
this._rawAttributes[i] = [k, await v];
} catch (err) {
Expand All @@ -112,7 +112,6 @@ export class Resource implements IResource {

const attrs: Attributes = {};
for (const [k, v] of this._rawAttributes) {
console.log(k,v)
if (isPromiseLike(v)) {
diag.debug(`Unsettled resource attribute ${k} skipped`);
continue;
Expand All @@ -133,16 +132,9 @@ export class Resource implements IResource {
public merge(resource: Resource | null) {
if (resource == null) return this;

// incoming attributes have a higher priority
// const attributes: DetectedResourceAttributes = {};
// for (const [k, v] of [...resource._rawAttributes, ...this._rawAttributes]) {
// if (v != null) {
// attributes[k] ??= v;
// }
// }

// return new Resource({ attributes });

return Resource.FromAttributeList([...resource._rawAttributes, ...this._rawAttributes]);
return Resource.FromAttributeList([
...resource._rawAttributes,
...this._rawAttributes,
]);
}
}
4 changes: 2 additions & 2 deletions packages/opentelemetry-resources/src/detect-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const detectResources = (
return resource;
} catch (e) {
diag.debug(`${d.constructor.name} failed: ${e.message}`);
return new Resource({ attributes: {}});
return new Resource({ attributes: {} });
}
});

Expand All @@ -43,7 +43,7 @@ export const detectResources = (

return resources.reduce(
(acc, resource) => acc.merge(resource),
new Resource({ attributes: {}})
new Resource({ attributes: {} })
);
};

Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry-resources/src/detectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
* limitations under the License.
*/

export {
browserDetector,
} from './BrowserDetector';
export { browserDetector } from './BrowserDetector';
export { envDetector } from './EnvDetector';
export {
hostDetector,
osDetector,
processDetector,
serviceInstanceIdDetector,
} from './platform';
export { noopDetector } from "./NoopDetector";
export { noopDetector } from './NoopDetector';
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
} from '@opentelemetry/semantic-conventions';
import { arch, hostname } from 'os';
import { ResourceDetectionConfig } from '../../../config';
import { DetectedResource, DetectedResourceAttributes, ResourceDetector } from '../../../types';
import {
DetectedResource,
DetectedResourceAttributes,
ResourceDetector,
} from '../../../types';
import { getMachineId } from './machine-id/getMachineId';
import { normalizeArch } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ class ServiceInstanceIdDetector implements ResourceDetector {
/**
* @experimental
*/
export const serviceInstanceIdDetector =
new ServiceInstanceIdDetector();
export const serviceInstanceIdDetector = new ServiceInstanceIdDetector();
6 changes: 5 additions & 1 deletion packages/opentelemetry-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export {
export { IResource } from './IResource';
export { defaultServiceName } from './platform';
export { Resource } from './Resource';
export { ResourceDetector, DetectedResource, DetectedResourceAttributes } from './types';
export {
ResourceDetector,
DetectedResource,
DetectedResourceAttributes,
} from './types';
18 changes: 14 additions & 4 deletions packages/opentelemetry-resources/test/Resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ describe('Resource', () => {
});
const actualResource = resource1.merge(resource2);
assert.strictEqual(Object.keys(actualResource.attributes).length, 5);
assert.deepStrictEqual(actualResource.attributes, expectedResource.attributes);
assert.deepStrictEqual(
actualResource.attributes,
expectedResource.attributes
);
});

it('should return merged resource when collision in attributes', () => {
Expand All @@ -75,7 +78,10 @@ describe('Resource', () => {
});
const actualResource = resource1.merge(resource3);
assert.strictEqual(Object.keys(actualResource.attributes).length, 4);
assert.deepStrictEqual(actualResource.attributes, expectedResource.attributes);
assert.deepStrictEqual(
actualResource.attributes,
expectedResource.attributes
);
});

it('should return merged resource when first resource is empty', () => {
Expand Down Expand Up @@ -152,7 +158,9 @@ describe('Resource', () => {
const resourceReject = new Resource({
attributes: {
async: new Promise((_, reject) => {
setTimeout(() => { reject(new Error('reject')) }, 1);
setTimeout(() => {
reject(new Error('reject'));
}, 1);
}),
},
});
Expand All @@ -169,7 +177,9 @@ describe('Resource', () => {
attributes: {
sync: 'fromsync',
// async attribute resolves after 1ms
async: new Promise(resolve => setTimeout(() => resolve('fromasync'), 1)),
async: new Promise(resolve =>
setTimeout(() => resolve('fromasync'), 1)
),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,10 @@ export const assertEmptyResource = (resource: DetectedResource) => {
* Assert that the `resource` has at least one known attribute with the given
* `prefix`. By "known", we mean it is an attribute defined in semconv.
*/
const assertHasOneLabel = (prefix: string, resource: DetectedResource): void => {
const assertHasOneLabel = (
prefix: string,
resource: DetectedResource
): void => {
const semconvModPrefix = `SEMRESATTRS_${prefix.toUpperCase()}_`;
const knownAttrs: Set<string> = new Set(
Object.entries(semconv)
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-metrics/test/export/MetricReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('MetricReader', () => {
resource: new Resource({
attributes: {
shouldBeDiscarded: 'should-be-discarded',
}
},
}),
scopeMetrics: testScopeMetrics,
},
Expand Down
Loading