Skip to content

Commit a27c276

Browse files
committed
feat: also allow Propagation objects in config
1 parent ffd0b70 commit a27c276

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

src/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as path from 'path';
18+
import {Propagation} from './plugin-types';
1819

1920
const pluginDirectory =
2021
path.join(path.resolve(__dirname, '..'), 'src', 'plugins');
@@ -110,13 +111,12 @@ export interface Config {
110111
plugins?: {[pluginName: string]: string;};
111112

112113
/**
113-
* A require-friendly path to an OpenCensus-compatible propagation module to
114-
* support context propagation across services with HTTP headers, or a list of
115-
* such paths. Any user-provided value will replace the default value. To
116-
* disable propagation completely, pass an empty string or empty array for
117-
* this field.
114+
* Either a require-friendly path to an OpenCensus-compatible propagation
115+
* module to support context propagation across services with HTTP headers or
116+
* the module itself, or a list of such paths or modules. Any user-provided
117+
* value will replace the default value.
118118
*/
119-
propagation?: string|string[];
119+
propagation?: string|Propagation|Array<string|Propagation>;
120120

121121
/**
122122
* The max number of frames to include on traces; pass a value of 0 to

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function initConfig(projectConfig: Forceable<Config>):
104104
// Canonicalize the user-specified propagation mechanism.
105105
if (!config.propagation) {
106106
config.propagation = [];
107-
} else if (typeof config.propagation === 'string') {
107+
} else if (!Array.isArray(config.propagation)) {
108108
config.propagation = [config.propagation];
109109
}
110110

src/trace-api.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export enum TraceContextHeaderBehavior {
5959
export interface StackdriverTracerConfig extends TracePolicyConfig {
6060
enhancedDatabaseReporting: boolean;
6161
contextHeaderBehavior: TraceContextHeaderBehavior;
62-
propagation: string[];
62+
propagation: Array<string|Propagation>;
6363
rootSpanNameOverride: (path: string) => string;
6464
spansPerTraceSoftLimit: number;
6565
spansPerTraceHardLimit: number;
@@ -159,8 +159,13 @@ export class StackdriverTracer implements Tracer {
159159
this.config = config;
160160
this.policy = new TracePolicy(config);
161161
this.enabled = true;
162-
this.propagationModules =
163-
config.propagation.map(propModulePath => require(propModulePath));
162+
this.propagationModules = config.propagation.map(propModule => {
163+
if (typeof propModule === 'string') {
164+
return require(propModule);
165+
} else {
166+
return propModule;
167+
}
168+
});
164169
}
165170

166171
/**

test/test-config.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as testTraceModule from './trace';
2424
import { NormalizedConfig } from '../src/tracing';
2525
import { StackdriverTracer } from '../src/trace-api';
2626
import {Logger} from '../src/logger';
27+
import { Propagation } from '../src/plugin-types';
2728

2829
describe('Behavior set by config for CLS', () => {
2930
const useAH = semver.satisfies(process.version, '>=8');
@@ -133,16 +134,21 @@ describe('Behavior set by config for Tracer', () => {
133134
});
134135

135136
describe('Propagation behavior', () => {
137+
const propagationString = `${__dirname}/fixtures/propagation-local`;
138+
const fakePropagation = { extract: () => null, inject: () => {} };
136139
const testCases: Array<{
137140
// tslint:disable-next-line:no-any
138141
input: any;
139-
expected: string[];
142+
expected: Array<string|Propagation>;
140143
}> = [{
141-
input: 'hi',
142-
expected: ['hi']
144+
input: propagationString,
145+
expected: [propagationString]
143146
}, {
144-
input: ['hi', 'bye'],
145-
expected: ['hi', 'bye']
147+
input: [propagationString, fakePropagation],
148+
expected: [propagationString, fakePropagation]
149+
}, {
150+
input: fakePropagation,
151+
expected: [fakePropagation]
146152
}, {
147153
input: '',
148154
expected: []

test/test-trace-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ describe('Trace Interface', () => {
360360
it('should support multiple mechanisms if provided', () => {
361361
const tracer = createTraceAgent({
362362
propagation: [
363-
`${__dirname}/fixtures/propagation-local`,
363+
require(`${__dirname}/fixtures/propagation-local`),
364364
'@opencensus/propagation-stackdriver'
365365
]
366366
});

0 commit comments

Comments
 (0)