Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit dde86d3

Browse files
authored
feat: rename TraceAgent/TraceApi to Tracer (#815)
BREAKING CHANGE: `TraceAgent` has been renamed to `Tracer`. In plugins, `Patch` has been renamed `Monkeypatch`, and `Patch` is now `Monkeypatch|Intercept` (this is a rename of `Instrumentation`). There are no user-visible JS changes. `TraceAgent` is a misnomer since the module as a whole is the "agent", not the object that is returned when the Trace Agent is started. (This is already reflected in the docs, where it has been called `TraceApi`). This change makes things consistent so it is called `Tracer` everywhere. The one implementation of `TraceAgent` (which was also called `TraceAgent`) is now `StackdriverTracer`. This object is never constructed by the user, so this is a no-op change for JS users. A couple of unused imports have also been removed.
1 parent 4ac6ded commit dde86d3

22 files changed

Lines changed: 126 additions & 131 deletions

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ require('@google-cloud/trace-agent').start({
4343
The object returned by `start()` may be used to create [custom trace spans](#custom-tracing-api):
4444

4545
```js
46-
const traceApi = require('@google-cloud/trace-agent').start();
47-
traceApi.runInRootSpan({ name: 'my-root-span' }, (rootSpan) => {
46+
const tracer = require('@google-cloud/trace-agent').start();
47+
tracer.runInRootSpan({ name: 'my-root-span' }, (rootSpan) => {
4848
// ...
4949
rootSpan.endSpan();
5050
});
@@ -99,22 +99,22 @@ For any of the web frameworks for which we provide [built-in plugins](#what-gets
9999

100100
### Accessing the API
101101

102-
Calling the `start` function returns an instance of `TraceApi`, which provides an interface for tracing:
102+
Calling the `start` function returns an instance of `Tracer`, which provides an interface for tracing:
103103

104104
```js
105-
const traceApi = require('@google-cloud/trace-agent').start();
105+
const tracer = require('@google-cloud/trace-agent').start();
106106
```
107107

108108
It can also be retrieved by subsequent calls to `get` elsewhere:
109109

110110
```js
111111
// after start() is called
112-
const traceApi = require('@google-cloud/trace-agent').get();
112+
const tracer = require('@google-cloud/trace-agent').get();
113113
```
114114

115-
A `TraceApi` object is guaranteed to be returned by both of these calls, even if the agent is disabled.
115+
A `Tracer` object is guaranteed to be returned by both of these calls, even if the agent is disabled.
116116

117-
A fully detailed overview of the `TraceApi` object is available [here](doc/trace-api.md).
117+
A fully detailed overview of the `Tracer` object is available [here](doc/trace-api.md).
118118

119119
## How does automatic tracing work?
120120

doc/trace-api.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
# The `TraceApi` Object
1+
# The `Tracer` Object
22

3-
A `TraceApi` instance provides functions that facilitate the following:
3+
A `Tracer` instance provides functions that facilitate the following:
44

55
- Creating trace spans and add labels to them.
66
- Getting information about how the trace agent was configured in the current application.
77
- Parsing and serializing trace contexts to support distributed tracing between microservices.
88
- Binding callbacks and event emitters in order to propagate trace contexts across asynchronous boundaries.
99

10-
In addition to the above, `TraceApi` also provides a number of well-known label keys and constants through its `labels` and `constants` fields respectively.
10+
In addition to the above, `Tracer` also provides a number of well-known label keys and constants through its `labels` and `constants` fields respectively.
1111

1212
## Trace Spans
1313

1414
These functions provide the capability to create trace spans, add labels to them, and close them.
1515

16-
* `TraceApi#runInRootSpan(options, fn)`
16+
* `Tracer#runInRootSpan(options, fn)`
1717
* `options`: [`TraceOptions`](#trace-span-options)
1818
* `fn`: `function(Span): any`
1919
* Returns `any` (return value of `fn`)
2020
* Creates a root span and runs the given callback, passing it a `Span` object. In some instances, this `Span` object doesn't correspond to an actual trace span; this can be checked by consulting the value of `Span#type`:
21-
* `TraceApi#spanTypes.ROOT`: This object corresponds to a real trace span.
22-
* `TraceApi#spanTypes.UNTRACED`: There isn't a real trace span corresponding to this object, for one of the following reasons:
21+
* `Tracer#spanTypes.ROOT`: This object corresponds to a real trace span.
22+
* `Tracer#spanTypes.UNTRACED`: There isn't a real trace span corresponding to this object, for one of the following reasons:
2323
* The trace policy, as specified by the user-given configuration, disallows a root span from being created under the current circumstances.
2424
* The trace agent is disabled, either because it wasn't started at all, started in disabled mode, or encountered an initialization error.
2525
* The incoming request had headers that explicitly specified that this request shouldn't be traced.
26-
* `TraceApi#spanTypes.UNCORRELATED`: `runInRootSpan` was called for a request that already has a root span. This likely indicates a programmer error, as nested root spans are not allowed.
26+
* `Tracer#spanTypes.UNCORRELATED`: `runInRootSpan` was called for a request that already has a root span. This likely indicates a programmer error, as nested root spans are not allowed.
2727
* **Note:** You must call `endSpan` on the span object provided as an argument for the span to be recorded.
28-
* `TraceApi#createChildSpan(options)`
28+
* `Tracer#createChildSpan(options)`
2929
* `options`: [`TraceOptions`](#trace-span-options)
3030
* Returns `Span`
3131
* Creates a child `Span` object and returns it. In some instances, this `Span` object doesn't correspond to an actual trace span; this can be checked by consulting the value of `Span#type`:
32-
* `TraceApi#spanTypes.CHILD`: This object corresponds to a real trace span.
33-
* `TraceApi#spanTypes.UNTRACED`: There isn't a real trace span corresponding to this object, because this span's parent is also an `UNTRACED` (root) span.
34-
* `TraceApi#spanTypes.UNCORRELATED`: There isn't a real trace span corresponding to this object, for one of the following reasons:
32+
* `Tracer#spanTypes.CHILD`: This object corresponds to a real trace span.
33+
* `Tracer#spanTypes.UNTRACED`: There isn't a real trace span corresponding to this object, because this span's parent is also an `UNTRACED` (root) span.
34+
* `Tracer#spanTypes.UNCORRELATED`: There isn't a real trace span corresponding to this object, for one of the following reasons:
3535
* A root span wasn't created beforehand because `runInRootSpan` was not called at all. This likely indicates a programmer error, because child spans should always be nested within a root span.
3636
* A root span was created beforehand, but context was lost between then and now. This may also be a programmer error, because child spans should always be created within the context of a root span. See [`Context Propagation`](#context-propagation) for details on properly propagating root span context.
3737
* **Note:** You must call `endSpan` on the returned span object for the span to be recorded.
38-
* `TraceApi#spanTypes`
38+
* `Tracer#spanTypes`
3939
* An enumeration of the types of spans: `ROOT`, `CHILD`, `UNTRACED`, `UNCORRELATED`
4040
* `Span#addLabel(key, value)`
4141
* `key`: `string`
@@ -64,7 +64,7 @@ Some functions above accept a `TraceOptions` object, which has the following fie
6464

6565
## Trace Agent Configuration
6666

67-
* `TraceApi#enhancedDatabaseReportingEnabled()`
67+
* `Tracer#enhancedDatabaseReportingEnabled()`
6868
* Returns `boolean`
6969
* Returns whether the trace agent was started with an enhanced level of reporting. See the [configuration][config-js] object definition for more details.
7070

@@ -78,11 +78,11 @@ Trace context is sent and received using the [`'x-cloud-trace-context'`][stackdr
7878

7979
Plugins that trace incoming HTTP requests (in other words, web frameworks) should support cross-service tracing by reading serialized trace context from the `'x-cloud-trace-context'` header, and supplying it as the [`traceContext` option](#trace-span-options) when creating a new root span. The trace agent will automatically deserialize the trace context and associate any new spans with it.
8080

81-
The string `'x-cloud-trace-context'` is provided as `TraceApi#constants.TRACE_CONTEXT_HEADER_NAME`.
81+
The string `'x-cloud-trace-context'` is provided as `Tracer#constants.TRACE_CONTEXT_HEADER_NAME`.
8282

8383
It is highly recommended for plugins to set this header field in responses, _if_ the incoming request has this header. The trace context that should be written can be obtained with the following function:
8484

85-
* `TraceApi#getResponseTraceContext(incomingTraceContext, isTraced)`
85+
* `Tracer#getResponseTraceContext(incomingTraceContext, isTraced)`
8686
* `incomingTraceContext`: `string`
8787
* `isTraced`: `boolean`
8888
* Returns `string`

src/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ export enum SpanType {
7070
UNTRACED = 'UNTRACED',
7171

7272
/**
73-
* This span object was created by TraceAgent#runInRootSpan, and represents
74-
* an incoming request.
73+
* This span object was created by StackdriverTracer#runInRootSpan, and
74+
* represents an incoming request.
7575
*/
7676
ROOT = 'ROOT',
7777

7878
/**
79-
* This span object was created by TraceAgent#createChildSpan, and represents
80-
* an outgoing RPC on behalf of an incoming request.
79+
* This span object was created by StackdriverTracer#createChildSpan, and
80+
* represents an outgoing RPC on behalf of an incoming request.
8181
*/
8282
CHILD = 'CHILD'
8383
}

src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import {Config, defaultConfig} from './config';
2323
import * as extend from 'extend';
2424
import * as path from 'path';
2525
import * as PluginTypes from './plugin-types';
26-
import {tracing, Tracing, NormalizedConfig} from './tracing';
27-
import {Singleton, FORCE_NEW, Forceable} from './util';
26+
import {Tracing, NormalizedConfig} from './tracing';
27+
import {FORCE_NEW, Forceable} from './util';
2828
import {Constants} from './constants';
29-
import {TraceAgent} from './trace-api';
29+
import {StackdriverTracer} from './trace-api';
3030

3131
export {Config, PluginTypes};
3232

33-
let traceAgent: TraceAgent;
33+
let traceAgent: StackdriverTracer;
3434

3535
/**
3636
* Normalizes the user-provided configuration object by adding default values
@@ -100,7 +100,7 @@ function initConfig(projectConfig: Forceable<Config>):
100100
* @example
101101
* trace.start();
102102
*/
103-
export function start(config?: Config): PluginTypes.TraceAgent {
103+
export function start(config?: Config): PluginTypes.Tracer {
104104
const normalizedConfig = initConfig(config || {});
105105
// Determine the preferred context propagation mechanism, as
106106
// continuation-local-storage should be loaded before any modules that do I/O.
@@ -111,7 +111,7 @@ export function start(config?: Config): PluginTypes.TraceAgent {
111111
}
112112

113113
if (!traceAgent) {
114-
traceAgent = new (require('./trace-api').TraceAgent)();
114+
traceAgent = new (require('./trace-api').StackdriverTracer)();
115115
}
116116

117117
try {
@@ -135,12 +135,12 @@ export function start(config?: Config): PluginTypes.TraceAgent {
135135
}
136136

137137
/**
138-
* Get the previously created TraceAgent object.
138+
* Get the previously created StackdriverTracer object.
139139
* @returns An object exposing functions for creating custom spans.
140140
*/
141-
export function get(): PluginTypes.TraceAgent {
141+
export function get(): PluginTypes.Tracer {
142142
if (!traceAgent) {
143-
traceAgent = new (require('./trace-api').TraceAgent)();
143+
traceAgent = new (require('./trace-api').StackdriverTracer)();
144144
}
145145
return traceAgent;
146146
}

src/plugin-types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
// This file only describes public-facing interfaces.
1718
// tslint:disable:no-any
1819

1920
import {Constants, SpanType} from './constants';
@@ -106,7 +107,7 @@ export interface RootSpanOptions extends SpanOptions {
106107
traceContext?: string|null;
107108
}
108109

109-
export interface TraceAgent {
110+
export interface Tracer {
110111
/**
111112
* Gets the value of enhancedDatabaseReporting in the trace agent's
112113
* configuration object.
@@ -228,19 +229,19 @@ export interface TraceAgent {
228229
};
229230
}
230231

231-
export interface Patch<T> {
232+
export interface Monkeypatch<T> {
232233
file?: string;
233234
versions?: string;
234-
patch: (module: T, agent: TraceAgent) => void;
235+
patch: (module: T, agent: Tracer) => void;
235236
unpatch?: (module: T) => void;
236237
}
237238

238239
export interface Intercept<T> {
239240
file?: string;
240241
versions?: string;
241-
intercept: (module: T, agent: TraceAgent) => T;
242+
intercept: (module: T, agent: Tracer) => T;
242243
}
243244

244-
export type Instrumentation<T> = Patch<T>|Intercept<T>;
245+
export type Patch<T> = Monkeypatch<T>|Intercept<T>;
245246

246-
export type Plugin = Array<Instrumentation<any>>;
247+
export type Plugin = Array<Patch<any>>;

src/plugins/plugin-connect.ts

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

1717
import {IncomingMessage, ServerResponse} from 'http';
18-
import * as shimmer from 'shimmer';
1918
import {parse as urlParse} from 'url';
2019

2120
import {PluginTypes} from '..';
@@ -37,7 +36,7 @@ function getFirstHeader(req: IncomingMessage, key: string): string|null {
3736
return headerValue;
3837
}
3938

40-
function createMiddleware(api: PluginTypes.TraceAgent):
39+
function createMiddleware(api: PluginTypes.Tracer):
4140
connect_3.NextHandleFunction {
4241
return function middleware(req: Request, res, next) {
4342
const options = {

src/plugins/plugin-express.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const methods: Array<keyof express_4.Application> =
3030

3131
const SUPPORTED_VERSIONS = '4.x';
3232

33-
function patchModuleRoot(express: Express4Module, api: PluginTypes.TraceAgent) {
33+
function patchModuleRoot(express: Express4Module, api: PluginTypes.Tracer) {
3434
const labels = api.labels;
3535
function middleware(
3636
req: express_4.Request, res: express_4.Response,
@@ -106,6 +106,6 @@ const plugin: PluginTypes.Plugin = [{
106106
versions: SUPPORTED_VERSIONS,
107107
patch: patchModuleRoot,
108108
unpatch: unpatchModuleRoot
109-
} as PluginTypes.Patch<Express4Module>];
109+
} as PluginTypes.Monkeypatch<Express4Module>];
110110

111111
export = plugin;

src/plugins/plugin-grpc.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as grpcModule from 'grpc'; // for types only.
1919
import {Client, MethodDefinition, ServerReadableStream, ServerUnaryCall, StatusObject} from 'grpc';
2020
import * as shimmer from 'shimmer';
2121

22-
import {Plugin, RootSpan, RootSpanOptions, Span, TraceAgent} from '../plugin-types';
22+
import {Plugin, RootSpan, RootSpanOptions, Span, Tracer} from '../plugin-types';
2323

2424
// Re-definition of Metadata with private fields
2525
type Metadata = grpcModule.Metadata&{
@@ -97,7 +97,7 @@ const SKIP_FRAMES = 1;
9797
// tslint:disable-next-line:variable-name
9898
let MetadataModuleValue: MetadataModule;
9999

100-
function patchMetadata(metadata: MetadataModule, api: TraceAgent) {
100+
function patchMetadata(metadata: MetadataModule, api: Tracer) {
101101
// metadata is the value of module.exports of src/node/src/metadata.js
102102
MetadataModuleValue = metadata;
103103
}
@@ -107,7 +107,7 @@ function unpatchMetadata() {
107107
// So it's safe to provide a no-op unpatch function.
108108
}
109109

110-
function patchClient(client: ClientModule, api: TraceAgent) {
110+
function patchClient(client: ClientModule, api: Tracer) {
111111
/**
112112
* Set trace context on a Metadata object if it exists.
113113
* @param metadata The Metadata object to which a trace context should be
@@ -281,7 +281,7 @@ function unpatchClient(client: ClientModule) {
281281
shimmer.unwrap(client, 'makeClientConstructor');
282282
}
283283

284-
function patchServer(server: ServerModule, api: TraceAgent) {
284+
function patchServer(server: ServerModule, api: Tracer) {
285285
/**
286286
* Returns a trace context on a Metadata object if it exists and is
287287
* well-formed, or null otherwise. The result will be encoded as a string.

src/plugins/plugin-hapi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function getFirstHeader(req: IncomingMessage, key: string): string|null {
4343
}
4444

4545
function instrument<T>(
46-
api: PluginTypes.TraceAgent, request: hapi_16.Request|hapi_17.Request,
46+
api: PluginTypes.Tracer, request: hapi_16.Request|hapi_17.Request,
4747
continueCb: () => T): T {
4848
const req = request.raw.req;
4949
const res = request.raw.res;
@@ -124,7 +124,7 @@ const plugin: PluginTypes.Plugin = [
124124
unpatch: (hapi) => {
125125
shimmer.unwrap(hapi.Server.prototype, 'connection');
126126
}
127-
} as PluginTypes.Patch<Hapi16Module>,
127+
} as PluginTypes.Monkeypatch<Hapi16Module>,
128128
/**
129129
* In Hapi 17, the work that is done on behalf of a request stems from
130130
* Request#_execute. We patch that function to ensure that context is
@@ -153,6 +153,6 @@ const plugin: PluginTypes.Plugin = [
153153
Request.prototype._execute = Request.prototype._execute[ORIGINAL]!;
154154
}
155155
}
156-
} as PluginTypes.Patch<{prototype: Hapi17Request}>
156+
} as PluginTypes.Monkeypatch<{prototype: Hapi17Request}>
157157
];
158158
export = plugin;

src/plugins/plugin-http.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as semver from 'semver';
2222
import * as shimmer from 'shimmer';
2323
import * as url from 'url';
2424

25-
import {Plugin, TraceAgent} from '../plugin-types';
25+
import {Plugin, Tracer} from '../plugin-types';
2626

2727
type HttpModule = typeof httpModule;
2828
type HttpsModule = typeof httpsModule;
@@ -78,14 +78,13 @@ function extractUrl(
7878
}
7979

8080
// tslint:disable-next-line:no-any
81-
function isTraceAgentRequest(options: any, api: TraceAgent) {
81+
function isTraceAgentRequest(options: any, api: Tracer) {
8282
return options && options.headers &&
8383
!!options.headers[api.constants.TRACE_AGENT_REQUEST_HEADER];
8484
}
8585

8686
function makeRequestTrace(
87-
protocol: string, request: RequestFunction,
88-
api: TraceAgent): RequestFunction {
87+
protocol: string, request: RequestFunction, api: Tracer): RequestFunction {
8988
// On Node 8+ we use the following function to patch both request and get.
9089
// Here `request` may also happen to be `get`.
9190
return function requestTrace(options, callback): ClientRequest {
@@ -187,7 +186,7 @@ function makeRequestTrace(
187186
};
188187
}
189188

190-
function patchHttp(http: HttpModule, api: TraceAgent) {
189+
function patchHttp(http: HttpModule, api: Tracer) {
191190
shimmer.wrap(http, 'request', (request) => {
192191
return makeRequestTrace('http:', request, api);
193192
});
@@ -217,7 +216,7 @@ function patchHttp(http: HttpModule, api: TraceAgent) {
217216

218217
// https.get depends on Node http internals in 8.9.0 and 9+ instead of the
219218
// public http module.
220-
function patchHttps(https: HttpsModule, api: TraceAgent) {
219+
function patchHttps(https: HttpsModule, api: Tracer) {
221220
shimmer.wrap(https, 'request', (request) => {
222221
return makeRequestTrace('https:', request, api);
223222
});

0 commit comments

Comments
 (0)