Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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(sdk-node): configure trace exporter with environment variables [#3143](https://github.com/open-telemetry/opentelemetry-js/pull/3143) @svetlanabrennan
* feat(prometheus): serialize resource as target_info gauge [#3300](https://github.com/open-telemetry/opentelemetry-js/pull/3300) @pichlermarc

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 @@ -86,7 +87,7 @@ export class PeriodicExportingMetricReader extends MetricReader {
}

return new Promise((resolve, reject) => {
this._exporter.export(resourceMetrics, result => {
internal._export(this._exporter, resourceMetrics, result => {
if (result.code !== ExportResultCode.SUCCESS) {
reject(
result.error ??
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
};
33 changes: 33 additions & 0 deletions packages/opentelemetry-core/src/internal/exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/
export function _export<T>(exporter: Exporter<T>, arg: T, resultCallback: (result: ExportResult) => void): void {
// prevent downstream exporter calls from generating spans
context.with(suppressTracing(context.active()), () => {
exporter.export(arg, resultCallback);
});
}
44 changes: 44 additions & 0 deletions packages/opentelemetry-core/test/internal/exporter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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', done => {
const suppressSpy = sandbox.spy(suppress, 'suppressTracing');
const exporter = new TestExporter();
_export(exporter, ['Test1'], (result: ExportResult) => {
assert.ok(suppressSpy.calledOnce);
done();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import { context, Context, TraceFlags } from '@opentelemetry/api';
import { Context, TraceFlags } from '@opentelemetry/api';
import {
internal,
ExportResultCode,
globalErrorHandler,
suppressTracing,
BindOnceFuture,
BindOnceFuture
} from '@opentelemetry/core';
import { Span } from '../Span';
import { SpanProcessor } from '../SpanProcessor';
Expand All @@ -45,7 +45,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 +56,15 @@ 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], result => {
if (result.code !== ExportResultCode.SUCCESS) {
globalErrorHandler(
result.error ??
new Error(
`SimpleSpanProcessor: span export failed (status ${result})`
)
);
}
});
}

Expand Down