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 5 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

### :bug: (Bug Fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as api from '@opentelemetry/api';
import {
ExportResultCode,
coreExport,
globalErrorHandler,
unrefTimer
} from '@opentelemetry/core';
Expand Down Expand Up @@ -86,7 +87,7 @@ export class PeriodicExportingMetricReader extends MetricReader {
}

return new Promise((resolve, reject) => {
this._exporter.export(resourceMetrics, result => {
coreExport(this._exporter, resourceMetrics, result => {
if (result.code !== ExportResultCode.SUCCESS) {
reject(
result.error ??
Expand Down
30 changes: 30 additions & 0 deletions packages/opentelemetry-core/src/Exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
legendecas marked this conversation as resolved.
Show resolved Hide resolved
* 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;
}

export function coreExport<T>(exporter: Exporter<T>, arg: T, resultCallback: (result: ExportResult) => void): void {
Copy link
Member

Choose a reason for hiding this comment

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

coreExport is sort of an odd name for a function in core I think. Also can you please add tsdocs since it is very non-obvious what this does to an outsider.

Copy link
Member

Choose a reason for hiding this comment

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

I worry a little bit about adding yet more API surface to the core module. I was hoping to eventually phase out the core module completely since it seems to just be a dumping ground for things. There also are other considerations beyond just suppressing tracing on export. We may for example want to suppress other signals like metrics.

Copy link
Member

Choose a reason for hiding this comment

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

We should have a dedicated module to share common snippets across SDK packages. I find the problem with the @opentelemetry/core is that the APIs that are supposed to be internal are exposed as public interfaces. Would it make sense to you to explicitly document this method as an internal interface so that we don't provide any API stability guarantees? Or should we create a new internal util package?

Copy link
Member

Choose a reason for hiding this comment

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

Seems like a reasonable compromise to document this as internal/unstable. IMO all of core should have been internal but it's too late for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added internal tsdoc, under score is usually added at the beginning of the name of the function, I'm not sure if you are using this pattern in this repo but let me know if that is a problem https://tsdoc.org/pages/tags/internal/
@dyladan is Metrics signal suppression already defined or implemented somewhere in OpenTelemetry?, trace suppression should make the generated Spans to be "not recording" ones, those should not be considered for metrics, Ex. HTTP duration metrics.

Copy link
Member

@legendecas legendecas Oct 17, 2022

Choose a reason for hiding this comment

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

Seems like a reasonable compromise to document this as internal/unstable. IMO all of core should have been internal but it's too late for that.

I don't think "all" of them are internal. I suppose APIs like CompositePropagator are public -- as there is no good place to export them.

// prevent downstream exporter calls from generating spans
context.with(suppressTracing(context.active()), () => {
exporter.export(arg, resultCallback);
});
}
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './common/global-error-handler';
export * from './common/logging-error-handler';
export * from './common/time';
export * from './common/types';
export * from './Exporter';
export * from './ExportResult';
export * from './version';
export * as baggageUtils from './baggage/utils';
Expand Down
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 {
ExportResultCode,
globalErrorHandler,
suppressTracing,
BindOnceFuture,
coreExport,
} 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})`
)
);
}
});
coreExport(this._exporter, [span], result => {
if (result.code !== ExportResultCode.SUCCESS) {
globalErrorHandler(
result.error ??
new Error(
`SimpleSpanProcessor: span export failed (status ${result})`
)
);
}
});
}

Expand Down