Skip to content

Commit

Permalink
refactor: address obecny suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Apr 25, 2020
1 parent 17c8991 commit be6e3cd
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,23 +307,7 @@ export class HttpPlugin extends BasePlugin<Http> {
};

return context.with(propagation.extract(headers), () => {
let span: Span;
const hasParent = plugin._tracer.getCurrentSpan() !== undefined;
/*
* If a parent is required but not present, we use a `NoRecordingSpan` to still
* propagate context without recording it.
*/
if (
plugin._config.requireParentforIncomingSpans === true &&
hasParent === false
) {
const spanContext =
getExtractedSpanContext(context.active()) ??
plugin._emptySpanContext;
span = new NoRecordingSpan(spanContext);
} else {
span = plugin._startHttpSpan(`${method} ${pathname}`, spanOptions);
}
const span = plugin._startHttpSpan(`${method} ${pathname}`, spanOptions);

return plugin._tracer.withSpan(span, () => {
context.bind(request);
Expand Down Expand Up @@ -421,22 +405,7 @@ export class HttpPlugin extends BasePlugin<Http> {
const spanOptions: SpanOptions = {
kind: SpanKind.CLIENT,
};
const hasParent = plugin._tracer.getCurrentSpan() !== undefined;
let span: Span;
/*
* If a parent is required but not present, we use a `NoRecordingSpan` to still
* propagate context without recording it.
*/
if (
plugin._config.requireParentforOutgoingSpans === true &&
hasParent === false
) {
const spanContext =
getExtractedSpanContext(context.active()) ?? plugin._emptySpanContext;
span = new NoRecordingSpan(spanContext);
} else {
span = plugin._startHttpSpan(operationName, spanOptions);
}
const span = plugin._startHttpSpan(operationName, spanOptions);

return plugin._tracer.withSpan(span, () => {
if (!optionsParsed.headers) optionsParsed.headers = {};
Expand All @@ -456,6 +425,18 @@ export class HttpPlugin extends BasePlugin<Http> {
}

private _startHttpSpan(name: string, options: SpanOptions) {
/*
* If a parent is required but not present, we use a `NoRecordingSpan` to still
* propagate context without recording it.
*/
const hasParent = this._tracer.getCurrentSpan() !== undefined;
const requireParent = options.kind === SpanKind.CLIENT ?
this._config.requireParentforOutgoingSpans : this._config.requireParentforIncomingSpans
if (hasParent === false && requireParent === true) {
const spanContext =
getExtractedSpanContext(context.active()) ?? plugin._emptySpanContext;
return new NoRecordingSpan(spanContext);
}
const span = this._tracer
.startSpan(name, options)
.setAttribute(AttributeNames.COMPONENT, this.component);
Expand Down

0 comments on commit be6e3cd

Please sign in to comment.