Skip to content

Commit

Permalink
plugin inheritance explicitly specified
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-pytel committed Mar 4, 2021
1 parent 9d2480f commit a493288
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/plugins/AMQPLibPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AMQPLibPlugin implements SwPlugin {
const queue = fields.routingKey || '';
const peer = `${this.connection.stream.remoteAddress}:${this.connection.stream.remotePort}`;

const span = ContextManager.current.newExitSpan('RabbitMQ/' + topic + '/' + queue + '/Producer', peer).start();
const span = ContextManager.current.newExitSpan('RabbitMQ/' + topic + '/' + queue + '/Producer', peer, Component.RABBITMQ_PRODUCER).start();

try {
span.inject().items.forEach((item) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/AxiosPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AxiosPlugin implements SwPlugin {

defaults.adapter = (config: any) => {
const { host, pathname: operation } = new URL(config.url); // TODO: this may throw invalid URL
const span = ContextManager.current.newExitSpan(operation, host).start();
const span = ContextManager.current.newExitSpan(operation, host, Component.AXIOS, Component.HTTP).start();

let ret: any;

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ExpressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ExpressPlugin implements SwPlugin {

const carrier = ContextCarrier.from(headersMap);
const operation = (req.url || '/').replace(/\?.*/g, '');
const span = ContextManager.current.newEntrySpan(operation, carrier).start();
const span = ContextManager.current.newEntrySpan(operation, carrier, Component.HTTP_SERVER).start();

let stopped = 0;
const stopIfNotStopped = (err: Error | null) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/HttpPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class HttpPlugin implements SwPlugin {

let stopped = 0; // compensating if request aborted right after creation 'close' is not emitted
const stopIfNotStopped = () => !stopped++ ? span.stop() : null; // make sure we stop only because other events may proc along with req.'close'
const span: ExitSpan = ContextManager.current.newExitSpan(operation, host).start() as ExitSpan;
const span: ExitSpan = ContextManager.current.newExitSpan(operation, host, Component.HTTP).start() as ExitSpan;

try {
if (span.depth === 1) { // only set HTTP if this span is not overridden by a higher level one
Expand Down
8 changes: 1 addition & 7 deletions src/plugins/MongoDBPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ class MongoDBPlugin implements SwPlugin {
return false;

cursor.on('error', (err: any) => {
// span.resync(); // this may precede 'close' .resync() but its fine
span.error(err);
span.stop();
});

cursor.on('close', () => {
// span.resync(); // cursor does not .resync() until it is closed because maybe other exit spans will be opened during processing
span.stop();
});

Expand All @@ -67,8 +65,6 @@ class MongoDBPlugin implements SwPlugin {

args[idx] = function(this: any, error: any, result: any) {
if (error || !plugin.maybeHookCursor(span, result)) {
// span.resync();

if (error)
span.error(error);

Expand Down Expand Up @@ -242,7 +238,7 @@ class MongoDBPlugin implements SwPlugin {
host = '???';
}

span = ContextManager.current.newExitSpan('MongoDB/' + operation, host).start();
span = ContextManager.current.newExitSpan('MongoDB/' + operation, host, Component.MONGODB).start();

try {
span.component = Component.MONGODB;
Expand All @@ -268,14 +264,12 @@ class MongoDBPlugin implements SwPlugin {
} else {
ret = ret.then(
(res: any) => {
// span.resync();
span.stop();

return res;
},

(err: any) => {
// span.resync();
span.error(err);
span.stop();

Expand Down
6 changes: 1 addition & 5 deletions src/plugins/MySQLPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class MySQLPlugin implements SwPlugin {
Connection.prototype.query = function(sql: any, values: any, cb: any) {
const wrapCallback = (_cb: any) => {
return function(this: any, error: any, results: any, fields: any) {
// span.resync();

if (error)
span.error(error);

Expand All @@ -50,7 +48,7 @@ class MySQLPlugin implements SwPlugin {
let query: any;

const host = `${this.config.host}:${this.config.port}`;
const span = ContextManager.current.newExitSpan('mysql/query', host).start();
const span = ContextManager.current.newExitSpan('mysql/query', host, Component.MYSQL).start();

try {
span.component = Component.MYSQL;
Expand Down Expand Up @@ -122,12 +120,10 @@ class MySQLPlugin implements SwPlugin {

if (streaming) {
query.on('error', (e: any) => {
// span.resync();
span.error(e);
});

query.on('end', () => {
// span.resync(); // may have already been done in 'error' but safe to do multiple times
span.stop()
});
}
Expand Down
8 changes: 1 addition & 7 deletions src/plugins/PgPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class MySQLPlugin implements SwPlugin {
Client.prototype.query = function(config: any, values: any, callback: any) {
const wrapCallback = (_cb: any) => {
return function(this: any, err: any, res: any) {
// span.resync();

if (err)
span.error(err);

Expand All @@ -57,7 +55,7 @@ class MySQLPlugin implements SwPlugin {
let query: any;

const host = `${this.host}:${this.port}`;
const span = ContextManager.current.newExitSpan('pg/query', host).start();
const span = ContextManager.current.newExitSpan('pg/query', host, Component.POSTGRESQL).start();

try {
span.component = Component.POSTGRESQL;
Expand Down Expand Up @@ -105,27 +103,23 @@ class MySQLPlugin implements SwPlugin {
if (query) {
if (Cursor && query instanceof Cursor) {
query.on('error', (err: any) => {
// span.resync(); // this may precede 'end' .resync() but its fine
span.error(err);
span.stop();
});

query.on('end', () => {
// span.resync(); // cursor does not .resync() until it is closed because maybe other exit spans will be opened during processing
span.stop();
});

} else if (typeof query.then === 'function') { // generic Promise check
query = query.then(
(res: any) => {
// span.resync();
span.stop();

return res;
},

(err: any) => {
// span.resync();
span.error(err);
span.stop();

Expand Down
11 changes: 9 additions & 2 deletions src/trace/context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@

import Span from '../../trace/span/Span';
import Segment from '../../trace/context/Segment';
import { Component } from '../../trace/Component';
import { ContextCarrier } from './ContextCarrier';

export default interface Context {
segment: Segment;

newLocalSpan(operation: string): Span;

newEntrySpan(operation: string, carrier?: ContextCarrier): Span;
/* If 'inherit' is specified then if the span at the top of the stack is an Entry span of this component type then the
span is reused instead of a new child span being created. This is intended for situations like an express handler
inheriting an opened incoming http connection to present a single span. */
newEntrySpan(operation: string, carrier?: ContextCarrier, inherit?: Component): Span;

newExitSpan(operation: string, peer: string): Span;
/* if 'inherit' is specified then the span returned is marked for inheritance by an Exit span component which is
created later and calls this function with a matching 'component' value. For example Axios using an Http exit
connection will be merged into a single exit span, see those plugins for how this is done. */
newExitSpan(operation: string, peer: string, component: Component, inherit?: Component): Span;

start(span: Span): Context;

Expand Down
5 changes: 3 additions & 2 deletions src/trace/context/DummyContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Context from '../../trace/context/Context';
import Span from '../../trace/span/Span';
import DummySpan from '../../trace/span/DummySpan';
import Segment from '../../trace/context/Segment';
import { Component } from '../../trace/Component';
import { SpanType } from '../../proto/language-agent/Tracing_pb';
import { ContextCarrier } from './ContextCarrier';

Expand All @@ -33,11 +34,11 @@ export default class DummyContext implements Context {
segment: Segment = new Segment();
depth = 0;

newEntrySpan(operation: string, carrier?: ContextCarrier): Span {
newEntrySpan(operation: string, carrier?: ContextCarrier, inherit?: Component): Span {
return this.span;
}

newExitSpan(operation: string, peer: string): Span {
newExitSpan(operation: string, peer: string, component: Component, inherit?: Component): Span {
return this.span;
}

Expand Down
12 changes: 8 additions & 4 deletions src/trace/context/SpanContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Segment from '../../trace/context/Segment';
import EntrySpan from '../../trace/span/EntrySpan';
import ExitSpan from '../../trace/span/ExitSpan';
import LocalSpan from '../../trace/span/LocalSpan';
import { Component } from '../../trace/Component';
import { createLogger } from '../../logging';
import { executionAsyncId } from 'async_hooks';
import { ContextCarrier } from './ContextCarrier';
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class SpanContext implements Context {
return undefined;
}

newEntrySpan(operation: string, carrier?: ContextCarrier): Span {
newEntrySpan(operation: string, carrier?: ContextCarrier, inherit?: Component): Span {
let span = this.ignoreCheck(operation, SpanType.ENTRY);

if (span)
Expand All @@ -79,7 +80,7 @@ export default class SpanContext implements Context {
});
}

if (parent && parent.type === SpanType.ENTRY) {
if (parent && parent.type === SpanType.ENTRY && inherit && inherit === parent.component) {
span = parent;
parent.operation = operation;

Expand All @@ -99,7 +100,7 @@ export default class SpanContext implements Context {
return span;
}

newExitSpan(operation: string, peer: string): Span {
newExitSpan(operation: string, peer: string, component: Component, inherit?: Component): Span {
let span = this.ignoreCheck(operation, SpanType.EXIT);

if (span)
Expand All @@ -117,7 +118,7 @@ export default class SpanContext implements Context {
});
}

if (parent && parent.type === SpanType.EXIT) {
if (parent && parent.type === SpanType.EXIT && component && component === parent.inherit) {
span = parent;

} else {
Expand All @@ -130,6 +131,9 @@ export default class SpanContext implements Context {
});
}

if (inherit)
span.inherit = inherit;

return span;
}

Expand Down
1 change: 1 addition & 0 deletions src/trace/span/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default abstract class Span {
operation: string;
layer = SpanLayer.UNKNOWN;
component = Component.UNKNOWN;
inherit?: Component;

readonly tags: Tag[] = [];
readonly logs: Log[] = [];
Expand Down

0 comments on commit a493288

Please sign in to comment.