Skip to content

Commit

Permalink
refactor(resources)!: replace ResourceAttributes with Attributes (#5016)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna authored Sep 26, 2024
1 parent e15d5b3 commit 91c67ba
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* feat(sdk-metrics)!: remove MeterProvider.addMetricReader() in favor of constructor option [#4419](https://github.com/open-telemetry/opentelemetry-js/pull/4419) @pichlermarc
* feat(sdk-metrics)!: replace attributeKeys with custom processors option [#4532](https://github.com/open-telemetry/opentelemetry-js/pull/4532) @pichlermarc
* refactor(sdk-trace-base)!: replace `SpanAttributes` with `Attributes` [#5009](https://github.com/open-telemetry/opentelemetry-js/pull/5009) @david-luna
* refactor(resources)!: replace `ResourceAttributes` with `Attributes` [#5016](https://github.com/open-telemetry/opentelemetry-js/pull/5016) @david-luna

### :rocket: (Enhancement)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/opentelemetry-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"webpack-merge": "5.10.0"
},
"peerDependencies": {
"@opentelemetry/api": ">=1.0.0 <1.10.0"
"@opentelemetry/api": ">=1.3.0 <1.10.0"
},
"dependencies": {
"@opentelemetry/core": "1.26.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-resources/src/IResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { ResourceAttributes } from './types';
import { Attributes } from '@opentelemetry/api';

/**
* An interface that represents a resource. A Resource describes the entity for which signals (metrics or trace) are
Expand All @@ -33,7 +33,7 @@ export interface IResource {
/**
* @returns the Resource's attributes.
*/
readonly attributes: ResourceAttributes;
readonly attributes: Attributes;

/**
* Returns a promise that will never be rejected. Resolves when all async attributes have finished being added to
Expand Down
15 changes: 7 additions & 8 deletions packages/opentelemetry-resources/src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { Attributes, diag } from '@opentelemetry/api';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
} from '@opentelemetry/semantic-conventions';
import { SDK_INFO } from '@opentelemetry/core';
import { ResourceAttributes } from './types';
import { defaultServiceName } from './platform';
import { IResource } from './IResource';

Expand All @@ -32,9 +31,9 @@ import { IResource } from './IResource';
*/
export class Resource implements IResource {
static readonly EMPTY = new Resource({});
private _syncAttributes?: ResourceAttributes;
private _asyncAttributesPromise?: Promise<ResourceAttributes>;
private _attributes?: ResourceAttributes;
private _syncAttributes?: Attributes;
private _asyncAttributesPromise?: Promise<Attributes>;
private _attributes?: Attributes;

/**
* Check if async attributes have resolved. This is useful to avoid awaiting
Expand Down Expand Up @@ -72,8 +71,8 @@ export class Resource implements IResource {
* information about the entity as numbers, strings or booleans
* TODO: Consider to add check/validation on attributes.
*/
attributes: ResourceAttributes,
asyncAttributesPromise?: Promise<ResourceAttributes>
attributes: Attributes,
asyncAttributesPromise?: Promise<Attributes>
) {
this._attributes = attributes;
this.asyncAttributesPending = asyncAttributesPromise != null;
Expand All @@ -92,7 +91,7 @@ export class Resource implements IResource {
);
}

get attributes(): ResourceAttributes {
get attributes(): Attributes {
if (this.asyncAttributesPending) {
diag.error(
'Accessing resource attributes before async attributes settled'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import {
SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION,
SEMRESATTRS_PROCESS_RUNTIME_NAME,
SEMRESATTRS_PROCESS_RUNTIME_VERSION,
} from '@opentelemetry/semantic-conventions';
import { DetectorSync, ResourceAttributes } from '../types';
import { DetectorSync } from '../types';
import { diag } from '@opentelemetry/api';
import { ResourceDetectionConfig } from '../config';
import { IResource } from '../IResource';
Expand All @@ -39,7 +40,7 @@ class BrowserDetectorSync implements DetectorSync {
if (!isBrowser) {
return Resource.empty();
}
const browserResource: ResourceAttributes = {
const browserResource: Attributes = {
[SEMRESATTRS_PROCESS_RUNTIME_NAME]: 'browser',
[SEMRESATTRS_PROCESS_RUNTIME_DESCRIPTION]: 'Web Browser',
[SEMRESATTRS_PROCESS_RUNTIME_VERSION]: navigator.userAgent,
Expand All @@ -54,7 +55,7 @@ class BrowserDetectorSync implements DetectorSync {
* @returns The sanitized resource attributes.
*/
private _getResourceAttributes(
browserResource: ResourceAttributes,
browserResource: Attributes,
_config?: ResourceDetectionConfig
) {
if (browserResource[SEMRESATTRS_PROCESS_RUNTIME_VERSION] === '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { Attributes, diag } from '@opentelemetry/api';
import { getEnv } from '@opentelemetry/core';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { Resource } from '../Resource';
import { DetectorSync, ResourceAttributes } from '../types';
import { DetectorSync } from '../types';
import { ResourceDetectionConfig } from '../config';
import { IResource } from '../IResource';

Expand Down Expand Up @@ -54,7 +54,7 @@ class EnvDetectorSync implements DetectorSync {
* @param config The resource detection config
*/
detect(_config?: ResourceDetectionConfig): IResource {
const attributes: ResourceAttributes = {};
const attributes: Attributes = {};
const env = getEnv();

const rawAttributes = env.OTEL_RESOURCE_ATTRIBUTES;
Expand Down Expand Up @@ -90,12 +90,10 @@ class EnvDetectorSync implements DetectorSync {
* of key/value pairs.
* @returns The sanitized resource attributes.
*/
private _parseResourceAttributes(
rawEnvAttributes?: string
): ResourceAttributes {
private _parseResourceAttributes(rawEnvAttributes?: string): Attributes {
if (!rawEnvAttributes) return {};

const attributes: ResourceAttributes = {};
const attributes: Attributes = {};
const rawAttributes: string[] = rawEnvAttributes.split(
this._COMMA_SEPARATOR,
-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import {
SEMRESATTRS_HOST_ARCH,
SEMRESATTRS_HOST_ID,
SEMRESATTRS_HOST_NAME,
} from '@opentelemetry/semantic-conventions';
import { Resource } from '../../../Resource';
import { DetectorSync, ResourceAttributes } from '../../../types';
import { DetectorSync } from '../../../types';
import { ResourceDetectionConfig } from '../../../config';
import { arch, hostname } from 'os';
import { normalizeArch } from './utils';
Expand All @@ -32,17 +33,17 @@ import { getMachineId } from './machine-id/getMachineId';
*/
class HostDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): Resource {
const attributes: ResourceAttributes = {
const attributes: Attributes = {
[SEMRESATTRS_HOST_NAME]: hostname(),
[SEMRESATTRS_HOST_ARCH]: normalizeArch(arch()),
};

return new Resource(attributes, this._getAsyncAttributes());
}

private _getAsyncAttributes(): Promise<ResourceAttributes> {
private _getAsyncAttributes(): Promise<Attributes> {
return getMachineId().then(machineId => {
const attributes: ResourceAttributes = {};
const attributes: Attributes = {};
if (machineId) {
attributes[SEMRESATTRS_HOST_ID] = machineId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import {
SEMRESATTRS_OS_TYPE,
SEMRESATTRS_OS_VERSION,
} from '@opentelemetry/semantic-conventions';
import { Resource } from '../../../Resource';
import { DetectorSync, ResourceAttributes } from '../../../types';
import { DetectorSync } from '../../../types';
import { ResourceDetectionConfig } from '../../../config';
import { platform, release } from 'os';
import { normalizeType } from './utils';
Expand All @@ -30,7 +31,7 @@ import { normalizeType } from './utils';
*/
class OSDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): Resource {
const attributes: ResourceAttributes = {
const attributes: Attributes = {
[SEMRESATTRS_OS_TYPE]: normalizeType(platform()),
[SEMRESATTRS_OS_VERSION]: release(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { Attributes, diag } from '@opentelemetry/api';
import {
SEMRESATTRS_PROCESS_COMMAND,
SEMRESATTRS_PROCESS_COMMAND_ARGS,
Expand All @@ -27,7 +27,7 @@ import {
SEMRESATTRS_PROCESS_RUNTIME_VERSION,
} from '@opentelemetry/semantic-conventions';
import { Resource } from '../../../Resource';
import { DetectorSync, ResourceAttributes } from '../../../types';
import { DetectorSync } from '../../../types';
import { ResourceDetectionConfig } from '../../../config';
import { IResource } from '../../../IResource';
import * as os from 'os';
Expand All @@ -38,7 +38,7 @@ import * as os from 'os';
*/
class ProcessDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): IResource {
const attributes: ResourceAttributes = {
const attributes: Attributes = {
[SEMRESATTRS_PROCESS_PID]: process.pid,
[SEMRESATTRS_PROCESS_EXECUTABLE_NAME]: process.title,
[SEMRESATTRS_PROCESS_EXECUTABLE_PATH]: process.execPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* limitations under the License.
*/

import { Attributes } from '@opentelemetry/api';
import { SEMRESATTRS_SERVICE_INSTANCE_ID } from '@opentelemetry/semantic-conventions';
import { Resource } from '../../../Resource';
import { DetectorSync, ResourceAttributes } from '../../../types';
import { DetectorSync } from '../../../types';
import { ResourceDetectionConfig } from '../../../config';
import { randomUUID } from 'crypto';

Expand All @@ -25,7 +26,7 @@ import { randomUUID } from 'crypto';
*/
class ServiceInstanceIdDetectorSync implements DetectorSync {
detect(_config?: ResourceDetectionConfig): Resource {
const attributes: ResourceAttributes = {
const attributes: Attributes = {
[SEMRESATTRS_SERVICE_INSTANCE_ID]: randomUUID(),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
export { Resource } from './Resource';
export { IResource } from './IResource';
export { defaultServiceName } from './platform';
export { DetectorSync, ResourceAttributes, Detector } from './types';
export { DetectorSync, Detector } from './types';
export { ResourceDetectionConfig } from './config';
export {
browserDetector,
Expand Down
7 changes: 0 additions & 7 deletions packages/opentelemetry-resources/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,8 @@
*/

import { ResourceDetectionConfig } from './config';
import { Attributes } from '@opentelemetry/api';
import { IResource } from './IResource';

/**
* Interface for Resource attributes.
*/
// TODO: replace ResourceAttributes with Attributes
export type ResourceAttributes = Attributes;

/**
* @deprecated please use {@link DetectorSync}
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-resources/test/Resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import * as sinon from 'sinon';
import * as assert from 'assert';
import { SDK_INFO } from '@opentelemetry/core';
import { Resource, ResourceAttributes } from '../src';
import { Resource } from '../src';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
SEMRESATTRS_TELEMETRY_SDK_NAME,
SEMRESATTRS_TELEMETRY_SDK_VERSION,
} from '@opentelemetry/semantic-conventions';
import { describeBrowser, describeNode } from './util';
import { diag } from '@opentelemetry/api';
import { Attributes, diag } from '@opentelemetry/api';
import { Resource as Resource190 } from '@opentelemetry/resources_1.9.0';

describe('Resource', () => {
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('Resource', () => {

it('should merge async attributes into sync attributes once resolved', async () => {
//async attributes that resolve after 1 ms
const asyncAttributes = new Promise<ResourceAttributes>(resolve => {
const asyncAttributes = new Promise<Attributes>(resolve => {
setTimeout(
() => resolve({ async: 'fromasync', shared: 'fromasync' }),
1
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('Resource', () => {
);

//async attributes that resolve after 1 ms
const asyncAttributes = new Promise<ResourceAttributes>(resolve => {
const asyncAttributes = new Promise<Attributes>(resolve => {
setTimeout(
() => resolve({ promise2: 'promise2val', shared: 'promise2val' }),
1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import {
Span,
SpanExporter,
} from '../../../src';
import { context } from '@opentelemetry/api';
import { Attributes, context } from '@opentelemetry/api';
import { TestRecordOnlySampler } from './TestRecordOnlySampler';
import { TestTracingSpanExporter } from './TestTracingSpanExporter';
import { TestStackContextManager } from './TestStackContextManager';
import { BatchSpanProcessorBase } from '../../../src/export/BatchSpanProcessorBase';
import { Resource, ResourceAttributes } from '@opentelemetry/resources';
import { Resource } from '@opentelemetry/resources';

function createSampledSpan(spanName: string): Span {
const tracer = new BasicTracerProvider({
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('BatchSpanProcessorBase', () => {
const tracer = new BasicTracerProvider({
resource: new Resource(
{},
new Promise<ResourceAttributes>(resolve => {
new Promise<Attributes>(resolve => {
setTimeout(() => resolve({ async: 'fromasync' }), 1);
})
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
} from '../../../src';
import { TestStackContextManager } from './TestStackContextManager';
import { TestTracingSpanExporter } from './TestTracingSpanExporter';
import { Resource, ResourceAttributes } from '@opentelemetry/resources';
import { Attributes } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { TestExporterWithDelay } from './TestExporterWithDelay';

describe('SimpleSpanProcessor', () => {
Expand Down Expand Up @@ -164,7 +165,7 @@ describe('SimpleSpanProcessor', () => {
const providerWithAsyncResource = new BasicTracerProvider({
resource: new Resource(
{},
new Promise<ResourceAttributes>(resolve => {
new Promise<Attributes>(resolve => {
setTimeout(() => resolve({ async: 'fromasync' }), 1);
})
),
Expand Down Expand Up @@ -205,7 +206,7 @@ describe('SimpleSpanProcessor', () => {
const providerWithAsyncResource = new BasicTracerProvider({
resource: new Resource(
{},
new Promise<ResourceAttributes>(resolve => {
new Promise<Attributes>(resolve => {
setTimeout(() => resolve({ async: 'fromasync' }), 1);
})
),
Expand Down

0 comments on commit 91c67ba

Please sign in to comment.