elastic / apm-agent-ruby

https://www.elastic.co/solutions/apm
Apache License 2.0
168 stars 131 forks source link

Support AWS lambda #1240

Open wjensen-godaddy opened 2 years ago

wjensen-godaddy commented 2 years ago

I haven't been able to get the agent to work on AWS Lambda. I see that there was some investigation into supporting it (https://github.com/elastic/apm-agent-ruby/pull/1168) but I didn't see anything on the project board. Any chance it will be supported in the near future?

estolfo commented 2 years ago

Hi @wjensen-godaddy, thanks for your question. We are planning on supporting AWS Lambda in the Ruby agent but we don't have a date yet for its release. I'll update here when we have more information. Thanks for your patience!

wjensen-godaddy commented 2 years ago

@estolfo Thanks for the quick response. In the meantime, is there any workaround?

estolfo commented 2 years ago

@wjensen-godaddy can you tell me what you mean by the agent doesn't work? That way, I can help you find a workaround, if there is one.

wjensen-godaddy commented 2 years ago

@estolfo When I run the agent in our Lambda, I don't get any errors, but I'm not seeing data reported to our APM server.

However when I run the agent locally, I'm able to see data reported to our APM server. So I assumed the issue was an incompatibility with Lambda after seeing https://github.com/elastic/apm-agent-ruby/pull/1168. Is it possible for the agent to run on Lambda? Perhaps I need to investigate further.

wjensen-godaddy commented 2 years ago

Hi @estolfo. I was able to get it working by not using at_exit to run ElasticAPM.stop. It seems that at_exit does not reliably fire on AWS ruby lambda. So ElasticAPM.stop wasn't being called.

estolfo commented 2 years ago

Hi @wjensen-godaddy where did you add the call to ElasticAPM.stop? I'm guessing that the problem you were experiencing was that the agent data wasn't being flushed before the lambda env was frozen due to the short-lived nature of a function. What is the average duration of your lambda function?

wjensen-godaddy commented 2 years ago

Average duration is < 400ms.

# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require_relative './serverless_rack'

def handler(event:, context:)
  app = Rack::Builder.parse_file('config.ru').first

  response = handle_request(app: app, config: {}, event: event, context: context)

  ElasticAPM.stop

  response
end
estolfo commented 2 years ago

Hi @wjensen-godaddy, thanks for sending that. If you stop the agent inside the lambda function and the environment is frozen after a call, subsequent calls to the function within that lambda environment won't be instrumented. I think you're seeing some APM data reported because the agent flushes the connection when it's stopped. I would assume that you are thus only seeing APM data for cold start function calls. Is that true? Until the Ruby agent officially instruments AWS lambda functions, you could try playing around with the api request time configuration option. Given that your functions are very short, I'm not sure this would help much but you can try as a workaround.

wjensen-godaddy commented 2 years ago

@estolfo Apologies for not seeing your last question.It's an interesting one. I had to double check if that was the case. I confirmed that we see APM data for all requests, not just during cold starts.