Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 8cc92a4

Browse files
Peter Martonmayurkale22
authored andcommitted
feat(core): start tracer with span attributes (#514)
* feat(core): start tracer with span attributes * feat(core): rename TracerBase attributes to defaultAttributes * fix(core): tracer base config can be empty * docs(CHANGELOG): document defaultAttributes
1 parent 89f20bb commit 8cc92a4

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44

55
## Unreleased
66

7+
- Add `defaultAttributes` config to `Tracer.start(config)`
78
- Add Cumulative (`DoubleCumulative`, `Int64Cumulative`) APIs.
89

910
**This release has a breaking change. Please test your code accordingly after upgrading.**

packages/opencensus-core/src/trace/config/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {Logger} from '../../common/types';
1818
import {Exporter} from '../../exporters/types';
1919
import {Stats} from '../../stats/types';
2020
import {PluginNames} from '../instrumentation/types';
21+
import {Attributes} from '../model/types';
2122
import {Propagation} from '../propagation/types';
2223

2324
/** Interface configuration for a buffer. */
@@ -32,6 +33,8 @@ export interface BufferConfig {
3233

3334
/** Defines tracer configuration parameters */
3435
export interface TracerConfig {
36+
/** A set of default attributes each in the format [KEY]:[VALUE] */
37+
defaultAttributes?: Attributes;
3538
/** Determines the sampling rate. Ranges from 0.0 to 1.0 */
3639
samplingRate?: number;
3740
/** A logger object */

packages/opencensus-core/src/trace/model/tracer-base.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ export class CoreTracerBase implements types.TracerBase {
124124
if (sampleDecision) {
125125
const rootSpan =
126126
new RootSpan(this, name, kind, traceId, parentSpanId, traceState);
127+
// Add default attributes
128+
const defaultAttributes = this.config && this.config.defaultAttributes;
129+
if (defaultAttributes) {
130+
Object.keys(defaultAttributes).forEach((key) => {
131+
rootSpan.addAttribute(key, defaultAttributes[key]);
132+
});
133+
}
127134
rootSpan.start();
128135
return fn(rootSpan);
129136
}
@@ -208,7 +215,16 @@ export class CoreTracerBase implements types.TracerBase {
208215
'no current trace found - must start a new root span first');
209216
return new NoRecordSpan();
210217
}
211-
return options.childOf.startChildSpan(options.name, options.kind);
218+
const span = options.childOf.startChildSpan(options.name, options.kind);
219+
220+
// Add default attributes
221+
const defaultAttributes = this.config && this.config.defaultAttributes;
222+
if (defaultAttributes) {
223+
Object.keys(defaultAttributes).forEach((key) => {
224+
span.addAttribute(key, defaultAttributes[key]);
225+
});
226+
}
227+
return span;
212228
}
213229

214230
/** Determine whether to sample request or not. */

packages/opencensus-core/test/test-tracer-base.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,33 @@ describe('Tracer Base', () => {
375375
});
376376
});
377377

378+
/** Should add tracer attributes to every span created by tracer */
379+
describe('startRootSpan() and startChildSpan() with attributes', () => {
380+
let tracerConfig: TracerConfig;
381+
let tracer: types.TracerBase;
382+
let span: types.Span;
383+
let rootSpanLocal: types.Span;
384+
before(() => {
385+
tracer = new CoreTracerBase();
386+
tracerConfig = {
387+
...defaultConfig,
388+
defaultAttributes:
389+
{cluster_name: 'test-cluster', asg_name: 'test-asg'}
390+
};
391+
tracer.start(tracerConfig);
392+
tracer.startRootSpan(options, (rootSpan) => {
393+
rootSpanLocal = rootSpan;
394+
span = tracer.startChildSpan(
395+
{name: 'spanName', kind: types.SpanKind.CLIENT, childOf: rootSpan});
396+
});
397+
});
398+
it('should add add attributes to spans', () => {
399+
assert.deepStrictEqual(
400+
rootSpanLocal.attributes, tracerConfig.defaultAttributes);
401+
assert.deepStrictEqual(span.attributes, tracerConfig.defaultAttributes);
402+
});
403+
});
404+
378405
/** Should run eventListeners when the rootSpan ends */
379406
describe('onEndSpan()', () => {
380407
it('should run eventListeners when the rootSpan ends', () => {

packages/opencensus-nodejs/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ Tracing has many options available to choose from. At `tracing.start()`, you can
7373

7474
| Options | Type | Description |
7575
| ------- | ---- | ----------- |
76-
| [`bufferSize`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L25) | `number` | The number of traces to be collected before exporting to a backend |
77-
| [`bufferTimeout`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L27) | `number` | Maximum time to wait before exporting to a backend |
78-
| [`logger`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L29) | `Logger` | A logger object |
79-
| [`logLevel`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L47) | `number` | Level of logger - 0: disable, 1: error, 2: warn, 3: info, 4: debug |
80-
| [`samplingRate`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L35) | `number` | Determines the span's sampling rate. Ranges from 0.0 to 1.0 |
81-
| [`propagation`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L41) | `Propagation` | A propagation instance to use |
82-
| [`maximumLabelValueSize`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L52) | `number` | The maximum number of characters reported on a label value |
83-
| [`plugin`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L68) | `PluginNames` | A list of trace instrumentations plugins to load |
84-
| [`exporter`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L70) | `Exporter` | An exporter object |
76+
| [`defaultAttributes`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L37) | `Attributes` | Default attributes added to every span created by the tracer |
77+
| [`bufferSize`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L27) | `number` | The number of traces to be collected before exporting to a backend |
78+
| [`bufferTimeout`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L29) | `number` | Maximum time to wait before exporting to a backend |
79+
| [`logger`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L31) | `Logger` | A logger object |
80+
| [`logLevel`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L51) | `number` | Level of logger - 0: disable, 1: error, 2: warn, 3: info, 4: debug |
81+
| [`samplingRate`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L39) | `number` | Determines the span's sampling rate. Ranges from 0.0 to 1.0 |
82+
| [`propagation`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L43) | `Propagation` | A propagation instance to use |
83+
| [`maximumLabelValueSize`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L56) | `number` | The maximum number of characters reported on a label value |
84+
| [`plugins`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#72) | `PluginNames` | A list of trace instrumentations plugins to load |
85+
| [`exporter`](https://github.com/census-instrumentation/opencensus-node/blob/master/packages/opencensus-core/src/trace/config/types.ts#L74) | `Exporter` | An exporter object |
8586

8687
## Plugins
8788

0 commit comments

Comments
 (0)