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 e35ff55
Showing 1 changed file with 24 additions and 36 deletions.
60 changes: 24 additions & 36 deletions packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,23 +307,10 @@ 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 +408,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,9 +428,25 @@ export class HttpPlugin extends BasePlugin<Http> {
}

private _startHttpSpan(name: string, options: SpanOptions) {
const span = this._tracer
.startSpan(name, options)
.setAttribute(AttributeNames.COMPONENT, this.component);
/*
* 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;
let span: Span;
if (hasParent === false && requireParent === true) {
const spanContext =
getExtractedSpanContext(context.active()) ?? plugin._emptySpanContext;
span = new NoRecordingSpan(spanContext);
} else {
span = this._tracer
.startSpan(name, options)
.setAttribute(AttributeNames.COMPONENT, this.component);
}
this._spanNotEnded.add(span);
return span;
}
Expand Down

0 comments on commit e35ff55

Please sign in to comment.