Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
add code for intialization
Browse files Browse the repository at this point in the history
Signed-off-by: Won Jun Jang <wjang@uber.com>
  • Loading branch information
black-adder committed Apr 12, 2018
1 parent 23c45a8 commit 3d549b2
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 22 deletions.
34 changes: 33 additions & 1 deletion src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Tracer from './tracer';
import UDPSender from './reporters/udp_sender';
import opentracing from 'opentracing';
import * as constants from './constants.js';
import RemoteThrottler from './throttler/remote_throttler';

let jaegerSchema = {
id: '/jaeger',
Expand Down Expand Up @@ -49,6 +50,14 @@ let jaegerSchema = {
},
additionalProperties: false,
},
throttler: {
properties: {
host: { type: 'string' },
port: { type: 'number' },
refreshIntervalMs: { type: 'number' },
},
additionalProperties: false,
},
},
};

Expand Down Expand Up @@ -93,7 +102,7 @@ export default class Configuration {
let reporterConfig = {};
let reporters = [];
let senderConfig = {
logger: config.logger,
logger: options.logger,
};
if (config.reporter) {
if (config.reporter.logSpans) {
Expand Down Expand Up @@ -121,6 +130,17 @@ export default class Configuration {
return new CompositeReporter(reporters);
}

static _getThrottler(config, options) {
const throttlerOptions = Object.assign({}, config.throttler);
if (options.logger) {
throttlerOptions.logger = options.logger;
}
if (options.metrics) {
throttlerOptions.metrics = options.metrics;
}
return new RemoteThrottler(config.serviceName, options);
}

/**
* Initialize and return a new instance of Jaeger Tracer.
*
Expand All @@ -139,6 +159,9 @@ export default class Configuration {
* @param {Object} [options.reporter] - if provided, this reporter will be used.
* Otherwise a new reporter will be created according to the description
* in the config.
* @param {Object} [options.throttler] - if provided, this throttler will be used.
* Otherwise a new throttler will be created according to the description
* in the config.
* @param {Object} [options.metrics] - a metrics factory (see ./_flow/metrics.js)
* @param {Object} [options.logger] - a logger (see ./_flow/logger.js)
* @param {Object} [options.tags] - set of key-value pairs which will be set
Expand All @@ -147,6 +170,7 @@ export default class Configuration {
static initTracer(config, options = {}) {
let reporter;
let sampler;
let throttler;
if (options.metrics) {
options.metrics = new Metrics(options.metrics);
}
Expand All @@ -166,6 +190,13 @@ export default class Configuration {
} else {
reporter = options.reporter;
}
if (!options.throttler) {
if (config.throttler) {
throttler = Configuration._getThrottler(config, options);
}
} else {
throttler = options.throttler;
}

if (options.logger) {
options.logger.info(`Initializing Jaeger Tracer with ${reporter.name()} and ${sampler.name()}`);
Expand All @@ -175,6 +206,7 @@ export default class Configuration {
metrics: options.metrics,
logger: options.logger,
tags: options.tags,
debugThrottler: throttler,
});
}
}
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const JAEGER_CLIENT_VERSION_TAG_KEY = 'jaeger.version';
// TRACER_HOSTNAME_TAG_KEY used to report host name of the process.
export const TRACER_HOSTNAME_TAG_KEY = 'jaeger.hostname';

// TRACER_CLIENT_ID_TAG_KEY used to report client ID of the process.
export const TRACER_CLIENT_ID_TAG_KEY = 'client-id';

// PROCESS_IP used to report ip of the process.
export const PROCESS_IP = 'ip';

Expand Down
4 changes: 3 additions & 1 deletion src/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ export default class Tracer {
this.registerInjector(opentracing.FORMAT_BINARY, binaryCodec);
this.registerExtractor(opentracing.FORMAT_BINARY, binaryCodec);

const uuid = Utils.getRandom64().toString('hex');
this._tags[constants.TRACER_CLIENT_ID_TAG_KEY] = uuid;
this._process = {
serviceName: serviceName,
tags: Utils.convertObjectToTags(this._tags),
uuid: Utils.getRandom64().toString('hex'),
uuid: uuid,
};
if (typeof this._debugThrottler.setProcess === 'function') {
this._debugThrottler.setProcess(this._process);
Expand Down
62 changes: 47 additions & 15 deletions test/init_tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import _ from 'lodash';
import { assert, expect } from 'chai';
import NoopReporter from '../src/reporters/noop_reporter';
import CompositeReporter from '../src/reporters/composite_reporter';
import RemoteReporter from '../src/reporters/remote_reporter';
import ConstSampler from '../src/samplers/const_sampler';
Expand All @@ -21,6 +20,24 @@ import RemoteSampler from '../src/samplers/remote_sampler';
import RateLimitingSampler from '../src/samplers/ratelimiting_sampler';
import { initTracer } from '../src/index.js';
import opentracing from 'opentracing';
import RemoteThrottler from '../src/throttler/remote_throttler';
import DefaultThrottler from '../src/throttler/default_throttler';

const logger = {
info: function info(msg) {},
};

const metrics = {
createCounter: function createCounter() {
return {};
},
createGauge: function createGauge() {
return {};
},
createTimer: function createTimer() {
return {};
},
};

describe('initTracer', () => {
it('should initialize noop tracer when disable is set', () => {
Expand Down Expand Up @@ -137,20 +154,6 @@ describe('initTracer', () => {
});

it('should pass options to tracer', () => {
let logger = {
info: function info(msg) {},
};
let metrics = {
createCounter: function createCounter() {
return {};
},
createGauge: function createGauge() {
return {};
},
createTimer: function createTimer() {
return {};
},
};
let tracer = initTracer(
{
serviceName: 'test-service',
Expand All @@ -167,4 +170,33 @@ describe('initTracer', () => {
assert.equal(tracer._metrics._factory, metrics);
assert.equal(tracer._tags['x'], 'y');
});

it('should initialize throttler from config', () => {
let config = {
serviceName: 'test-service',
throttler: {
refreshIntervalMs: 60000,
},
};
const tracer = initTracer(config, { logger: logger, metrics: metrics });
expect(tracer._debugThrottler).to.be.an.instanceof(RemoteThrottler);
});

it('should delegate throttler initialization to tracer', () => {
let config = {
serviceName: 'test-service',
};
const tracer = initTracer(config);
expect(tracer._debugThrottler).to.be.an.instanceof(DefaultThrottler);
});

it('should use throttler passed in via options', () => {
let config = {
serviceName: 'test-service',
};
const throttler = new RemoteThrottler();
const tracer = initTracer(config, { throttler: throttler });
expect(tracer._debugThrottler).to.be.an.instanceof(RemoteThrottler);
throttler.close();
});
});
14 changes: 13 additions & 1 deletion test/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import ConstSampler from '../src/samplers/const_sampler.js';
import * as constants from '../src/constants.js';
import InMemoryReporter from '../src/reporters/in_memory_reporter.js';
import * as opentracing from 'opentracing';
import { Tags as opentracing_tags } from 'opentracing';
import SpanContext from '../src/span_context.js';
import Tracer from '../src/tracer.js';
import Utils from '../src/util.js';
import Metrics from '../src/metrics/metrics.js';
import LocalMetricFactory from './lib/metrics/local/metric_factory.js';
import LocalBackend from './lib/metrics/local/backend.js';
import sinon from 'sinon';
import DefaultThrottler from '../src/throttler/default_throttler';

describe('tracer should', () => {
let tracer;
Expand Down Expand Up @@ -244,6 +245,17 @@ describe('tracer should', () => {
assert.equal(reporter.spans.length, 1);
});

it('set _process on initialization', () => {
const throttler = new DefaultThrottler();
throttler.setProcess = sinon.spy();
tracer = new Tracer('x', reporter, new ConstSampler(true), {
debugThrottler: throttler,
});
assert.equal(tracer._process.serviceName, 'x');
assert.isString(tracer._process.uuid);
sinon.assert.calledWith(throttler.setProcess, tracer._process);
});

describe('Metrics', () => {
it('startSpan', () => {
let params = [
Expand Down
9 changes: 5 additions & 4 deletions test/udp_sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ describe('udp sender', () => {
let actualTags = _.sortBy(batch.process.tags, o => {
return o.key;
});
assert.equal(actualTags.length, 3);
assert.equal(actualTags[0].key, 'ip');
assert.equal(actualTags[1].key, 'jaeger.hostname');
assert.equal(actualTags[2].key, 'jaeger.version');
assert.equal(actualTags.length, 4);
assert.equal(actualTags[0].key, 'client-id');
assert.equal(actualTags[1].key, 'ip');
assert.equal(actualTags[2].key, 'jaeger.hostname');
assert.equal(actualTags[3].key, 'jaeger.version');

sender.close();
done();
Expand Down

0 comments on commit 3d549b2

Please sign in to comment.