Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow parent span to be null #569

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opentelemetry-plugin-dns/src/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class DnsPlugin extends BasePlugin<Dns> {
plugin._logger.debug('wrap lookup callback function and starts span');
const name = utils.getOperationName('lookup');
const span = plugin._startDnsSpan(name, {
parent: plugin._tracer.getCurrentSpan() || undefined,
parent: plugin._tracer.getCurrentSpan(),
attributes: {
[AttributeNames.PEER_HOSTNAME]: hostname,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,14 @@ export class DocumentLoad extends BasePlugin<unknown> {
const metaElement = [...document.getElementsByTagName('meta')].find(
e => e.getAttribute('name') === TRACE_PARENT_HEADER
);
const serverContext =
parseTraceParent((metaElement && metaElement.content) || '') || undefined;

const entries = this._getEntries();

const rootSpan = this._startSpan(
AttributeNames.DOCUMENT_LOAD,
PTN.FETCH_START,
entries,
{ parent: serverContext }
{ parent: parseTraceParent(metaElement?.content ?? '') }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like optional chaining was finally added in TS3.7... Cool!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only works for node apparently. It breaks the browser test.

);
if (!rootSpan) {
return;
Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry-plugin-grpc/src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ export class GrpcPlugin extends BasePlugin<grpc> {
const self = this;

const spanName = `grpc.${name.replace('/', '')}`;
const parentSpan = plugin._getSpanContext(call.metadata);
const spanOptions: SpanOptions = {
kind: SpanKind.SERVER,
parent: parentSpan || undefined,
parent: plugin._getSpanContext(call.metadata),
};

plugin._logger.debug(
Expand Down Expand Up @@ -347,11 +346,10 @@ export class GrpcPlugin extends BasePlugin<grpc> {
return function clientMethodTrace(this: grpcTypes.Client) {
const name = `grpc.${original.path.replace('/', '')}`;
const args = Array.prototype.slice.call(arguments);
const currentSpan = plugin._tracer.getCurrentSpan();
const span = plugin._tracer
.startSpan(name, {
kind: SpanKind.CLIENT,
parent: currentSpan || undefined,
parent: plugin._tracer.getCurrentSpan(),
})
.setAttribute(AttributeNames.COMPONENT, GrpcPlugin.component);
return plugin._makeGrpcClientRemoteCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function pgStartSpan(
const jdbcString = getJDBCString(client.connectionParameters);
return tracer.startSpan(name, {
kind: SpanKind.CLIENT,
parent: tracer.getCurrentSpan() || undefined,
parent: tracer.getCurrentSpan(),
attributes: {
[AttributeNames.COMPONENT]: PostgresPlugin.COMPONENT, // required
[AttributeNames.DB_INSTANCE]: client.connectionParameters.database, // required
Expand Down
4 changes: 1 addition & 3 deletions packages/opentelemetry-plugin-redis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ export const getTracedInternalSendCommand = (
this: redisTypes.RedisClient & RedisPluginClientTypes,
cmd?: RedisCommand
) {
const parentSpan = tracer.getCurrentSpan();

// New versions of redis (2.4+) use a single options object
// instead of named arguments
if (arguments.length === 1 && typeof cmd === 'object') {
const span = tracer.startSpan(`${RedisPlugin.COMPONENT}-${cmd.command}`, {
kind: SpanKind.CLIENT,
parent: parentSpan || undefined,
parent: tracer.getCurrentSpan(),
attributes: {
[AttributeNames.COMPONENT]: RedisPlugin.COMPONENT,
[AttributeNames.DB_STATEMENT]: cmd.command,
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-tracing/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class BasicTracer implements types.Tracer {
}

private _getParentSpanContext(
parent: types.Span | types.SpanContext | undefined
parent?: types.Span | types.SpanContext | null
): types.SpanContext | undefined {
if (!parent) return undefined;

Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-types/src/trace/SpanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface SpanOptions {
* A parent `SpanContext` (or `Span`, for convenience) that the newly-started
* span will be the child of.
*/
parent?: Span | SpanContext;
parent?: Span | SpanContext | null;

/** A manually specified start time for the created `Span` object. */
startTime?: number;
Expand Down