Skip to content
Open
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 @@ -13,6 +13,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
### :rocket: Features

* feat(sdk-logs): deprecate `SdkLogRecord` in favor of `ReadWriteLogRecord` [#6939](https://github.com/open-telemetry/opentelemetry-js/pull/6939) @pichlermarc
* feat(otlp-exporter-base): add `createOtlp*Exporter()` factory functions to the OTLP trace, metric and log exporter packages (`-otlp-http`, `-otlp-proto`, `-otlp-grpc`); unlike the exporter classes, they do not read `OTEL_EXPORTER_OTLP_*` environment variables, which makes them suitable for use with declarative configuration [#6959](https://github.com/open-telemetry/opentelemetry-js/issues/6959) @pacocartones

### :bug: Bug Fixes

Expand Down
12 changes: 12 additions & 0 deletions experimental/packages/exporter-logs-otlp-grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ const loggerProvider = new LoggerProvider({

> Settings configured programmatically take precedence over environment variables. Per-signal environment variables take precedence over non-per-signal environment variables.

### Creating an exporter without environment variable configuration

The `OTLPLogExporter` class reads the environment variables described above. When that is not desirable — for example, when the exporter is configured from a configuration file — use `createOtlpGrpcLogExporter` instead. It accepts the same options, but does not read `OTEL_EXPORTER_OTLP_*` environment variables; options that are not provided fall back to the defaults defined by the OTLP exporter specification.

```js
const { createOtlpGrpcLogExporter } = require('@opentelemetry/exporter-logs-otlp-grpc');

const exporter = createOtlpGrpcLogExporter({
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is localhost:4317
});
```

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
import type { OTLPGRPCExporterConfigNode } from '@opentelemetry/otlp-grpc-exporter-base';
import {
convertLegacyOtlpGrpcOptions,
convertLegacyOtlpGrpcOptionsWithoutEnv,
createOtlpGrpcExportDelegate,
} from '@opentelemetry/otlp-grpc-exporter-base';
import {
Expand Down Expand Up @@ -40,3 +41,28 @@ export class OTLPLogExporter
);
}
}

/**
* Creates a log record exporter that sends data over OTLP/gRPC.
*
* Unlike the {@link OTLPLogExporter} class, the created exporter does not use
* `OTEL_EXPORTER_OTLP_*` environment variables for configuration: options that
* are not provided in `config` fall back to the defaults defined by the OTLP
* exporter specification. Reading configuration from the environment is the
* caller's responsibility.
*/
export function createOtlpGrpcLogExporter(
config: OTLPGRPCExporterConfigNode = {}
): LogRecordExporter {
return new OTLPExporterBase(
createOtlpGrpcExportDelegate(
convertLegacyOtlpGrpcOptionsWithoutEnv(config),
ProtobufLogsSerializer,
OTEL_COMPONENT_TYPE_VALUE_OTLP_GRPC_LOG_EXPORTER,
LogsExporterMetricsHelper,
config.selfObsMeterProvider,
'LogsExportService',
'/opentelemetry.proto.collector.logs.v1.LogsService/Export'
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { OTLPLogExporter } from './OTLPLogExporter';
export { createOtlpGrpcLogExporter, OTLPLogExporter } from './OTLPLogExporter';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { OTLPLogExporter } from '../src';
import { OTLPLogExporter, createOtlpGrpcLogExporter } from '../src';
import type { ServerTestContext } from './utils';
import { startServer, TestMetricReader } from './utils';
import * as assert from 'assert';
Expand Down Expand Up @@ -100,4 +100,28 @@ describe('OTLPLogExporter', function () {
assert.ok(scopeMetrics);
await meterProvider.shutdown();
});

describe('createOtlpGrpcLogExporter', function () {
it('successfully exports data', async () => {
// arrange
const loggerProvider = new LoggerProvider({
processors: [
new SimpleLogRecordProcessor({
exporter: createOtlpGrpcLogExporter({
url: 'http://localhost:1503',
}),
}),
],
});

// act
loggerProvider.getLogger('test-logger').emit({
body: 'test-body',
});
await loggerProvider.shutdown();

// assert
assert.strictEqual(serverTestContext.requests.length, 1);
});
});
});
12 changes: 12 additions & 0 deletions experimental/packages/exporter-logs-otlp-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ In addition to settings passed to the constructor, the exporter also supports co

> Settings configured programmatically take precedence over environment variables. Per-signal environment variables take precedence over non-per-signal environment variables.

### Creating an exporter without environment variable configuration

The `OTLPLogExporter` class reads the environment variables described above. When that is not desirable — for example, when the exporter is configured from a configuration file — use `createOtlpHttpLogExporter` instead. It accepts the same options, but does not read `OTEL_EXPORTER_OTLP_*` environment variables; options that are not provided fall back to the defaults defined by the OTLP exporter specification.

```js
const { createOtlpHttpLogExporter } = require('@opentelemetry/exporter-logs-otlp-http');

const exporter = createOtlpHttpLogExporter({
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/logs
});
```

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { OTLPLogExporter } from './platform';
export { createOtlpHttpLogExporter, OTLPLogExporter } from './platform';
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,28 @@ export class OTLPLogExporter
);
}
}

