Skip to content

Commit

Permalink
feat: add TraceParams config (#227)
Browse files Browse the repository at this point in the history
* feat: add TraceParams config

* fix: remove extend dependency
  • Loading branch information
mayurkale22 authored Sep 9, 2019
1 parent 77a25dc commit 52a86c9
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

import * as types from '@opentelemetry/types';
import {
ALWAYS_SAMPLER,
BinaryTraceContext,
HttpTraceContext,
randomTraceId,
isValid,
randomSpanId,
Expand All @@ -31,9 +28,10 @@ import {
TraceOptions,
Logger,
} from '@opentelemetry/types';
import { BasicTracerConfig } from '../src/types';
import { BasicTracerConfig, TraceParams } from '../src/types';
import { ScopeManager } from '@opentelemetry/scope-base';
import { Span } from './Span';
import { mergeConfig } from './utility';

/**
* This class represents a basic tracer.
Expand All @@ -44,17 +42,20 @@ export class BasicTracer implements types.Tracer {
private readonly _httpTextFormat: types.HttpTextFormat;
private readonly _sampler: types.Sampler;
private readonly _scopeManager: ScopeManager;
private readonly _traceParams: TraceParams;
readonly logger: Logger;

/**
* Constructs a new Tracer instance.
*/
constructor(config: BasicTracerConfig) {
this._binaryFormat = config.binaryFormat || new BinaryTraceContext();
this._defaultAttributes = config.defaultAttributes || {};
this._httpTextFormat = config.httpTextFormat || new HttpTraceContext();
this._sampler = config.sampler || ALWAYS_SAMPLER;
this._scopeManager = config.scopeManager;
const localConfig = mergeConfig(config);
this._binaryFormat = localConfig.binaryFormat;
this._defaultAttributes = localConfig.defaultAttributes;
this._httpTextFormat = localConfig.httpTextFormat;
this._sampler = localConfig.sampler;
this._scopeManager = localConfig.scopeManager;
this._traceParams = localConfig.traceParams;
this.logger = config.logger || new ConsoleLogger(config.logLevel);
}

Expand Down Expand Up @@ -157,6 +158,11 @@ export class BasicTracer implements types.Tracer {
return this._httpTextFormat;
}

/** Returns the active {@link TraceParams}. */
getActiveTraceParams(): TraceParams {
return this._traceParams;
}

private _getParentSpanContext(
parent: types.Span | types.SpanContext | undefined
): types.SpanContext | undefined {
Expand Down
52 changes: 52 additions & 0 deletions packages/opentelemetry-basic-tracer/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
ALWAYS_SAMPLER,
BinaryTraceContext,
HttpTraceContext,
LogLevel,
} from '@opentelemetry/core';
import { NoopScopeManager } from '@opentelemetry/scope-base';

/** Default limit for Message events per span */
export const DEFAULT_MAX_EVENTS_PER_SPAN = 128;
/** Default limit for Attributes per span */
export const DEFAULT_MAX_ATTRIBUTES_PER_SPAN = 32;
/** Default limit for Links per span */
export const DEFAULT_MAX_LINKS_PER_SPAN = 32;

/**
* Default configuration. For fields with primitive values, any user-provided
* value will override the corresponding default value. For fields with
* non-primitive values (like `traceParams`), the user-provided value will be
* used to extend the default value.
*/
export const defaultConfig = {
defaultAttributes: {},
binaryFormat: new BinaryTraceContext(),
httpTextFormat: new HttpTraceContext(),
logLevel: LogLevel.DEBUG,
sampler: ALWAYS_SAMPLER,
scopeManager: new NoopScopeManager(),
traceParams: {
numberOfAttributesPerSpan: DEFAULT_MAX_ATTRIBUTES_PER_SPAN,
numberOfLinksPerSpan: DEFAULT_MAX_LINKS_PER_SPAN,
numberOfEventsPerSpan: DEFAULT_MAX_EVENTS_PER_SPAN,
},
// @todo add support for plugins
// plugins: {},
};
13 changes: 13 additions & 0 deletions packages/opentelemetry-basic-tracer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ export interface BasicTracerConfig {
* Scope manager keeps context across in-process operations.
*/
scopeManager: ScopeManager;

/** Trace Parameters */
traceParams?: TraceParams;
}

/** Global configuration of trace service */
export interface TraceParams {
/** numberOfAttributesPerSpan is number of attributes per span */
numberOfAttributesPerSpan?: number;
/** numberOfLinksPerSpan is number of links per span */
numberOfLinksPerSpan?: number;
/** numberOfEventsPerSpan is number of message events per span */
numberOfEventsPerSpan?: number;
}
43 changes: 43 additions & 0 deletions packages/opentelemetry-basic-tracer/src/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { BasicTracerConfig } from './types';
import {
DEFAULT_MAX_ATTRIBUTES_PER_SPAN,
DEFAULT_MAX_EVENTS_PER_SPAN,
DEFAULT_MAX_LINKS_PER_SPAN,
} from './config';
import { defaultConfig } from './config';

/**
* Function to merge Default configuration (as specified in './config') with
* user provided configurations.
*/
export function mergeConfig(userConfig: BasicTracerConfig) {
const traceParams = userConfig.traceParams;
const target = Object.assign({}, defaultConfig, userConfig);

// the user-provided value will be used to extend the default value.
if (traceParams) {
target.traceParams.numberOfAttributesPerSpan =
traceParams.numberOfAttributesPerSpan || DEFAULT_MAX_ATTRIBUTES_PER_SPAN;
target.traceParams.numberOfEventsPerSpan =
traceParams.numberOfEventsPerSpan || DEFAULT_MAX_EVENTS_PER_SPAN;
target.traceParams.numberOfLinksPerSpan =
traceParams.numberOfLinksPerSpan || DEFAULT_MAX_LINKS_PER_SPAN;
}
return target;
}
53 changes: 53 additions & 0 deletions packages/opentelemetry-basic-tracer/test/BasicTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,59 @@ describe('BasicTracer', () => {
assert.ok(tracer instanceof BasicTracer);
});

it('should construct an instance with default trace params', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
assert.deepStrictEqual(tracer.getActiveTraceParams(), {
numberOfAttributesPerSpan: 32,
numberOfEventsPerSpan: 128,
numberOfLinksPerSpan: 32,
});
});

it('should construct an instance with customized numberOfAttributesPerSpan trace params', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
traceParams: {
numberOfAttributesPerSpan: 100,
},
});
assert.deepStrictEqual(tracer.getActiveTraceParams(), {
numberOfAttributesPerSpan: 100,
numberOfEventsPerSpan: 128,
numberOfLinksPerSpan: 32,
});
});

it('should construct an instance with customized numberOfEventsPerSpan trace params', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
traceParams: {
numberOfEventsPerSpan: 300,
},
});
assert.deepStrictEqual(tracer.getActiveTraceParams(), {
numberOfAttributesPerSpan: 32,
numberOfEventsPerSpan: 300,
numberOfLinksPerSpan: 32,
});
});

it('should construct an instance with customized numberOfLinksPerSpan trace params', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
traceParams: {
numberOfLinksPerSpan: 10,
},
});
assert.deepStrictEqual(tracer.getActiveTraceParams(), {
numberOfAttributesPerSpan: 32,
numberOfEventsPerSpan: 128,
numberOfLinksPerSpan: 10,
});
});

it('should construct an instance with default attributes', () => {
const tracer = new BasicTracer({
defaultAttributes: {
Expand Down

0 comments on commit 52a86c9

Please sign in to comment.