Skip to content

Commit 4420402

Browse files
pichlermarcdyladan
andauthored
feat(metrics-exporters): configure temporality via env var(#3305)
Co-authored-by: Daniel Dyla <[email protected]>
1 parent 30a81bd commit 4420402

File tree

13 files changed

+169
-83
lines changed

13 files changed

+169
-83
lines changed

experimental/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes to experimental packages in this project will be documented
1414
* feat: enable tree shaking [#3329](https://github.com/open-telemetry/opentelemetry-js/pull/3329) @pkanal
1515
* feat(prometheus): serialize resource as target_info gauge [#3300](https://github.com/open-telemetry/opentelemetry-js/pull/3300) @pichlermarc
1616
* deps: remove unused proto-loader dependencies and update grpc-js and proto-loader versions [#3337](https://github.com/open-telemetry/opentelemetry-js/pull/3337) @seemk
17+
* feat(metrics-exporters): configure temporality via environment variable [#3305](https://github.com/open-telemetry/opentelemetry-js/pull/3305) @pichlermarc
1718

1819
### :bug: (Bug Fix)
1920

experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/README.md

+25-18
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ npm install --save @opentelemetry/exporter-metrics-otlp-grpc
1717
## Service Name
1818

1919
The OpenTelemetry Collector Exporter does not have a service name configuration.
20-
In order to set the service name, use the `service.name` resource attribute as prescribed in the [OpenTelemetry Resource Semantic Conventions][semconv-resource-service-name].
21-
To see sample code and documentation for the traces exporter, as well as instructions for using TLS, visit the [Collector Trace Exporter for web and node][trace-exporter-url].
20+
In order to set the service name, use the `service.name` resource attribute as prescribed in
21+
the [OpenTelemetry Resource Semantic Conventions][semconv-resource-service-name].
22+
To see sample code and documentation for the traces exporter, as well as instructions for using TLS, visit
23+
the [Collector Trace Exporter for web and node][trace-exporter-url].
2224

2325
## Metrics in Node - GRPC
2426

25-
The OTLPMetricsExporter in Node expects the URL to only be the hostname. It will not work with `/v1/metrics`. All options that work with trace also work with metrics.
27+
The OTLPMetricsExporter in Node expects the URL to only be the hostname. It will not work with `/v1/metrics`. All
28+
options that work with trace also work with metrics.
2629

2730
```js
2831
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
29-
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
32+
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
3033
const collectorOptions = {
3134
// url is optional and can be omitted - default is http://localhost:4317
3235
url: 'http://<collector-hostname>:<port>',
@@ -52,20 +55,24 @@ counter.add(10, { 'key': 'value' });
5255

5356
## Environment Variable Configuration
5457

55-
| Environment variable | Description |
56-
|----------------------|-------------|
57-
| OTEL_EXPORTER_OTLP_METRICS_COMPRESSION | The compression type to use on OTLP metric requests. Options include gzip. By default no compression will be used. |
58-
| OTEL_EXPORTER_OTLP_COMPRESSION | The compression type to use on OTLP trace, metric, and log requests. Options include gzip. By default no compression will be used. |
59-
| OTEL_EXPORTER_OTLP_METRICS_INSECURE | Whether to enable client transport security for the exporter's gRPC connection for metric requests. This option only applies to OTLP/gRPC when an endpoint is provided without the http or https scheme. Options include true or false. By default insecure is false which creates a secure connection. |
60-
| OTEL_EXPORTER_OTLP_INSECURE | Whether to enable client transport security for the exporter's gRPC connection for trace, metric and log requests. This option only applies to OTLP/gRPC when an endpoint is provided without the http or https scheme. Options include true or false. By default insecure is false which creates a secure connection. |
61-
| OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE | The path to the file containing trusted root certificate to use when verifying an OTLP metric server's TLS credentials. By default the host platform's trusted root certificate is used.|
62-
| OTEL_EXPORTER_OTLP_CERTIFICATE | The path to the file containing trusted root certificate to use when verifying an OTLP trace, metric, or log server's TLS credentials. By default the host platform's trusted root certificate is used. |
63-
| OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY | The path to the file containing private client key to use when verifying an OTLP metric client's TLS credentials. Must provide a client certificate/chain when providing a private client key. By default no client key file is used. |
64-
| OTEL_EXPORTER_OTLP_CLIENT_KEY | The path to the file containing private client key to use when verifying an OTLP trace, metric or log client's TLS credentials. Must provide a client certificate/chain when providing a private client key. By default no client key file is used. |
65-
| OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE | The path to the file containing trusted client certificate/chain for clients private key to use when verifying an OTLP metric server's TLS credentials. Must provide a private client key when providing a certificate/chain. By default no chain file is used. |
66-
| OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE | The path to the file containing trusted client certificate/chain for clients private key to use when verifying an OTLP trace, metric and log server's TLS credentials. Must provide a private client key when providing a certificate/chain. By default no chain file is used. |
67-
68-
> Settings configured programmatically take precedence over environment variables. Per-signal environment variables take precedence over non-per-signal environment variables.
58+
In addition to settings passed to the constructor, the exporter also supports configuration via environment variables:
59+
60+
| Environment variable | Description |
61+
|----------------------|-------------|
62+
| OTEL_EXPORTER_OTLP_ENDPOINT | The endpoint to send metrics to. This will also be used for the traces exporter if `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` is not configured. By default `localhost:4317` will be used. |
63+
| OTEL_EXPORTER_OTLP_METRICS_ENDPOINT | The endpoint to send metrics to. By default `localhost:4317` will be used. |
64+
| OTEL_EXPORTER_OTLP_COMPRESSION | The compression type to use on OTLP trace, metric, and log requests. Options include gzip. By default no compression will be used. |
65+
| OTEL_EXPORTER_OTLP_METRICS_INSECURE | Whether to enable client transport security for the exporter's gRPC connection for metric requests. This option only applies to OTLP/gRPC when an endpoint is provided without the http or https scheme. Options include true or false. By default insecure is false which creates a secure connection. |
66+
| OTEL_EXPORTER_OTLP_INSECURE | Whether to enable client transport security for the exporter's gRPC connection for trace, metric and log requests. This option only applies to OTLP/gRPC when an endpoint is provided without the http or https scheme. Options include true or false. By default insecure is false which creates a secure connection. |
67+
| OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE | The path to the file containing trusted root certificate to use when verifying an OTLP metric server's TLS credentials. By default the host platform's trusted root certificate is used.|
68+
| OTEL_EXPORTER_OTLP_CERTIFICATE | The path to the file containing trusted root certificate to use when verifying an OTLP trace, metric, or log server's TLS credentials. By default the host platform's trusted root certificate is used. |
69+
| OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY | The path to the file containing private client key to use when verifying an OTLP metric client's TLS credentials. Must provide a client certificate/chain when providing a private client key. By default no client key file is used. |
70+
| OTEL_EXPORTER_OTLP_CLIENT_KEY | The path to the file containing private client key to use when verifying an OTLP trace, metric or log client's TLS credentials. Must provide a client certificate/chain when providing a private client key. By default no client key file is used. |
71+
| OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE | The path to the file containing trusted client certificate/chain for clients private key to use when verifying an OTLP metric server's TLS credentials. Must provide a private client key when providing a certificate/chain. By default no chain file is used. |
72+
| OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE | The path to the file containing trusted client certificate/chain for clients private key to use when verifying an OTLP trace, metric and log server's TLS credentials. Must provide a private client key when providing a certificate/chain. By default no chain file is used. |
73+
| OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE | The exporters aggregation temporality preference. Valid values are `cumulative`, and `delta`. `cumulative` selects cumulative temporality for all instrument kinds. `delta` selects delta aggregation temporality for Counter, Asynchronous Counter and Histogram instrument kinds, and selects cumulative aggregation for UpDownCounter and Asynchronous UpDownCounter instrument kinds. By default `cumulative` is used. |
74+
75+
> Settings configured programmatically take precedence over environment variables. Per-signal environment variables take precedence over non-per-signal environment variables.
6976
7077
## Running opentelemetry-collector locally to see the metrics
7178

experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/src/OTLPMetricExporter.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import {
18-
defaultOptions,
1918
OTLPMetricExporterBase,
2019
OTLPMetricExporterOptions
2120
} from '@opentelemetry/exporter-metrics-otlp-http';
@@ -33,7 +32,7 @@ import { createExportMetricsServiceRequest, IExportMetricsServiceRequest } from
3332

3433
class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<ResourceMetrics, IExportMetricsServiceRequest> {
3534

36-
constructor(config: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions= defaultOptions) {
35+
constructor(config?: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions) {
3736
super(config);
3837
const headers = baggageUtils.parseKeyPairsIntoRecord(getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS);
3938
this.metadata ||= new Metadata();
@@ -73,7 +72,7 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<ResourceMetrics,
7372
* OTLP-gRPC metric exporter
7473
*/
7574
export class OTLPMetricExporter extends OTLPMetricExporterBase<OTLPMetricExporterProxy>{
76-
constructor(config: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions = defaultOptions) {
75+
constructor(config?: OTLPGRPCExporterConfigNode & OTLPMetricExporterOptions) {
7776
super(new OTLPMetricExporterProxy(config), config);
7877
}
7978
}

experimental/packages/opentelemetry-exporter-metrics-otlp-http/README.md

+17-29
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ npm install --save @opentelemetry/exporter-metrics-otlp-http
1717
## Service Name
1818

1919
The OpenTelemetry Collector Metrics Exporter does not have a service name configuration.
20-
In order to set the service name, use the `service.name` resource attribute as prescribed in the [OpenTelemetry Resource Semantic Conventions][semconv-resource-service-name].
21-
To see sample code and documentation for the traces exporter, visit the [Collector Trace Exporter for web and node][trace-exporter-url].
20+
In order to set the service name, use the `service.name` resource attribute as prescribed in
21+
the [OpenTelemetry Resource Semantic Conventions][semconv-resource-service-name].
22+
To see sample code and documentation for the traces exporter, visit
23+
the [Collector Trace Exporter for web and node][trace-exporter-url].
2224

2325
## Metrics in Web
2426

@@ -27,6 +29,7 @@ The OTLPMetricExporter in Web expects the endpoint to end in `/v1/metrics`.
2729
```js
2830
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
2931
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
32+
3033
const collectorOptions = {
3134
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
3235
headers: {}, // an optional object containing custom headers to be sent with each request
@@ -50,7 +53,7 @@ counter.add(10, { 'key': 'value' });
5053

5154
```js
5255
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
53-
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
56+
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
5457
const collectorOptions = {
5558
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
5659
concurrencyLimit: 1, // an optional limit on pending requests
@@ -70,35 +73,18 @@ counter.add(10, { 'key': 'value' });
7073

7174
```
7275

73-
## GRPC
74-
75-
For exporting metrics with GRPC please check [exporter-metrics-otlp-grpc][npm-url-grpc]
76-
77-
## PROTOBUF
78-
79-
For exporting metrics with PROTOBUF please check [exporter-metrics-otlp-proto][npm-url-proto]
80-
81-
## Configuration options as environment variables
82-
83-
Instead of providing options to `OTLPMetricExporter` and `OTLPTraceExporter` explicitly, environment variables may be provided instead.
84-
85-
```sh
86-
OTEL_EXPORTER_OTLP_ENDPOINT=https://localhost:4318
87-
# this will automatically append the version and signal path
88-
# e.g. https://localhost:4318/v1/traces for `OTLPTraceExporter` and https://localhost:4318/v1/metrics for `OTLPMetricExporter`
89-
```
90-
91-
If the trace and metric exporter endpoints have different providers, the env var for per-signal endpoints are available to use
76+
## Environment Variable Configuration
9277

93-
```sh
94-
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://trace-service:4318/v1/traces
95-
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://metric-service:4318/v1/metrics
96-
# version and signal needs to be explicit
97-
```
78+
In addition to settings passed to the constructor, the exporter also supports configuration via environment variables:
9879

99-
> The per-signal endpoints take precedence and overrides `OTEL_EXPORTER_OTLP_ENDPOINT`
80+
| Environment variable | Description |
81+
|---------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
82+
| OTEL_EXPORTER_OTLP_ENDPOINT | The endpoint to send metrics to. This will also be used for the traces exporter if `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` is not configured. By default `http://localhost:4318` will be used. `/v1/metrics` will be automatically appended to configured values. |
83+
| OTEL_EXPORTER_OTLP_METRICS_ENDPOINT | The endpoint to send metrics to. By default `https://localhost:4318/v1/metrics` will be used. `v1/metrics` will not be appended automatically and has to be added explicitly. |
84+
| OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE | The exporters aggregation temporality preference. Valid values are `cumulative`, and `delta`. `cumulative` selects cumulative temporality for all instrument kinds. `delta` selects delta aggregation temporality for Counter, Asynchronous Counter and Histogram instrument kinds, and selects cumulative aggregation for UpDownCounter and Asynchronous UpDownCounter instrument kinds. By default `cumulative` is used. |
10085

101-
For more details, see [OpenTelemetry Specification on Protocol Exporter][opentelemetry-spec-protocol-exporter].
86+
> Settings configured programmatically take precedence over environment variables. Per-signal environment variables take
87+
> precedence over non-per-signal environment variables.
10288
10389
## Running opentelemetry-collector locally to see the metrics
10490

@@ -110,6 +96,8 @@ For more details, see [OpenTelemetry Specification on Protocol Exporter][opentel
11096
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
11197
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>
11298
- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]
99+
- For exporting metrics via gRPC please check [exporter-metrics-otlp-grpc][npm-url-grpc]
100+
- For exporting metrics via protobuf please check [exporter-metrics-otlp-proto][npm-url-proto]
113101

114102
## License
115103

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterBase.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ExportResult } from '@opentelemetry/core';
17+
import {
18+
ExportResult,
19+
getEnv
20+
} from '@opentelemetry/core';
1821
import {
1922
AggregationTemporality,
2023
AggregationTemporalitySelector,
2124
InstrumentType,
2225
PushMetricExporter,
2326
ResourceMetrics
2427
} from '@opentelemetry/sdk-metrics';
25-
import { defaultOptions, OTLPMetricExporterOptions } from './OTLPMetricExporterOptions';
28+
import {
29+
OTLPMetricExporterOptions
30+
} from './OTLPMetricExporterOptions';
2631
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';
2732
import { IExportMetricsServiceRequest } from '@opentelemetry/otlp-transformer';
33+
import { diag } from '@opentelemetry/api';
2834

2935
export const CumulativeTemporalitySelector: AggregationTemporalitySelector = () => AggregationTemporality.CUMULATIVE;
3036

@@ -41,14 +47,33 @@ export const DeltaTemporalitySelector: AggregationTemporalitySelector = (instrum
4147
}
4248
};
4349

44-
function chooseTemporalitySelector(temporalityPreference?: AggregationTemporality): AggregationTemporalitySelector {
45-
if (temporalityPreference === AggregationTemporality.DELTA) {
50+
function chooseTemporalitySelectorFromEnvironment() {
51+
const env = getEnv();
52+
const configuredTemporality = env.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE.trim().toLowerCase();
53+
54+
if (configuredTemporality === 'cumulative') {
55+
return CumulativeTemporalitySelector;
56+
}
57+
if (configuredTemporality === 'delta') {
4658
return DeltaTemporalitySelector;
4759
}
4860

61+
diag.warn(`OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to '${env.OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE}', but only 'cumulative' and 'delta' are allowed. Using default ('cumulative') instead.`);
4962
return CumulativeTemporalitySelector;
5063
}
5164

65+
function chooseTemporalitySelector(temporalityPreference?: AggregationTemporality): AggregationTemporalitySelector {
66+
// Directly passed preference has priority.
67+
if (temporalityPreference != null) {
68+
if (temporalityPreference === AggregationTemporality.DELTA) {
69+
return DeltaTemporalitySelector;
70+
}
71+
return CumulativeTemporalitySelector;
72+
}
73+
74+
return chooseTemporalitySelectorFromEnvironment();
75+
}
76+
5277
export class OTLPMetricExporterBase<T extends OTLPExporterBase<OTLPMetricExporterOptions,
5378
ResourceMetrics,
5479
IExportMetricsServiceRequest>>
@@ -57,9 +82,9 @@ implements PushMetricExporter {
5782
protected _aggregationTemporalitySelector: AggregationTemporalitySelector;
5883

5984
constructor(exporter: T,
60-
config: OTLPMetricExporterOptions = defaultOptions) {
85+
config?: OTLPMetricExporterOptions) {
6186
this._otlpExporter = exporter;
62-
this._aggregationTemporalitySelector = chooseTemporalitySelector(config.temporalityPreference);
87+
this._aggregationTemporalitySelector = chooseTemporalitySelector(config?.temporalityPreference);
6388
}
6489

6590
export(metrics: ResourceMetrics, resultCallback: (result: ExportResult) => void): void {

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/OTLPMetricExporterOptions.ts

-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@ import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
2020
export interface OTLPMetricExporterOptions extends OTLPExporterConfigBase {
2121
temporalityPreference?: AggregationTemporality
2222
}
23-
export const defaultExporterTemporality = AggregationTemporality.CUMULATIVE;
24-
export const defaultOptions = {temporalityPreference: defaultExporterTemporality};

0 commit comments

Comments
 (0)