Skip to content

Commit

Permalink
remove bad commit and address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaskarbanerjee committed Nov 20, 2024
1 parent 0ee9a27 commit 67ad4da
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 4 deletions.
3 changes: 3 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ This is an alternative to programmatically configuring an exporter or span proce
| OTEL_EXPORTER_OTLP_TRACES_PROTOCOL | The transport protocol to use on OTLP trace requests. Options include `grpc`, `http/protobuf`, and `http/json`. Default is `http/protobuf`. |
| OTEL_EXPORTER_OTLP_METRICS_PROTOCOL | The transport protocol to use on OTLP metric requests. Options include `grpc`, `http/protobuf`, and `http/json`. Default is `http/protobuf`. |
| OTEL_EXPORTER_OTLP_LOGS_PROTOCOL | The transport protocol to use on OTLP log requests. Options include `grpc`, `http/protobuf`, and `http/json`. Default is `http/protobuf`. |
| OTEL_METRICS_EXPORTER | Metrics exporter to be used. options are `otlp`, `prometheus`, `console` or `none`. |
| OTEL_METRIC_EXPORT_INTERVAL | The export interval when using a push Metric Reader. Default is `60000`.. |
| OTEL_METRIC_EXPORT_TIMEOUT | The export timeout when using a push Metric Reader. Default is `30000`.. |

Additionally, you can specify other applicable environment variables that apply to each exporter such as the following:

Expand Down
126 changes: 122 additions & 4 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ import {
import { OTLPLogExporter as OTLPHttpLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
import { OTLPLogExporter as OTLPGrpcLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc';
import { OTLPLogExporter as OTLPProtoLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
import { MeterProvider, MetricReader, View } from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter as OTLPGrpcMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { OTLPMetricExporter as OTLPProtoMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { OTLPMetricExporter as OTLPHttpMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { PrometheusExporter as PrometheusMetricExporter } from '@opentelemetry/exporter-prometheus';
import {
MeterProvider,
MetricReader,
View,
ConsoleMetricExporter,
PeriodicExportingMetricReader,
} from '@opentelemetry/sdk-metrics';
import {
BatchSpanProcessor,
SpanProcessor,
Expand Down Expand Up @@ -86,6 +96,108 @@ export type LoggerProviderConfig = {
logRecordProcessors: LogRecordProcessor[];
};

/**
* @Returns param value, if set else returns the default value
*/
function getValueInMillis(envName: string, defaultValue: number): number {
return parseInt(process.env[envName] || '') || defaultValue;
}

/**
*
* @returns MetricReader[] if appropriate environment variables are configured
*/
function configureMetricProviderFromEnv(): MetricReader[] {
const metricReaders: MetricReader[] = [];
const metricsExporterList = process.env.OTEL_METRICS_EXPORTER?.trim();
if (!metricsExporterList) {
return metricReaders;
}
const enabledExporters = filterBlanksAndNulls(metricsExporterList.split(','));

if (enabledExporters.length === 0) {
diag.info('OTEL_METRICS_EXPORTER is empty. Using default otlp exporter.');
}

if (enabledExporters.includes('none')) {
diag.info(
`OTEL_METRICS_EXPORTER contains "none". Metric provider will not be initialized.`
);
return metricReaders;
} else {
enabledExporters.forEach(exporter => {
if (exporter === 'otlp') {
const protocol =
process.env.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL?.trim() ||
process.env.OTEL_EXPORTER_OTLP_PROTOCOL?.trim();

const exportIntervalMillis = getValueInMillis(
'OTEL_METRIC_EXPORT_INTERVAL',
60000
);
const exportTimeoutMillis = getValueInMillis(
'OTEL_METRIC_EXPORT_TIMEOUT',
30000
);

switch (protocol) {
case 'grpc':
metricReaders.push(
new PeriodicExportingMetricReader({
exporter: new OTLPGrpcMetricExporter(),
exportIntervalMillis: exportIntervalMillis,
exportTimeoutMillis: exportTimeoutMillis,
})
);
break;
case 'http/json':
metricReaders.push(
new PeriodicExportingMetricReader({
exporter: new OTLPHttpMetricExporter(),
exportIntervalMillis: exportIntervalMillis,
exportTimeoutMillis: exportTimeoutMillis,
})
);
break;
case 'http/protobuf':
metricReaders.push(
new PeriodicExportingMetricReader({
exporter: new OTLPProtoMetricExporter(),
exportIntervalMillis: exportIntervalMillis,
exportTimeoutMillis: exportTimeoutMillis,
})
);
break;
default:
diag.warn(
`Unsupported OTLP metrics protocol: "${protocol}". Using http/protobuf.`
);
metricReaders.push(
new PeriodicExportingMetricReader({
exporter: new OTLPProtoMetricExporter(),
exportIntervalMillis: exportIntervalMillis,
exportTimeoutMillis: exportTimeoutMillis,
})
);
}
} else if (exporter === 'console') {
metricReaders.push(
new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
})
);
} else if (exporter === 'prometheus') {
metricReaders.push(new PrometheusMetricExporter());
} else {
diag.warn(
`Unsupported OTEL_METRICS_EXPORTER value: "${exporter}". Supported values are: otlp, console, prometheus, none.`
);
}
});
}

return metricReaders;
}
export class NodeSDK {
private _tracerProviderConfig?: {
tracerConfig: NodeTracerConfig;
Expand Down Expand Up @@ -290,11 +402,18 @@ export class NodeSDK {
logs.setGlobalLoggerProvider(loggerProvider);
}

if (this._meterProviderConfig) {
const metricReadersFromEnv: MetricReader[] =
configureMetricProviderFromEnv();
if (this._meterProviderConfig || metricReadersFromEnv.length > 0) {
const readers: MetricReader[] = [];
if (this._meterProviderConfig.reader) {
if (this._meterProviderConfig?.reader) {
readers.push(this._meterProviderConfig.reader);
}

if (readers.length === 0) {
metricReadersFromEnv.forEach((r: MetricReader) => readers.push(r));
}

const meterProvider = new MeterProvider({
resource: this._resource,
views: this._meterProviderConfig?.views ?? [],
Expand All @@ -303,7 +422,6 @@ export class NodeSDK {
});

this._meterProvider = meterProvider;

metrics.setGlobalMeterProvider(meterProvider);

// TODO: This is a workaround to fix https://github.com/open-telemetry/opentelemetry-js/issues/3609
Expand Down
Loading

0 comments on commit 67ad4da

Please sign in to comment.