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

get tracer in aws auto instrumented lambda #106

Closed
niko-achilles opened this issue Jun 22, 2021 · 7 comments
Closed

get tracer in aws auto instrumented lambda #106

niko-achilles opened this issue Jun 22, 2021 · 7 comments

Comments

@niko-achilles
Copy link

niko-achilles commented Jun 22, 2021

Is your feature request related to a problem? Please describe.
Is there a way to use the tracing api (@opentelemetry/api ) in aws lambda in order to interact in my Lambda app with the extension Layer of OpenTelemetry, which is intalled as extension to my Lambda app ?

Describe the solution you'd like
Assumption: given the Extension Layer of OpenTelemetry for lambda does automatic instrumentation, as read the code by registering instrumentation and providing a tracerProvider: https://github.com/open-telemetry/opentelemetry-lambda/blob/c58c998138f355b1faa5fa071a645ca37895ccb3/nodejs/packages/layer/src/wrapper.ts

On top of the instrumentation that the Layer extension provides for my application in aws Lambda, without modifying my code,
i would like to use the @opentelemetry/api to get a tracer in order to create custom Spans, add Spans to an active context .
The same way as described in the specs: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#context-interaction and in the opentelemetry/api repo .

Describe alternatives you've considered

Additional context

@niko-achilles niko-achilles changed the title customize tracing in aws lambda, Distributed tracing customize tracing in aws lambda, Distributed tracing constraints ??? Jun 22, 2021
@niko-achilles
Copy link
Author

I am asking the above question because in the first experimentation with opentelemetry/api in combination with Lambda and Layer extesion installed, i get the following results:

(with this result i am out of ideas or sources to read in order to continue ... by first creating spans and adding to current active context)

So,

In method invocation of Lambda

const handler = async (event, context)=>{
const tracer = opentelemetryAPI.trace.getTracer("nameOfTracer"); // tracer is a ProxyTracer
const provider = opentelemetryAPI.trace.getTracerProvider(); // tracerProvider is a ProxyTracerProvider

const span = tracer.startSpan("do some work"); // span is undefined

const currentSpan = opentelemetryAPI.trace.getSpan(opentelemetryAPI.context.active()); // currentSpan is undefined
} 

After reading the tests of the opentelemetryAPI , i understand that when no delegate is set it should return proxy tracers and 'startSpan should return Noop Spans
Link tests: https://github.com/open-telemetry/opentelemetry-js-api/blob/main/test/proxy-implementations/proxy-tracer.test.ts

Also by logging the event object in method Lambda invocation i get Propagation Headers values for:
x-b3-spanid, x-b3-traceid, "x-b3-sampled": "1", X-Amzn-Trace-Id and traceParent

The question is how do get a Tracer from Tracer Provider in order to create spans and add spans to an active context ?

@anuraaga
Copy link
Contributor

Hi @niko-achilles - we store the lambda span into the current context here

https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts#L184

Without it other instrumentation would not connect to the trace.

So your code snippet seems like it's supposed to work. Are you able to create a small repro?

@niko-achilles
Copy link
Author

Hi @anuraaga , thanks for the link, nice to learn from this.

So,
i created a small repo here : https://github.com/niko-achilles/lambda-otel-spans/blob/main/functions/hello.js

Notice in this line that i get a ProxyTracer from provider : https://github.com/niko-achilles/lambda-otel-spans/blob/main/functions/hello.js#L25

and here the getSpanis undefined: https://github.com/niko-achilles/lambda-otel-spans/blob/main/functions/hello.js#L13

Can you help on how to get a tracer in order to create Spans and how to get the active context ?

@anuraaga
Copy link
Contributor

Thanks for the repro @niko-achilles. Just wondering, do you see the behavior on all requests, not just the first?

Here's where we set a "real" tracer

https://github.com/open-telemetry/opentelemetry-lambda/blob/main/nodejs/packages/layer/src/wrapper.ts#L88

Due to the design of detectResources though it's asynchronous, and I guess it is possible for the first execution of a handler to happen before it completes. If this is the issue, there seems to be a long-standing PR on helping with it

open-telemetry/opentelemetry-js#1484

Will ping it

@niko-achilles
Copy link
Author

hi @anuraaga

i modified the code to add timer execution, in order to
a. execute after the tracer starts a Span with the purpose to get the span created
b. execute after the tracer ends the created span with the purpose to see the app-developer created traces in X-RAY

https://github.com/niko-achilles/lambda-otel-spans/blob/main/functions/hello.js#L23

Additionally i invoked the function under oberservabilty 50 times via a script in order to be able to answer your question about
Just wondering, do you see the behavior on all requests, not just the first?

Yes the behavior is the same .
I get in the setup, results :

/* -- Tracer instance Log --
tracer ProxyTracer {
_provider: ProxyTracerProvider {},
name: 'helloTracer',
version: '1.0'
} */

and created spans as undefined

@niko-achilles
Copy link
Author

update i also added a waiting behavior before getting a tracer ,
https://github.com/niko-achilles/lambda-otel-spans/blob/main/functions/hello.js#L38

but i get the same results:
/* -- Tracer instance Log --
tracer ProxyTracer {
_provider: ProxyTracerProvider {},
name: 'helloTracer',
version: '1.0'
} */

and created spans as undefined

@niko-achilles
Copy link
Author

@anuraaga with interacting with openetelemetry-js community contrib. i managed to get information for a workaround , in order to create custome span names.

I unistalled "@opentelemetry/api": "^1.0.0" and installed "@opentelemetry/api": "^1.0.0-rc.0".

Then i get a NodeTraceProvider and with openetelemetry api i can set/get Spans .

@niko-achilles niko-achilles changed the title customize tracing in aws lambda, Distributed tracing constraints ??? get tracer in aws auto instrumented lambda Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants