Skip to content

Commit

Permalink
[Jaeger-Exporter] host default env var (open-telemetry#924)
Browse files Browse the repository at this point in the history
It still falls back to localhost, but if the env var is set, it will use that as the host to send spans to. This prevents end users from writing extra code when the jaeger agent is remote.

Signed-off-by: Naseem <naseem@transit.app>

Co-authored-by: Mayur Kale <mayurkale@google.com>
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 8, 2020
1 parent c061699 commit 4a5717c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/opentelemetry-exporter-jaeger/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# OpenTelemetry Jaeger Trace Exporter

[![Gitter chat][gitter-image]][gitter-url]
[![NPM Published Version][npm-img]][npm-url]
[![dependencies][dependencies-image]][dependencies-url]
Expand Down Expand Up @@ -54,6 +55,10 @@ npm install --save @opentelemetry/exporter-jaeger

Install the exporter on your application and pass the options, it must contain a service name.

Furthermore, the `host` option (which defaults to `localhost`), can instead be set by the
`JAEGER_AGENT_HOST` environment variable to reduce in-code config. If both are
set, the value set by the option in code is authoritative.

```js
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';

Expand All @@ -78,8 +83,8 @@ You can use built-in `SimpleSpanProcessor` or `BatchSpanProcessor` or write your
- [SimpleSpanProcessor](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/sdk-tracing.md#simple-processor): The implementation of `SpanProcessor` that passes ended span directly to the configured `SpanExporter`.
- [BatchSpanProcessor](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/sdk-tracing.md#batching-processor): The implementation of the `SpanProcessor` that batches ended spans and pushes them to the configured `SpanExporter`. It is recommended to use this `SpanProcessor` for better performance and optimization.


## Useful links

- To know more about Jaeger, visit: https://www.jaegertracing.io/docs/latest/getting-started/
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-exporter-jaeger/src/jaeger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class JaegerExporter implements SpanExporter {
this._onShutdownFlushTimeout =
typeof config.flushTimeout === 'number' ? config.flushTimeout : 2000;

config.host = config.host || process.env.JAEGER_AGENT_HOST;

this._sender = new jaegerTypes.UDPSender(config);
if (this._sender._client instanceof Socket) {
// unref socket to prevent it from keeping the process running
Expand Down
31 changes: 30 additions & 1 deletion packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import { Resource } from '@opentelemetry/resources';

describe('JaegerExporter', () => {
describe('constructor', () => {
afterEach(() => {
delete process.env.JAEGER_AGENT_HOST;
});

it('should construct an exporter', () => {
const exporter = new JaegerExporter({ serviceName: 'opentelemetry' });
assert.ok(typeof exporter.export === 'function');
Expand All @@ -38,7 +42,7 @@ describe('JaegerExporter', () => {
it('should construct an exporter with host, port, logger and tags', () => {
const exporter = new JaegerExporter({
serviceName: 'opentelemetry',
host: 'localhost',
host: 'remotehost',
port: 8080,
logger: new NoopLogger(),
tags: [{ key: 'opentelemetry-exporter-jaeger', value: '0.1.0' }],
Expand All @@ -47,13 +51,38 @@ describe('JaegerExporter', () => {
assert.ok(typeof exporter.shutdown === 'function');

const process: ThriftProcess = exporter['_sender']._process;
assert.strictEqual(exporter['_sender']._host, 'remotehost');
assert.strictEqual(process.serviceName, 'opentelemetry');
assert.strictEqual(process.tags.length, 1);
assert.strictEqual(process.tags[0].key, 'opentelemetry-exporter-jaeger');
assert.strictEqual(process.tags[0].vType, 'STRING');
assert.strictEqual(process.tags[0].vStr, '0.1.0');
});

it('should default to localhost if no host is configured', () => {
const exporter = new JaegerExporter({
serviceName: 'opentelemetry',
});
assert.strictEqual(exporter['_sender']._host, 'localhost');
});

it('should respect jaeger host env variable', () => {
process.env.JAEGER_AGENT_HOST = 'env-set-host';
const exporter = new JaegerExporter({
serviceName: 'test-service',
});
assert.strictEqual(exporter['_sender']._host, 'env-set-host');
});

it('should prioritize host option over env variable', () => {
process.env.JAEGER_AGENT_HOST = 'env-set-host';
const exporter = new JaegerExporter({
serviceName: 'test-service',
host: 'option-set-host',
});
assert.strictEqual(exporter['_sender']._host, 'option-set-host');
});

it('should construct an exporter with flushTimeout', () => {
const exporter = new JaegerExporter({
serviceName: 'opentelemetry',
Expand Down

0 comments on commit 4a5717c

Please sign in to comment.