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

Not Getting Data on the Dashboard #43

Open
TheMadrasTechie opened this issue Jul 30, 2024 · 7 comments
Open

Not Getting Data on the Dashboard #43

TheMadrasTechie opened this issue Jul 30, 2024 · 7 comments

Comments

@TheMadrasTechie
Copy link

I am using fastapi .
I have generated the API key and used it in the code.
I am not getting the response on the dashboard.
I have hit the APIs locally as well.
I am getting this.
No requests logged

Please help.
Thanks

@tom-draper
Copy link
Owner

I'll have a look into this. What version of the FastAPI middleware package are you using, and what kind of environment are you running on?

@TheMadrasTechie
Copy link
Author

TheMadrasTechie commented Jul 31, 2024

Thanks for thee response tom.
Your work is good.

Here is the list of libraries I am using.
fastapi 0.104.1
fastapi-analytics 1.2.2

@tom-draper
Copy link
Owner

It looks as though your requests are not making it to the server.
If you download the latest version of the fastapi-analytics package (1.2.3), and then add:

import logging
logging.basicConfig(level=logging.DEBUG)

to your FastAPI application, it will display debug messages and give us a better idea of what the problem is.

The logged requests are pushed to the server every 60 seconds, so run some test requests to one of your endpoints, wait a minute, and then post some more.

@Dev4rno
Copy link

Dev4rno commented Nov 14, 2024

Hi Tom, I seem to be having a similar issue

My FastAPI app uses a password generator to create a variety of passwords, and then uses a Jinja2Template to render the generated data with some styling.

My default route

from fastapi import FastAPI, Request, HTTPException, status
from fastapi.templating import Jinja2Templates
from generator import PasswordGenerator
from fastapi.responses import HTMLResponse

#-=-=-=-=-=-=-=-=-=-=-=-=>
# Default route
@app.get("/", response_class=HTMLResponse)
#-=-=-=-=-=-=-=-=-=-=-=-=>

async def render_all_passwords(request: Request):
    """Endpoint to render a simple HTML page with all generated passwords"""
    try:        
        return templates.TemplateResponse(
            request=request,
            name="all_passwords.html",
            context={
                "passwords": generator._generate_all_passwords(), # returns dict
                "random_method": generator._get_random_password_type(), # returns str
            }
        )
    except Exception:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Something went wrong, try again later",
        )

After setting up the app as suggested in the docs and following your suggestion of hitting an endpoint, waiting 60s and then repeating the process, I am consistently seeing:

INFO:     127.0.0.1:64498 - "GET / HTTP/1.1" 200 OK
DEBUG:api_analytics:Logging request: {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/favicon.ico', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T14:56:42.305855'}
INFO:     127.0.0.1:64498 - "GET /favicon.ico HTTP/1.1" 200 OK
DEBUG:api_analytics:Logging request: {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T14:56:54.592783'}
INFO:     127.0.0.1:64500 - "GET / HTTP/1.1" 200 OK
DEBUG:api_analytics:Logging request: {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/favicon.ico', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T14:56:54.706698'}
INFO:     127.0.0.1:64500 - "GET /favicon.ico HTTP/1.1" 200 OK
DEBUG:api_analytics:Logging request: {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T14:56:59.997922'}
DEBUG:api_analytics:Posting 11 logged requests to server: https://www.apianalytics-server.com/api/log-request
INFO:     127.0.0.1:64503 - "GET / HTTP/1.1" 200 OK
Exception in thread Thread-1 (_post_requests):
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/threading.py", line 1041, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/threading.py", line 992, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/api_analytics/core.py", line 55, in _post_requests
    response = requests.post(
        url,
    ...<6 lines>...
        timeout=10,
    )
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/requests/api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/requests/sessions.py", line 575, in request
    prep = self.prepare_request(req)
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/requests/sessions.py", line 484, in prepare_request
    p.prepare(
    ~~~~~~~~~^
        method=request.method.upper(),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<10 lines>...
        hooks=merge_hooks(request.hooks, self.hooks),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/requests/models.py", line 370, in prepare
    self.prepare_body(data, files, json)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/alexarno/mypy/pwplz/.venv/lib/python3.13/site-packages/requests/models.py", line 510, in prepare_body
    body = complexjson.dumps(json, allow_nan=False)
  File "/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ~~~~~~^^^^^
  File "/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
  File "/opt/homebrew/Cellar/python@3.13/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type set is not JSON serializable
DEBUG:api_analytics:Logging request: {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/favicon.ico', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 1, 'user_id': None, 'created_at': '2024-11-14T14:57:00.104395'}

Ive spent a while thinking this is because api_analytics isnt built to handle TemplateResponse just yet - but after printing requests_data in api_analytics/core.py Im not so sure theres an issue with the data itself:

[{'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T15:06:53.797348'}, {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/favicon.ico', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 8, 'user_id': None, 'created_at': '2024-11-14T15:06:53.921947'}, {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T15:06:59.494086'}, {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/favicon.ico', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T15:06:59.604124'}, {'hostname': 'localhost', 'ip_address': '127.0.0.1', 'path': '/', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36', 'method': 'GET', 'status': 200, 'response_time': 0, 'user_id': None, 'created_at': '2024-11-14T15:07:06.835613'}]

As a last attempt I tried to trim the default route down to the bare basics as found in the docs:

@app.get('/')
async def root():
    return {'message': 'Hello, World!'}

but I still seem to be getting the same TypeError: Object of type set is not JSON serializable, and I'm far more confused now. Can you please suggest where I might be going wrong, or what can be done differently?

And here's what pip list reveals:

INFO:     Finished server process [31521]
(.venv)  alexarno@Alexs-MacBook-Pro  ~/mypy/pwplz   main ±  pip list
Package              Version
-------------------- -----------
annotated-types      0.7.0
anyio                4.6.2.post1
api-analytics        1.2.5
argon2-cffi          23.1.0
argon2-cffi-bindings 21.2.0
bcrypt               4.2.0
certifi              2024.8.30
cffi                 1.17.1
charset-normalizer   3.4.0
click                8.1.7
fastapi              0.115.5
fastapi-analytics    1.2.3
h11                  0.14.0
httpcore             1.0.6
httptools            0.6.4
httpx                0.27.2
idna                 3.10
iniconfig            2.0.0
Jinja2               3.1.4
MarkupSafe           3.0.2
packaging            24.2
pip                  24.3.1
pluggy               1.5.0
pycparser            2.22
pydantic             2.9.2
pydantic_core        2.23.4
pytest               8.3.3
python-dotenv        1.0.1
PyYAML               6.0.2
requests             2.32.3
sniffio              1.3.1
starlette            0.41.2
typing_extensions    4.12.2
urllib3              2.2.3
uvicorn              0.30.6
uvloop               0.21.0
watchfiles           0.24.0
websockets           14.1

I ingeniously decided to ship my application before checking that the analytics were working as expected, and I've probably missed out on a chunk of traffic already 🙃

Any advice or pointers you have are greatly appreciated - cheers.

@tom-draper
Copy link
Owner

@devArno88 Ok - that's bizarre. I'll try to reproduce and let you know

@tom-draper
Copy link
Owner

@devArno88 Ah, looks as though it's when you access your API key. It's being loaded into a set.

API_ANALYTICS_KEY={os.getenv("API_ANALYTICS_KEY")}

@Dev4rno
Copy link

Dev4rno commented Nov 14, 2024

🤦🏻‍♂️ my bad, thanks for taking the time for this Tom. Gave you a follow as your work is fantastic 👏

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

3 participants