/**
* Creates a log record exporter that sends data over OTLP/HTTP with JSON
* encoding.
*
* The created exporter does not use `OTEL_EXPORTER_OTLP_*` environment
* variables for configuration: options that are not provided in `config` fall
* back to the defaults defined by the OTLP exporter specification. Reading
* configuration from the environment is the caller's responsibility.
*/
export function createOtlpHttpLogExporter(
config: OTLPExporterConfigBase = {}
): LogRecordExporter {
return new OTLPExporterBase(
createLegacyOtlpBrowserExportDelegate(
config,
JsonLogsSerializer,
OTEL_COMPONENT_TYPE_VALUE_OTLP_HTTP_LOG_EXPORTER,
LogsExporterMetricsHelper,
config.selfObsMeterProvider,
'v1/logs',
{ 'Content-Type': 'application/json' }
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { OTLPLogExporter } from './OTLPLogExporter';
export { createOtlpHttpLogExporter, OTLPLogExporter } from './OTLPLogExporter';
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { OTLPLogExporter } from './node';
export { createOtlpHttpLogExporter, OTLPLogExporter } from './node';
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@opentelemetry/otlp-transformer';
import {
convertLegacyHttpOptions,
convertLegacyHttpOptionsWithoutEnv,
createOtlpHttpExportDelegate,
} from '@opentelemetry/otlp-exporter-base/node-http';

Expand All @@ -41,3 +42,29 @@ export class OTLPLogExporter
);
}
}

/**
* Creates a log record exporter that sends data over OTLP/HTTP with JSON
* encoding.
*
* Unlike the {@link OTLPLogExporter} class, the created exporter does not use
* `OTEL_EXPORTER_OTLP_*` environment variables for configuration: options that
* are not provided in `config` fall back to the defaults defined by the OTLP
* exporter specification. Reading configuration from the environment is the
* caller's responsibility.
*/
export function createOtlpHttpLogExporter(
config: OTLPExporterNodeConfigBase = {}
): LogRecordExporter {
return new OTLPExporterBase(
createOtlpHttpExportDelegate(
convertLegacyHttpOptionsWithoutEnv(config, 'v1/logs', {
'Content-Type': 'application/json',
}),
JsonLogsSerializer,
OTEL_COMPONENT_TYPE_VALUE_OTLP_HTTP_LOG_EXPORTER,
LogsExporterMetricsHelper,
config.selfObsMeterProvider
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { OTLPLogExporter } from './OTLPLogExporter';
export { createOtlpHttpLogExporter, OTLPLogExporter } from './OTLPLogExporter';
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import * as assert from 'assert';
import * as sinon from 'sinon';

import { OTLPLogExporter } from '../../src/platform/browser';
import {
createOtlpHttpLogExporter,
OTLPLogExporter,
} from '../../src/platform/browser';
import {
LoggerProvider,
SimpleLogRecordProcessor,
Expand Down Expand Up @@ -66,3 +69,31 @@ describe('OTLPLogExporter', function () {
});
});
});

describe('createOtlpHttpLogExporter', () => {
afterEach(() => {
sinon.restore();
});

it('should send data to the default endpoint', async function () {
// arrange
const stubFetch = sinon
.stub(window, 'fetch')
.resolves(new Response('', { status: 200 }));
const loggerProvider = new LoggerProvider({
processors: [
new SimpleLogRecordProcessor({ exporter: createOtlpHttpLogExporter() }),
],
});

// act
loggerProvider.getLogger('test-logger').emit({ body: 'test-body' });
await loggerProvider.shutdown();

// assert
assert.strictEqual(
stubFetch.firstCall.args[0],
'http://localhost:4318/v1/logs'
);
});
});
Loading