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

Add tracing suppresing for Metrics Export #3332

Merged
merged 20 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(metrics-sdk): Add tracing suppresing for Metrics Export [#3332](https://github.com/open-telemetry/opentelemetry-js/pull/3332) @hectorhdzg
* feat(instrumentation): implement `require-in-the-middle` singleton [#3161](https://github.com/open-telemetry/opentelemetry-js/pull/3161) @mhassan1
* feat(sdk-node): configure trace exporter with environment variables [#3143](https://github.com/open-telemetry/opentelemetry-js/pull/3143) @svetlanabrennan
* feat: enable tree shaking [#3329](https://github.com/open-telemetry/opentelemetry-js/pull/3329) @pkanal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as api from '@opentelemetry/api';
import {
internal,
ExportResultCode,
globalErrorHandler,
unrefTimer
Expand Down Expand Up @@ -85,20 +86,12 @@ export class PeriodicExportingMetricReader extends MetricReader {
api.diag.error('PeriodicExportingMetricReader: metrics collection errors', ...errors);
}

return new Promise((resolve, reject) => {
this._exporter.export(resourceMetrics, result => {
if (result.code !== ExportResultCode.SUCCESS) {
reject(
result.error ??
new Error(
`PeriodicExportingMetricReader: metrics export failed (error ${result.error})`
)
);
} else {
resolve();
}
});
});
const result = await internal._export(this._exporter, resourceMetrics);
if (result.code !== ExportResultCode.SUCCESS) {
throw new Error(
`PeriodicExportingMetricReader: metrics export failed (error ${result.error})`
);
}
}

protected override onInitialized(): void {
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export * from './utils/url';
export * from './utils/wrap';
export * from './utils/callback';
export * from './version';
import { _export } from './internal/exporter';
export const internal = {
_export
};
38 changes: 38 additions & 0 deletions packages/opentelemetry-core/src/internal/exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { context } from '@opentelemetry/api';
import { ExportResult } from '../ExportResult';
import { suppressTracing } from '../trace/suppress-tracing';

export interface Exporter<T> {
export(arg: T, resultCallback: (result: ExportResult) => void): void;
}

/**
* @internal
* Shared functionality used by Exporters while exporting data, including suppresion of Traces.
*/
legendecas marked this conversation as resolved.
Show resolved Hide resolved
export function _export<T>(exporter: Exporter<T>, arg: T): Promise<ExportResult> {
return new Promise(resolve => {
// prevent downstream exporter calls from generating spans
context.with(suppressTracing(context.active()), () => {
exporter.export(arg, (result: ExportResult) => {
resolve(result);
});
});
});
}
43 changes: 43 additions & 0 deletions packages/opentelemetry-core/test/internal/exporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import * as sinon from 'sinon';
import { ExportResult, ExportResultCode } from '../../src';
import * as suppress from '../../src/trace/suppress-tracing';
import { _export } from '../../src/internal/exporter';

describe('exporter', () => {
const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
});

class TestExporter {
export(arg: any, resultCallback: (result: ExportResult) => void) {
resultCallback({ code: ExportResultCode.SUCCESS });
}
}

it('_export should suppress tracing', async () => {
const suppressSpy = sandbox.spy(suppress, 'suppressTracing');
const exporter = new TestExporter();
const result = await _export(exporter, ['Test1']);
assert.strictEqual(result.code, ExportResultCode.SUCCESS);
assert.ok(suppressSpy.calledOnce);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* limitations under the License.
*/

import { context, Context, TraceFlags } from '@opentelemetry/api';
import { Context, TraceFlags } from '@opentelemetry/api';
import {
internal,
ExportResultCode,
globalErrorHandler,
suppressTracing,
BindOnceFuture,
ExportResult
} from '@opentelemetry/core';
import { Span } from '../Span';
import { SpanProcessor } from '../SpanProcessor';
Expand All @@ -45,7 +46,7 @@ export class SimpleSpanProcessor implements SpanProcessor {
}

// does nothing.
onStart(_span: Span, _parentContext: Context): void {}
onStart(_span: Span, _parentContext: Context): void { }

onEnd(span: ReadableSpan): void {
if (this._shutdownOnce.isCalled) {
Expand All @@ -56,18 +57,17 @@ export class SimpleSpanProcessor implements SpanProcessor {
return;
}

// prevent downstream exporter calls from generating spans
context.with(suppressTracing(context.active()), () => {
this._exporter.export([span], result => {
if (result.code !== ExportResultCode.SUCCESS) {
globalErrorHandler(
result.error ??
new Error(
`SimpleSpanProcessor: span export failed (status ${result})`
)
);
}
});
internal._export(this._exporter, [span]).then((result: ExportResult) => {
if (result.code !== ExportResultCode.SUCCESS) {
globalErrorHandler(
result.error ??
new Error(
`SimpleSpanProcessor: span export failed (status ${result})`
)
);
}
}).catch(error => {
globalErrorHandler(error);
});
}

Expand Down