question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FastAPI / Starlette log shipping to APM Server

See original GitHub issue

Describe the bug: The FastAPI / Starlette client doesn’t seem to be sending data from the python logger to the APM server. I see that elasticapm.contrib.flask.ElasticAPM support logging=True as an argument, but don’t see the same for elasticapm.contrib.starlette.ElasticAPM. So I’m not sure if this is a bug or feature request.

To Reproduce

  1. Run the FastAPI app specified in main.py (see Additional context below).
  2. Send requests to the API by running.
for i in $(seq 100); do http :8000/foo foo="${i}@email.domain" bar="${i}"; done
  1. Check the elasticsearch index pattern apm-* if logged data has been added.
  2. Result: Only the message from apm_client.capture_message('hello, world!') has been uploaded, but the message from logger.warning('This is a warning') was not uploaded.

Environment (please complete the following information)

  • OS: OSX
  • Python version: 3.8
  • Framework and version: fastapi==0.54.1
  • APM Server version: 7.7.0
  • Agent version: elastic-apm==5.6.0

Additional context This is the code in main.py.

from fastapi import Body, FastAPI
from pydantic import BaseModel
from elasticapm.contrib.starlette import make_apm_client, ElasticAPM
from elasticapm.handlers.logging import LoggingFilter, Formatter
import elasticapm

# Parse loggs to be correlated with APM 
# https://www.elastic.co/guide/en/apm/agent/python/master/log-correlation.html
formatter = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
handler.addFilter(LoggingFilter())
handler.setLevel(logging.DEBUG)
logger = logging.getLogger("elasticapm")
logger.addHandler(handler)

# Create the fastapi app
app = FastAPI()

# Creating the fastapi apm middleware
# https://www.elastic.co/guide/en/apm/agent/python/master/starlette-support.html
# https://www.elastic.co/guide/en/apm/agent/python/master/configuration.html
settings = {
    'SERVER_URL':os.environ.get('APM_SERVER_URL'),
    'SERVICE_NAME':os.environ.get('APM_SEVICE_NAME'),
    'SECRET_TOKEN':os.environ.get('APM_SECRET_TOKEN'),
    'CAPTURE_BODY':'all',
    'CAPTURE_HEADERS': True,
    'DEBUG': True,
    'COLLECT_LOCAL_VARIABLES':'all',
    'ELASTIC_APM_AUTO_LOG_STACKS':True
    } 
apm_client = make_apm_client(settings)
app.add_middleware(ElasticAPM, client=apm_client)

# Below is a fast API example app
# https://fastapi.tiangolo.com/
class Foo(BaseModel):
    foo: str
    bar: str = None

@app.post("/foo", response_model=Foo)
def foo(request: Foo = Body(...)):
    result = {
        'foo': request.foo,
        'bar': request.bar,
    }
    # Log a message with the root logger
    logger.warning('This is a warning')

    # Log a generic message with capture_message:
    apm_client.capture_message('hello, world!')

    return result

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
basepicommented, Jun 4, 2020

@zero0nee We’d love to have you work on this!

However, I think the direction I’d really like to take this is to add log-shipping as a top-level feature of the agent, rather than continuing to add it to each individual instrumentation/integration. To that end, I need to find some time to crystallize the design of this in my mind a little so I can provide you some good guidance on where to start on adding this as a feature. Stay tuned, I’ll revisit this either tomorrow or next week.

2reactions
DanielFerreiraJorgecommented, Jun 6, 2020

Hi!

I too am really interested in FastAPI being on par with flask in terms of features of the apm agent. FastAPI is a really important framework to be left behind. It is not really usable right now…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Starlette/FastAPI Support | APM Python Agent ... - Elastic
Once you have configured the agent, it will automatically track transactions and capture uncaught exceptions within starlette. ... Log a generic message with ......
Read more >
Integrations — ddtrace documentation - Read the Docs
The aiohttp integration traces requests made with the client or to the server. The client is automatically instrumented while the server must be...
Read more >
Python Compatibility Requirements - Datadog Docs
The Python APM Client library follows a versioning policy that specifies the support level for the different versions of the library and Python...
Read more >
APM Server has still not connected to Elasticsearch in docker ...
I have the following docker-compose in order to store logs from fastapi. apm-server: image: docker.elastic.co/apm/apm-server:7.13.0 cap_add: ...
Read more >
Awesome Fastapi Overview
SQLAlchemy Admin (⭐609) - Admin Panel for FastAPI/Starlette that works with ... FastAPI Login (⭐383) - Account management and authentication (based on ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found