Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-*)!: align merge resource behavior with spec #5219

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* refactor(sdk-trace-base)!: remove `new Span` constructor in favor of `Tracer.startSpan` API [#5048](https://github.com/open-telemetry/opentelemetry-js/pull/5048) @david-luna
* refactor(sdk-trace-base)!: remove `BasicTracerProvider.addSpanProcessor` API in favor of constructor options. [#5134](https://github.com/open-telemetry/opentelemetry-js/pull/5134) @david-luna
* refactor(sdk-trace-base)!: make `resource` property private in `BasicTracerProvider` and remove `getActiveSpanProcessor` API. [#5192](https://github.com/open-telemetry/opentelemetry-js/pull/5192) @david-luna
* refactor!: align merge resource behavior with spec in all SDKs. [#5219](https://github.com/open-telemetry/opentelemetry-js/pull/5219) @david-luna
david-luna marked this conversation as resolved.
Show resolved Hide resolved

### :rocket: (Enhancement)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ const udsAddr = 'unix:///tmp/otlp-metrics.sock';
type TestParams = {
Copy link
Member

@pichlermarc pichlermarc Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I actually removed all of these tests on main already. I'll open a PR to sync main to next.
It's time again to do so anyway.

address?: string;
useTLS?: boolean;
useDefaultResource?: boolean;
metadata?: grpc.Metadata;
};

const metadata = new grpc.Metadata();
metadata.set('k', 'v');

const testOTLPMetricExporter = (params: TestParams) => {
const { address = httpAddr, useTLS, metadata } = params;
const { address = httpAddr, useTLS, metadata, useDefaultResource } = params;
return describe(`OTLPMetricExporter - node ${
useTLS ? 'with' : 'without'
} TLS, ${metadata ? 'with' : 'without'} metadata, target ${address}`, () => {
Expand Down Expand Up @@ -143,7 +144,7 @@ const testOTLPMetricExporter = (params: TestParams) => {
temporalityPreference: AggregationTemporalityPreference.CUMULATIVE,
});

setUp();
setUp(!!useDefaultResource);

const counter = mockCounter();
mockObservableGauge(observableResult => {
Expand Down Expand Up @@ -290,7 +291,7 @@ const testOTLPMetricExporter = (params: TestParams) => {
['0', '2', '0']
);
assert.ok(typeof resource !== 'undefined', "resource doesn't exist");
ensureResourceIsCorrect(resource);
ensureResourceIsCorrect(resource, !!useDefaultResource);

ensureMetadataIsCorrect(reqMetadata, metadata);

Expand All @@ -304,5 +305,6 @@ const testOTLPMetricExporter = (params: TestParams) => {
testOTLPMetricExporter({ useTLS: true });
testOTLPMetricExporter({ useTLS: false });
testOTLPMetricExporter({ metadata });
testOTLPMetricExporter({ useDefaultResource: true });
// skip UDS tests on windows
process.platform !== 'win32' && testOTLPMetricExporter({ address: udsAddr });
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export async function collect() {
return (await reader.collect())!;
}

export function setUp() {
export function setUp(useDefaultResource: boolean) {
reader = new TestMetricReader();
meterProvider = new MeterProvider({
resource: testResource,
resource: useDefaultResource ? undefined : testResource,
views: [
{
aggregation: {
Expand Down Expand Up @@ -211,61 +211,72 @@ export function ensureExportedHistogramIsCorrect(
assert.deepStrictEqual(dp.explicitBounds, explicitBounds);
}

export function ensureResourceIsCorrect(resource: IResource) {
assert.deepStrictEqual(resource, {
attributes: [
{
key: 'service.name',
value: {
stringValue: `unknown_service:${process.argv0}`,
value: 'stringValue',
export function ensureResourceIsCorrect(
resource: IResource,
checkDefault: boolean
) {
if (checkDefault) {
assert.deepStrictEqual(resource, {
attributes: [
{
key: 'service.name',
value: {
stringValue: `unknown_service:${process.argv0}`,
value: 'stringValue',
},
},
},
{
key: 'telemetry.sdk.language',
value: {
stringValue: 'nodejs',
value: 'stringValue',
{
key: 'telemetry.sdk.language',
value: {
stringValue: 'nodejs',
value: 'stringValue',
},
},
},
{
key: 'telemetry.sdk.name',
value: {
stringValue: 'opentelemetry',
value: 'stringValue',
{
key: 'telemetry.sdk.name',
value: {
stringValue: 'opentelemetry',
value: 'stringValue',
},
},
},
{
key: 'telemetry.sdk.version',
value: {
stringValue: VERSION,
value: 'stringValue',
{
key: 'telemetry.sdk.version',
value: {
stringValue: VERSION,
value: 'stringValue',
},
},
},
{
key: 'service',
value: {
stringValue: 'ui',
value: 'stringValue',
],
droppedAttributesCount: 0,
});
} else {
assert.deepStrictEqual(resource, {
attributes: [
{
key: 'service',
value: {
stringValue: 'ui',
value: 'stringValue',
},
},
},
{
key: 'version',
value: {
intValue: '1',
value: 'intValue',
{
key: 'version',
value: {
intValue: '1',
value: 'intValue',
},
},
},
{
key: 'cost',
value: {
doubleValue: 112.12,
value: 'doubleValue',
{
key: 'cost',
value: {
doubleValue: 112.12,
value: 'doubleValue',
},
},
},
],
droppedAttributesCount: 0,
});
],
droppedAttributesCount: 0,
});
}
}

export function ensureMetadataIsCorrect(
Expand Down
6 changes: 0 additions & 6 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export class NodeSDK {

private _resource: IResource;
private _resourceDetectors: Array<Detector | DetectorSync>;
private _mergeResourceWithDefaults: boolean;

private _autoDetectResources: boolean;

Expand Down Expand Up @@ -139,8 +138,6 @@ export class NodeSDK {
this._configuration = configuration;

this._resource = configuration.resource ?? new Resource({});
this._mergeResourceWithDefaults =
configuration.mergeResourceWithDefaults ?? true;
this._autoDetectResources = configuration.autoDetectResources ?? true;
if (!this._autoDetectResources) {
this._resourceDetectors = [];
Expand Down Expand Up @@ -263,7 +260,6 @@ export class NodeSDK {
this._tracerProvider = new NodeTracerProvider({
...this._configuration,
resource: this._resource,
mergeResourceWithDefaults: this._mergeResourceWithDefaults,
spanProcessors,
});

Expand All @@ -281,7 +277,6 @@ export class NodeSDK {
if (this._loggerProviderConfig) {
const loggerProvider = new LoggerProvider({
resource: this._resource,
mergeResourceWithDefaults: this._mergeResourceWithDefaults,
});

for (const logRecordProcessor of this._loggerProviderConfig
Expand All @@ -303,7 +298,6 @@ export class NodeSDK {
resource: this._resource,
views: this._meterProviderConfig?.views ?? [],
readers: readers,
mergeResourceWithDefaults: this._mergeResourceWithDefaults,
});

this._meterProvider = meterProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export interface NodeSDKConfiguration {
instrumentations: (Instrumentation | Instrumentation[])[];
resource: IResource;
resourceDetectors: Array<Detector | DetectorSync>;
mergeResourceWithDefaults?: boolean;
sampler: Sampler;
serviceName?: string;
/** @deprecated use spanProcessors instead*/
Expand Down
17 changes: 1 addition & 16 deletions experimental/packages/sdk-logs/src/LoggerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,13 @@ import { LoggerProviderSharedState } from './internal/LoggerProviderSharedState'

export const DEFAULT_LOGGER_NAME = 'unknown';

function prepareResource(
mergeWithDefaults: boolean,
providedResource: Resource | undefined
) {
const resource = providedResource ?? Resource.empty();

if (mergeWithDefaults) {
return Resource.default().merge(resource);
}
return resource;
}

export class LoggerProvider implements logsAPI.LoggerProvider {
private _shutdownOnce: BindOnceFuture<void>;
private readonly _sharedState: LoggerProviderSharedState;

constructor(config: LoggerProviderConfig = {}) {
const mergedConfig = merge({}, loadDefaultConfig(), config);
const resource = prepareResource(
mergedConfig.mergeResourceWithDefaults,
config.resource
);
const resource = config.resource ?? Resource.default();
this._sharedState = new LoggerProviderSharedState(
resource,
mergedConfig.forceFlushTimeoutMillis,
Expand Down
1 change: 0 additions & 1 deletion experimental/packages/sdk-logs/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function loadDefaultConfig() {
attributeCountLimit: getEnv().OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT,
},
includeTraceContext: true,
mergeResourceWithDefaults: true,
};
}

Expand Down
6 changes: 0 additions & 6 deletions experimental/packages/sdk-logs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ export interface LoggerProviderConfig {

/** Log Record Limits*/
logRecordLimits?: LogRecordLimits;

/**
* Merge resource with {@link Resource.default()}?
* Default: {@code true}
*/
mergeResourceWithDefaults?: boolean;
}

export interface LogRecordLimits {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,15 @@ describe('LoggerProvider', () => {
assert.deepStrictEqual(resource, Resource.default());
});

it('should fallback to default resource attrs', () => {
const passedInResource = new Resource({ foo: 'bar' });
const provider = new LoggerProvider({ resource: passedInResource });
const { resource } = provider['_sharedState'];
assert.deepStrictEqual(
resource,
Resource.default().merge(passedInResource)
);
});

it('should not merge with default resource attrs when flag is set to false', function () {
it('should not have default resource if passed', function () {
const passedInResource = new Resource({ foo: 'bar' });
const provider = new LoggerProvider({
resource: passedInResource,
mergeResourceWithDefaults: false,
});
const { resource } = provider['_sharedState'];
assert.deepStrictEqual(resource, passedInResource);
});

it('should merge with default resource attrs when flag is set to true', function () {
const passedInResource = new Resource({ foo: 'bar' });
const provider = new LoggerProvider({
resource: passedInResource,
mergeResourceWithDefaults: true,
});
const { resource } = provider['_sharedState'];
assert.deepStrictEqual(
resource,
Resource.default().merge(passedInResource)
);
});

it('should have default forceFlushTimeoutMillis if not pass', () => {
const provider = new LoggerProvider();
const sharedState = provider['_sharedState'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ export class BasicTracerProvider implements TracerProvider {
loadDefaultConfig(),
reconfigureLimits(config)
);
this._resource = mergedConfig.resource ?? Resource.empty();

if (mergedConfig.mergeResourceWithDefaults) {
this._resource = Resource.default().merge(this._resource);
}
this._resource = mergedConfig.resource ?? Resource.default();

this._config = Object.assign({}, mergedConfig, {
resource: this._resource,
Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export function loadDefaultConfig() {
_env.OTEL_SPAN_ATTRIBUTE_PER_EVENT_COUNT_LIMIT,
attributePerLinkCountLimit: _env.OTEL_SPAN_ATTRIBUTE_PER_LINK_COUNT_LIMIT,
},
mergeResourceWithDefaults: true,
};
}

Expand Down
6 changes: 0 additions & 6 deletions packages/opentelemetry-sdk-trace-base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ export interface TracerConfig {
/** Span Limits */
spanLimits?: SpanLimits;

/**
* Merge resource with {@link Resource.default()}?
* Default: {@code true}
**/
mergeResourceWithDefaults?: boolean;

/** Resource associated with trace telemetry */
resource?: IResource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,25 +932,12 @@ describe('BasicTracerProvider', () => {
assert.deepStrictEqual(tracerProvider['_resource'], Resource.default());
});

it('should not merge with defaults when flag is set to false', function () {
const expectedResource = new Resource({ foo: 'bar' });
const tracerProvider = new BasicTracerProvider({
mergeResourceWithDefaults: false,
resource: expectedResource,
});
assert.deepStrictEqual(tracerProvider['_resource'], expectedResource);
});

it('should merge with defaults when flag is set to true', function () {
it('should use not use the default if resource passed', function () {
const providedResource = new Resource({ foo: 'bar' });
const tracerProvider = new BasicTracerProvider({
mergeResourceWithDefaults: true,
resource: providedResource,
});
assert.deepStrictEqual(
tracerProvider['_resource'],
Resource.default().merge(providedResource)
);
assert.deepStrictEqual(tracerProvider['_resource'], providedResource);
});
});

Expand Down
Loading
Loading