Skip to content

Commit

Permalink
feat(resource)!: Remove resource class export in favor of functions a…
Browse files Browse the repository at this point in the history
…nd types only to aid in cross-version compatibility (#5421)

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
dyladan and pichlermarc authored Feb 13, 2025
1 parent 7438735 commit 4b8ae0c
Show file tree
Hide file tree
Showing 68 changed files with 526 additions and 550 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
* (user-facing): `baggageUtils.parsePairKeyValue` was an internal utility function that was unintentionally exported. It has been removed without replacement.
* (user-facing): `TimeOriginLegacy` has been removed without replacement.
* (user-facing): `isAttributeKey` was an internal utility function that was unintentionally exported. It has been removed without replacement.
* feat(resource)!: Remove resource class export in favor of functions and types only to aid in cross-version compatibility [#5421](https://github.com/open-telemetry/opentelemetry-js/pull/5421)
* Renames `Resource` class to `ResourceImpl` and makes it package-private
* Renames `IResource` interface to `Resource`
* Export function `resourceFromAttributes` to create a `Resource` from a `DetectedAttributes` object
* Only export types and functions. This aids in cross-version compatibility and makes it more easily extensible in the future.

### :rocket: (Enhancement)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ const process = require('process');
const opentelemetry = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base');
const { Resource } = require('@opentelemetry/resources');
const { resourceFromAttributes } = require('@opentelemetry/resources');
const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-conventions');

// configure the SDK to export telemetry data to the console
// enable all auto-instrumentations from the meta package
const traceExporter = new ConsoleSpanExporter();
const sdk = new opentelemetry.NodeSDK({
resource: new Resource({
resource: resourceFromAttributes({
[SEMRESATTRS_SERVICE_NAME]: 'my-service',
}),
traceExporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ npm install --save @opentelemetry/opentelemetry-browser-detector
## Usage

```js
import { Resource, detectResources } from '@opentelemetry/resources';
import { resourceFromAttributes, detectResources } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { browserDetector } from '@opentelemetry/opentelemetry-browser-detector';

async function start(){
let resource= new Resource({
let resource = resourceFromAttributes({
[SEMRESATTRS_SERVICE_NAME]: 'Test App Name',
});
let detectedResources= await detectResources({detectors:[browserDetector]});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { Attributes, diag } from '@opentelemetry/api';
import {
DetectedResource,
ResourceDetector,
Resource,
ResourceDetectionConfig,
EMPTY_RESOURCE,
} from '@opentelemetry/resources';
import { BROWSER_ATTRIBUTES, UserAgentData } from './types';

Expand All @@ -30,7 +30,7 @@ class BrowserDetector implements ResourceDetector {
detect(config?: ResourceDetectionConfig): DetectedResource {
const isBrowser = typeof navigator !== 'undefined';
if (!isBrowser) {
return Resource.EMPTY;
return EMPTY_RESOURCE;
}
const browserResource: Attributes = getBrowserAttributes();
return this._getResourceAttributes(browserResource, config);
Expand All @@ -53,7 +53,7 @@ class BrowserDetector implements ResourceDetector {
diag.debug(
'BrowserDetector failed: Unable to find required browser resources. '
);
return Resource.EMPTY;
return EMPTY_RESOURCE;
} else {
return { attributes: browserResource };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
Histogram,
} from '@opentelemetry/sdk-metrics';
import { hrTimeToMilliseconds } from '@opentelemetry/core';
import { IResource } from '@opentelemetry/resources';
import { Resource } from '@opentelemetry/resources';

type PrometheusDataTypeLiteral =
| 'counter'
Expand Down Expand Up @@ -324,7 +324,7 @@ export class PrometheusSerializer {
return results;
}

protected _serializeResource(resource: IResource): string {
protected _serializeResource(resource: Resource): string {
const name = 'target_info';
const help = `# HELP ${name} Target metadata`;
const type = `# TYPE ${name} gauge`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
sdkVersion,
serviceName,
} from './util';
import { Resource } from '@opentelemetry/resources';
import { resourceFromAttributes } from '@opentelemetry/resources';
import { AggregationType } from '@opentelemetry/sdk-metrics';

const attributes = {
Expand Down Expand Up @@ -703,14 +703,12 @@ describe('PrometheusSerializer', () => {
it('should serialize resource', () => {
const serializer = new PrometheusSerializer(undefined, true);
const result = serializer['_serializeResource'](
new Resource({
attributes: {
env: 'prod',
hostname: 'myhost',
datacenter: 'sdc',
region: 'europe',
owner: 'frontend',
},
resourceFromAttributes({
env: 'prod',
hostname: 'myhost',
datacenter: 'sdc',
region: 'europe',
owner: 'frontend',
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as sinon from 'sinon';
import { Resource } from '@opentelemetry/resources';
import { DEFAULT_RESOURCE } from '@opentelemetry/resources';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE,
Expand All @@ -29,19 +29,23 @@ export function mockHrTime() {
sinon.useFakeTimers(mockedHrTimeMs);
}

export const serviceName = Resource.default()
.attributes[SEMRESATTRS_SERVICE_NAME]?.toString()
export const serviceName = DEFAULT_RESOURCE.attributes[
SEMRESATTRS_SERVICE_NAME
]?.toString()
.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\n');
export const sdkLanguage = Resource.default()
.attributes[SEMRESATTRS_TELEMETRY_SDK_LANGUAGE]?.toString()
export const sdkLanguage = DEFAULT_RESOURCE.attributes[
SEMRESATTRS_TELEMETRY_SDK_LANGUAGE
]?.toString()
.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\n');
export const sdkName = Resource.default()
.attributes[SEMRESATTRS_TELEMETRY_SDK_NAME]?.toString()
export const sdkName = DEFAULT_RESOURCE.attributes[
SEMRESATTRS_TELEMETRY_SDK_NAME
]?.toString()
.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\n');
export const sdkVersion = Resource.default()
.attributes[SEMRESATTRS_TELEMETRY_SDK_VERSION]?.toString()
export const sdkVersion = DEFAULT_RESOURCE.attributes[
SEMRESATTRS_TELEMETRY_SDK_VERSION
]?.toString()
.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\n');
15 changes: 7 additions & 8 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import {
registerInstrumentations,
} from '@opentelemetry/instrumentation';
import {
DEFAULT_RESOURCE,
detectResources,
envDetector,
hostDetector,
IResource,
processDetector,
Resource,
processDetector,
ResourceDetectionConfig,
ResourceDetector,
resourceFromAttributes,
} from '@opentelemetry/resources';
import {
LogRecordProcessor,
Expand Down Expand Up @@ -208,7 +209,7 @@ export class NodeSDK {
private _meterProviderConfig?: MeterProviderConfig;
private _instrumentations: Instrumentation[];

private _resource: IResource;
private _resource: Resource;
private _resourceDetectors: Array<ResourceDetector>;

private _autoDetectResources: boolean;
Expand Down Expand Up @@ -244,7 +245,7 @@ export class NodeSDK {

this._configuration = configuration;

this._resource = configuration.resource ?? Resource.default();
this._resource = configuration.resource ?? DEFAULT_RESOURCE;
this._autoDetectResources = configuration.autoDetectResources ?? true;
if (!this._autoDetectResources) {
this._resourceDetectors = [];
Expand Down Expand Up @@ -352,10 +353,8 @@ export class NodeSDK {
this._serviceName === undefined
? this._resource
: this._resource.merge(
new Resource({
attributes: {
[ATTR_SERVICE_NAME]: this._serviceName,
},
resourceFromAttributes({
[ATTR_SERVICE_NAME]: this._serviceName,
})
);

Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { ContextManager } from '@opentelemetry/api';
import { TextMapPropagator } from '@opentelemetry/api';
import { Instrumentation } from '@opentelemetry/instrumentation';
import { IResource, ResourceDetector } from '@opentelemetry/resources';
import { Resource, ResourceDetector } from '@opentelemetry/resources';
import { LogRecordProcessor } from '@opentelemetry/sdk-logs';
import { IMetricReader, ViewOptions } from '@opentelemetry/sdk-metrics';
import {
Expand All @@ -38,7 +38,7 @@ export interface NodeSDKConfiguration {
metricReader: IMetricReader;
views: ViewOptions[];
instrumentations: (Instrumentation | Instrumentation[])[];
resource: IResource;
resource: Resource;
resourceDetectors: Array<ResourceDetector>;
sampler: Sampler;
serviceName?: string;
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ import {
envDetector,
processDetector,
hostDetector,
Resource,
serviceInstanceIdDetector,
DetectedResource,
DEFAULT_RESOURCE,
} from '@opentelemetry/resources';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { logs, ProxyLoggerProvider } from '@opentelemetry/api-logs';
Expand Down Expand Up @@ -956,7 +956,7 @@ describe('Node SDK', () => {
const resource = sdk['_resource'];
await resource.waitForAsyncAttributes?.();

assert.deepStrictEqual(resource, Resource.default());
assert.deepStrictEqual(resource, DEFAULT_RESOURCE);
await sdk.shutdown();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { SDK_INFO } from '@opentelemetry/core';
import * as assert from 'assert';
import { IResource, Resource } from '@opentelemetry/resources';
import { Resource } from '@opentelemetry/resources';
import {
SEMRESATTRS_CLOUD_ACCOUNT_ID,
SEMRESATTRS_CLOUD_AVAILABILITY_ZONE,
Expand Down Expand Up @@ -260,7 +260,7 @@ export const assertTelemetrySDKResource = (
* @param validations validations for the resource attributes
*/
export const assertServiceResource = (
resource: IResource,
resource: Resource,
validations: {
name: string;
instanceId?: string;
Expand Down Expand Up @@ -362,7 +362,7 @@ const assertHasOneLabel = (prefix: string, resource: Resource): void => {
);
};

export const assertServiceInstanceIdIsUUID = (resource: IResource): void => {
export const assertServiceInstanceIdIsUUID = (resource: Resource): void => {
const UUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
assert.equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/** Properties of a Resource. */
export interface IResource {
export interface Resource {
/** Resource attributes */
attributes: IKeyValue[];

Expand Down
6 changes: 3 additions & 3 deletions experimental/packages/otlp-transformer/src/common/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import type {
IAnyValue,
IInstrumentationScope,
IKeyValue,
IResource,
Resource,
} from './internal-types';
import { Attributes } from '@opentelemetry/api';
import { InstrumentationScope } from '@opentelemetry/core';
import { IResource as ISdkResource } from '@opentelemetry/resources';
import { Resource as ISdkResource } from '@opentelemetry/resources';

export function createResource(resource: ISdkResource): IResource {
export function createResource(resource: ISdkResource): Resource {
return {
attributes: toAttributes(resource.attributes),
droppedAttributesCount: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
IAnyValue,
IInstrumentationScope,
IKeyValue,
IResource,
Resource,
} from '../common/internal-types';

/** Properties of an ExportLogsServiceRequest. */
Expand All @@ -31,7 +31,7 @@ export interface IExportLogsServiceRequest {
/** Properties of a ResourceLogs. */
export interface IResourceLogs {
/** ResourceLogs resource */
resource?: IResource;
resource?: Resource;

/** ResourceLogs scopeLogs */
scopeLogs: IScopeLogs[];
Expand Down
6 changes: 3 additions & 3 deletions experimental/packages/otlp-transformer/src/logs/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ILogRecord,
IResourceLogs,
} from './internal-types';
import { IResource } from '@opentelemetry/resources';
import { Resource } from '@opentelemetry/resources';
import { Encoder, getOtlpEncoder } from '../common/utils';
import {
createInstrumentationScope,
Expand All @@ -45,9 +45,9 @@ export function createExportLogsServiceRequest(

function createResourceMap(
logRecords: ReadableLogRecord[]
): Map<IResource, Map<string, ReadableLogRecord[]>> {
): Map<Resource, Map<string, ReadableLogRecord[]>> {
const resourceMap: Map<
IResource,
Resource,
Map<string, ReadableLogRecord[]>
> = new Map();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Fixed64,
IInstrumentationScope,
IKeyValue,
IResource,
Resource,
} from '../common/internal-types';

/** Properties of an ExportMetricsServiceRequest. */
Expand All @@ -29,7 +29,7 @@ export interface IExportMetricsServiceRequest {
/** Properties of a ResourceMetrics. */
export interface IResourceMetrics {
/** ResourceMetrics resource */
resource?: IResource;
resource?: Resource;

/** ResourceMetrics scopeMetrics */
scopeMetrics: IScopeMetrics[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Fixed64,
IInstrumentationScope,
IKeyValue,
IResource,
Resource,
} from '../common/internal-types';

/** Properties of an ExportTraceServiceRequest. */
Expand All @@ -30,7 +30,7 @@ export interface IExportTraceServiceRequest {
/** Properties of a ResourceSpans. */
export interface IResourceSpans {
/** ResourceSpans resource */
resource?: IResource;
resource?: Resource;

/** ResourceSpans scopeSpans */
scopeSpans: IScopeSpans[];
Expand Down
4 changes: 2 additions & 2 deletions experimental/packages/otlp-transformer/src/trace/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import type { Link } from '@opentelemetry/api';
import { IResource } from '@opentelemetry/resources';
import { Resource } from '@opentelemetry/resources';
import type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
import type { Encoder } from '../common/utils';
import {
Expand Down Expand Up @@ -112,7 +112,7 @@ export function createExportTraceServiceRequest(
}

function createResourceMap(readableSpans: ReadableSpan[]) {
const resourceMap: Map<IResource, Map<string, ReadableSpan[]>> = new Map();
const resourceMap: Map<Resource, Map<string, ReadableSpan[]>> = new Map();
for (const record of readableSpans) {
let ilsMap = resourceMap.get(record.resource);

Expand Down
Loading

0 comments on commit 4b8ae0c

Please sign in to comment.