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

getCurrentSpan() returns undefined when a span is started #1228

Closed
rbruggem opened this issue Jun 21, 2020 · 6 comments
Closed

getCurrentSpan() returns undefined when a span is started #1228

rbruggem opened this issue Jun 21, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@rbruggem
Copy link

Please answer these questions before submitting a bug report.

What version of OpenTelemetry are you using?

    "@opentelemetry/api": "^0.8.3",
    "@opentelemetry/context-base": "^0.9.0",
    "@opentelemetry/node": "^0.8.3",
    "@opentelemetry/plugin-express": "^0.8.0",
    "@opentelemetry/plugin-http": "^0.8.3",
    "@opentelemetry/plugin-https": "^0.8.3",
    "@opentelemetry/plugin-pg": "^0.8.0",
    "@opentelemetry/plugin-pg-pool": "^0.8.0",
    "@opentelemetry/tracing": "^0.8.3",

What version of Node are you using?

v10.18.1

What did you do?

If possible, provide a recipe for reproducing the error.

        const parentSpan = tracer.startSpan('main');
        console.log(tracer.getCurrentSpan())

getCurrentSpan() returns undefined, even though the main span started on the previous line.

What did you expect to see?

tracer.getCurrentSpan() not returning undefined.

What did you see instead?

undefined

Additional context

I've got quite a large codebase that I want to instrument with opentelemetry.
I'd like to avoid passing parent spans down the call stack explicitly because that would require me to change all function signatures.
I expected const span = tracer.startSpan('name') to inject the parent if not provided.
Instead it seems like the only way for a span to be linked to its parent is const span = tracer.startSpan('name', { parent }).
I tried getCurrentSpan(), but it's always undefined.
Would be great if you could advise on how to proceed.
Thank you

@rbruggem rbruggem added the bug Something isn't working label Jun 21, 2020
@vmarchaud
Copy link
Member

I guess you didn't register a ContextManager, how did you configure the tracer exactly ? Could you show the full code used ? See this example

@rbruggem
Copy link
Author

Hi @vmarchaud, thanks for the quick response, it's very appreciated!

I do call register. register adds and enables an instance of AsyncHooksContextManager.
Here's a simple script which I run with ts-node.


const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/tracing');
const { TraceExporter } = require('@google-cloud/opentelemetry-cloud-trace-exporter');

let tracer
let exporter

export function initialise() {
    const projectId = 'HIDDEN' // I use Google Cloud Trace
    const provider = new NodeTracerProvider();

    exporter = new TraceExporter({ projectId: projectId });

    provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
    provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

    provider.register()

    opentelemetry.trace.setGlobalTracerProvider(provider);
    tracer = opentelemetry.trace.getTracer('basic')
}

function doWork() {
    const span = tracer.startSpan('doWork')

    for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
        // empty
    }

    span.setAttribute('key', 'value');
    span.addEvent('invoking doWork');

    span.end();
}

function run() {
    initialise()

    const parentSpan = tracer.startSpan('main');
    console.log('Current span')
    console.log(tracer.getCurrentSpan())

    for (let i = 0; i < 10; i += 1) {
        doWork();
    }
    parentSpan.end();
}

run()

console.log(tracer.getCurrentSpan()) prints undefined.
Also, all child spans do not link to the parent.

If I pass parentSpan to doWork, then child spans do link to their parent. Like so

// ...
function doWork(parent) {
    const span = tracer.startSpan('doWork', { parent })

    for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
        // empty
    }

    span.setAttribute('key', 'value');
    span.addEvent('invoking doWork');

    span.end();
}

function run() {
    initialise()

    const parentSpan = tracer.startSpan('main');
    console.log('Current span')
    console.log(tracer.getCurrentSpan())

    for (let i = 0; i < 10; i += 1) {
        doWork(parentSpan);
    }
    parentSpan.end();
}
// ...

But it does not happen via the context.

I suspect, but it's just a guess, that the child spans not linking to the parent (via the context) and getCurrenctSpan() returning undefined could be the same issue.

Unless I am doing something completely wrong in the script.
In any case, would you be able to advice on how best to proceed?
Thank you!

@dyladan
Copy link
Member

dyladan commented Jun 22, 2020

startSpan does not add the span to the context, it only creates a new span.

const parentSpan = tracer.startSpan('main');

tracer.withSpan(parentSpan, () => {
	console.log(tracer.getCurrentSpan());
});

@mayurkale22
Copy link
Member

Yes, that's right. You can read more in API Documentation: startSpan and withSpan.

@rbruggem
Copy link
Author

Ok, thanks guys! I'm going to close this.

@brandonros
Copy link

Does getNodeAutoInstrumentations which auto calls HttpInstrumentation auto-wrap every incoming HTTP server with a request hook that will auto start/create a root span if the request comes in with no baggage headers or not?

pichlermarc pushed a commit to dynatrace-oss-contrib/opentelemetry-js that referenced this issue Dec 15, 2023
Co-authored-by: Rauno Viskus <Rauno56@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants