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

Sync SDK - Async resource detection #1484

Closed
wants to merge 12 commits into from
12 changes: 6 additions & 6 deletions packages/opentelemetry-exporter-zipkin/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const DUMMY_RESOURCE = new Resource({

describe('transform', () => {
describe('toZipkinSpan', () => {
it('should convert an OpenTelemetry span to a Zipkin span', () => {
it('should convert an OpenTelemetry span to a Zipkin span', async () => {
const span = new Span(
tracer,
'my-span',
Expand All @@ -68,7 +68,7 @@ describe('transform', () => {
span.end();

const zipkinSpan = toZipkinSpan(
span,
await span.toReadableSpan(),
'my-service',
statusCodeTagName,
statusDescriptionTagName
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('transform', () => {
traceId: span.spanContext.traceId,
});
});
it("should skip parentSpanId if doesn't exist", () => {
it("should skip parentSpanId if doesn't exist", async () => {
const span = new Span(
tracer,
'my-span',
Expand All @@ -112,7 +112,7 @@ describe('transform', () => {
span.end();

const zipkinSpan = toZipkinSpan(
span,
await span.toReadableSpan(),
'my-service',
statusCodeTagName,
statusDescriptionTagName
Expand Down Expand Up @@ -149,12 +149,12 @@ describe('transform', () => {
].forEach(item =>
it(`should map OpenTelemetry SpanKind ${
api.SpanKind[item.ot]
} to Zipkin ${item.zipkin}`, () => {
} to Zipkin ${item.zipkin}`, async () => {
const span = new Span(tracer, 'my-span', spanContext, item.ot);
span.end();

const zipkinSpan = toZipkinSpan(
span,
await span.toReadableSpan(),
'my-service',
statusCodeTagName,
statusDescriptionTagName
Expand Down
8 changes: 5 additions & 3 deletions packages/opentelemetry-grpc-utils/test/grpcUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,12 @@ export const runTests = (
const args = [client, method.request, method.metadata];
await (method.method as any)
.apply({}, args)
.then((result: TestRequestResponse | TestRequestResponse[]) => {
.then(async (result: TestRequestResponse | TestRequestResponse[]) => {
assert.ok(
checkEqual(result)(method.result),
'gRPC call returns correct values'
);
await new Promise(resolve => setTimeout(resolve));
const spans = memoryExporter.getFinishedSpans();
if (checkSpans) {
const incomingSpan = spans[0];
Expand Down Expand Up @@ -516,7 +517,7 @@ export const runTests = (
});
});

it(`should raise an error for client childSpan/server rootSpan - ${method.description} - status = OK`, () => {
it(`should raise an error for client childSpan/server rootSpan - ${method.description} - status = OK`, async () => {
const expectEmpty = memoryExporter.getFinishedSpans();
assert.strictEqual(expectEmpty.length, 0);

Expand All @@ -533,9 +534,10 @@ export const runTests = (
const args = [client, method.request, method.metadata];
await (method.method as any)
.apply({}, args)
.then(() => {
.then(async () => {
// Assert
if (checkSpans) {
await new Promise(resolve => setTimeout(resolve));
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 2);
const serverSpan = spans[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/BaseObserverMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export abstract class BaseObserverMetric
name: string,
options: api.MetricOptions,
private readonly _batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
metricKind: MetricKind,
instrumentationLibrary: InstrumentationLibrary,
callback?: (observerResult: api.ObserverResult) => unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class BatchObserverMetric
name: string,
options: api.BatchMetricOptions,
private readonly _batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary,
callback?: (observerResult: api.BatchObserverResult) => void
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/CounterMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class CounterMetric extends Metric<BoundCounter> implements api.Counter {
name: string,
options: api.MetricOptions,
private readonly _batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary
) {
super(name, options, MetricKind.COUNTER, resource, instrumentationLibrary);
Expand Down
6 changes: 4 additions & 2 deletions packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Meter implements api.Meter {
private readonly _logger: api.Logger;
private readonly _metrics = new Map<string, Metric<BaseBoundInstrument>>();
private readonly _batcher: Batcher;
private readonly _resource: Resource;
private readonly _resource: Promise<Resource>;
private readonly _instrumentationLibrary: InstrumentationLibrary;
private readonly _controller: PushController;

Expand All @@ -51,7 +51,9 @@ export class Meter implements api.Meter {
) {
this._logger = config.logger || new ConsoleLogger(config.logLevel);
this._batcher = config.batcher ?? new UngroupedBatcher();
this._resource = config.resource || Resource.createTelemetrySDKResource();
this._resource = Promise.resolve(
config.resource || Resource.createTelemetrySDKResource()
);
this._instrumentationLibrary = instrumentationLibrary;
// start the push controller
const exporter = config.exporter || new NoopExporter();
Expand Down
6 changes: 4 additions & 2 deletions packages/opentelemetry-metrics/src/MeterProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export class MeterProvider implements api.MeterProvider {
private readonly _config: MeterConfig;
private readonly _meters: Map<string, Meter> = new Map();
private _cleanNotifyOnGlobalShutdown: Function | undefined;
readonly resource: Resource;
readonly resource: Promise<Resource>;
readonly logger: api.Logger;

constructor(config: MeterConfig = DEFAULT_CONFIG) {
this.logger = config.logger ?? new ConsoleLogger(config.logLevel);
this.resource = config.resource ?? Resource.createTelemetrySDKResource();
this.resource = Promise.resolve(
config.resource ?? Resource.createTelemetrySDKResource()
);
this._config = Object.assign({}, config, {
logger: this.logger,
resource: this.resource,
Expand Down
22 changes: 10 additions & 12 deletions packages/opentelemetry-metrics/src/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export abstract class Metric<T extends BaseBoundInstrument>
private readonly _name: string,
private readonly _options: api.MetricOptions,
private readonly _kind: MetricKind,
readonly resource: Resource,
readonly resource: Promise<Resource>,
readonly instrumentationLibrary: InstrumentationLibrary
) {
this._disabled = !!_options.disabled;
Expand Down Expand Up @@ -78,17 +78,15 @@ export abstract class Metric<T extends BaseBoundInstrument>
}

getMetricRecord(): Promise<MetricRecord[]> {
return new Promise(resolve => {
resolve(
Array.from(this._instruments.values()).map(instrument => ({
descriptor: this._descriptor,
labels: instrument.getLabels(),
aggregator: instrument.getAggregator(),
resource: this.resource,
instrumentationLibrary: this.instrumentationLibrary,
}))
);
});
return this.resource.then(resource =>
Array.from(this._instruments.values()).map(instrument => ({
descriptor: this._descriptor,
labels: instrument.getLabels(),
aggregator: instrument.getAggregator(),
resource,
instrumentationLibrary: this.instrumentationLibrary,
}))
);
}

private _getMetricDescriptor(): MetricDescriptor {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/SumObserverMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class SumObserverMetric
name: string,
options: api.MetricOptions,
batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary,
callback?: (observerResult: api.ObserverResult) => unknown
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class UpDownCounterMetric
name: string,
options: api.MetricOptions,
private readonly _batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary
) {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class UpDownSumObserverMetric
name: string,
options: api.MetricOptions,
batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary,
callback?: (observerResult: api.ObserverResult) => unknown
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ValueObserverMetric
name: string,
options: api.MetricOptions,
batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary,
callback?: (observerResult: api.ObserverResult) => unknown
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ValueRecorderMetric
name: string,
options: api.MetricOptions,
private readonly _batcher: Batcher,
resource: Resource,
resource: Promise<Resource>,
instrumentationLibrary: InstrumentationLibrary
) {
super(
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-metrics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface MeterConfig {
interval?: number;

/** Resource associated with metric telemetry */
resource?: Resource;
resource?: Resource | Promise<Resource>;

/** Metric batcher. */
batcher?: Batcher;
Expand Down
12 changes: 6 additions & 6 deletions packages/opentelemetry-metrics/test/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Meter', () => {

it('should pipe through resource', async () => {
const counter = meter.createCounter('name') as CounterMetric;
assert.ok(counter.resource instanceof Resource);
assert.ok((await counter.resource) instanceof Resource);

counter.add(1, { foo: 'bar' });

Expand Down Expand Up @@ -349,7 +349,7 @@ describe('Meter', () => {
const upDownCounter = meter.createUpDownCounter(
'name'
) as UpDownCounterMetric;
assert.ok(upDownCounter.resource instanceof Resource);
assert.ok((await upDownCounter.resource) instanceof Resource);

upDownCounter.add(1, { foo: 'bar' });

Expand Down Expand Up @@ -559,7 +559,7 @@ describe('Meter', () => {
const valueRecorder = meter.createValueRecorder(
'name'
) as ValueRecorderMetric;
assert.ok(valueRecorder.resource instanceof Resource);
assert.ok((await valueRecorder.resource) instanceof Resource);

valueRecorder.record(1, { foo: 'bar' });

Expand Down Expand Up @@ -869,7 +869,7 @@ describe('Meter', () => {
result.observe(42, { foo: 'bar' });
return Promise.resolve();
}) as SumObserverMetric;
assert.ok(sumObserver.resource instanceof Resource);
assert.ok((await sumObserver.resource) instanceof Resource);

const [record] = await sumObserver.getMetricRecord();
assert.ok(record.resource instanceof Resource);
Expand Down Expand Up @@ -950,7 +950,7 @@ describe('Meter', () => {
const valueObserver = meter.createValueObserver('name', {}, result => {
result.observe(42, { foo: 'bar' });
}) as ValueObserverMetric;
assert.ok(valueObserver.resource instanceof Resource);
assert.ok((await valueObserver.resource) instanceof Resource);

const [record] = await valueObserver.getMetricRecord();
assert.ok(record.resource instanceof Resource);
Expand Down Expand Up @@ -1093,7 +1093,7 @@ describe('Meter', () => {
return Promise.resolve();
}
) as UpDownSumObserverMetric;
assert.ok(upDownSumObserver.resource instanceof Resource);
assert.ok((await upDownSumObserver.resource) instanceof Resource);

const [record] = await upDownSumObserver.getMetricRecord();
assert.ok(record.resource instanceof Resource);
Expand Down
7 changes: 4 additions & 3 deletions packages/opentelemetry-node/test/NodeTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,16 @@ describe('NodeTracerProvider', () => {
assert.strictEqual(span.isRecording(), true);
});

it('should assign resource to span', () => {
it('should assign resource to span', async () => {
provider = new NodeTracerProvider({
logger: new NoopLogger(),
});
const span = provider.getTracer('default').startSpan('my-span') as Span;
assert.ok(span);
assert.ok(span.resource instanceof Resource);
const resource = await span.resource;
assert.ok(resource instanceof Resource);
assert.equal(
span.resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE],
resource.attributes[TELEMETRY_SDK_RESOURCE.LANGUAGE],
'nodejs'
);
});
Expand Down
Loading