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

APM Python/Starlette agent not working with a GraphQL application #1302

Closed
kleysonr opened this issue Aug 20, 2021 · 8 comments
Closed

APM Python/Starlette agent not working with a GraphQL application #1302

kleysonr opened this issue Aug 20, 2021 · 8 comments
Assignees
Milestone

Comments

@kleysonr
Copy link

I'm trying to use the apm agent in a python Starlette application (https://www.elastic.co/guide/en/apm/agent/python/master/starlette-support.html)

My starlette app is based on GraphQL app (https://www.starlette.io/graphql/)

After configure the apm agent to monitor my application, I'm getting all the transactions, regardless the endpoint called, aggregated as a POST / transaction in the Kibana.

Is APM python agent prepared to work with GraphQL ? How to get the correct graphql transactions ?

Best Regards.

@basepi
Copy link
Contributor

basepi commented Aug 20, 2021

Thanks for the report! We haven't done any testing with GraphQL and Starlette. I noticed that Starlette's GraphQL support is on a deprecation path so I'm not sure that we'll be able to put a lot of time into this issue, but we'll see if it's an easy fix.

@basepi basepi transferred this issue from elastic/apm Aug 20, 2021
@basepi basepi added the feature label Aug 20, 2021
@kleysonr
Copy link
Author

kleysonr commented Aug 20, 2021

@basepi Yes. Starlette graphql support is deprecated, but still supporting mounting third-party library that provide GraphQL support, such as Ariadne / tartiflette-asgi / Strawberry / starlette-graphene3.

In my case I'm using Starlette to mount an Ariadne GraphQL application.

@kleysonr
Copy link
Author

@basepi Use de code below to run a Starlette app mounting a GraphQL Ariadne application.

ariadne==0.13.0
starlette==0.14.2
uvicorn==0.15.0
elastic-apm==6.3.3
import random
import time
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware import Middleware
from starlette.applications import Starlette
from ariadne.asgi import GraphQL
from ariadne import QueryType, gql, make_executable_schema
from elasticapm.contrib.starlette import make_apm_client, ElasticAPM

apm = make_apm_client({
    'SERVICE_NAME': 'pythonapp',
    'SECRET_TOKEN': '12345678',
    'DEBUG': True,
    'SERVER_URL': 'http://localhost:8200',
})    

type_defs = gql("""
    type Query {
        ping1: String!
        randWait1: String!
    }
""")

query = QueryType()

@query.field('ping1')
def resolve_ping(obj, info):

    # try:
    #     1 / 0
    # except ZeroDivisionError:
    #     apm.capture_exception()

    return str(time.time())

@query.field('randWait1')
def resolve_randWait(obj, info):

    w = random.randrange(1,5)
    time.sleep(w)
    return str(w)

schema = make_executable_schema(type_defs, query)

def create_app():

    middleware = [
        Middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]),
        Middleware(ElasticAPM, client=apm),
    ]

    app = Starlette(debug=True, middleware=middleware)
    app.mount(
        '/graphql',
        GraphQL(
            schema,
            debug=True,
        )
    )

    return app

app = create_app()
$ uvicorn asgi:app --port 7070 --host 0.0.0.0

Then open http://localhost:7070/graphql/

@basepi
Copy link
Contributor

basepi commented Aug 23, 2021

Thanks for the sample code! We'll find some time to look into this. And thanks for the clarification on your use of a mounted GraphQL application.

@basepi basepi added bug and removed feature labels Aug 23, 2021
@kleysonr
Copy link
Author

@basepi Hi.

Did you have a chance to take a look on this ?

I need to bring observability into my application.

Best Regards.

@basepi
Copy link
Contributor

basepi commented Sep 27, 2021

I haven't, unfortunately. We're currently reworking how we prioritize these bugs and other community interactions -- it's so easy to fill a release with feature work and then bugs can languish. But we hope to bring more focused attention here soon!

@process0
Copy link

Can confirm the APM is not working with Ariadne + Starlette

@beniwohli beniwohli self-assigned this Jan 11, 2022
@beniwohli
Copy link
Contributor

Hey all

the issue here is that the APM agent can't determine an appropriate transaction name. For e.g. REST APIs or other hierarchical URL systems, we can use the (parametrized) URL as the transaction name. GraphQL uses the same URL endpoint for all calls, so we end up grouping all GraphQL queries together.

A while ago, we merged a PR (#850) that derives an appropriate transaction name from a GraphQL query for Graphene 2. Something similar would have to be done for Ariadne and all other GraphQL implementations mentioned here. Unfortunately, we currently don't have the resources to implement this in a timely manner, but if anybody is interested in contributing a PR, I'd be happy to review and merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants