Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature: add integrations for common frameworks like FastAPI #91

Open
brettimus opened this issue Sep 23, 2023 · 0 comments
Open

Feature: add integrations for common frameworks like FastAPI #91

brettimus opened this issue Sep 23, 2023 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@brettimus
Copy link
Collaborator

Taking inspiration from the way Strawberry handles integrations with common frameworks.

For instance, they have a package called strawberry-graphql[fastapi], which allows you to import a GraphQLRouter, which you can then use directly with FastAPI as follows (docs here):

import strawberry

from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter


@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello World"


schema = strawberry.Schema(Query)

graphql_app = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")

I could imagine something similar for autometrics-py, which allows you to do the following

import strawberry

from fastapi import FastAPI
# NOTE - you'll need to `pip install 'autometrics[fastapi]` to use `MetricsRouter`
from autometrics.fastapi import MetricsRouter

metrics_app = MetricsRouter()

app = FastAPI()
app.include_router(metrics_app, prefix="/metrics")

Effectively, this MetricsRouter would be sugar for

from prometheus_client import generate_latest

# Set up a metrics endpoint for Prometheus to scrape
#   `generate_latest` returns metrics data in the Prometheus text format
@app.get("/metrics")
def metrics():
    return Response(generate_latest())
@brettimus brettimus added enhancement New feature or request good first issue Good for newcomers labels Sep 23, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant