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

Improve Tracing Setup #11524

Open
smeubank opened this issue Oct 9, 2024 · 0 comments
Open

Improve Tracing Setup #11524

smeubank opened this issue Oct 9, 2024 · 0 comments
Labels
Platform: JavaScript SDKs Type: Content Issues about the contents of our docs

Comments

@smeubank
Copy link
Member

smeubank commented Oct 9, 2024

SDK

JavaScript SDK

Description

I ran into a few issues while setting up my last distributed services

  1. CORS config on server
  2. adblocking -> tunnel config on python server

This should also be in onbaording more clearly too

Suggested Solution

  1. Update docs to state that sometimes it is configured directly in platforms like AWS, but others it can be configured directly in the server app
  • include examples in top platforms, maintaining such snippets for all will be cumbersome
  • fastAPI for example
from fastapi.middleware.cors import CORSMiddleware
. . .
# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Adjust this to your specific frontend domain
    allow_credentials=True, # potentially irrelevant for sentry
    allow_methods=["*"], # adjust to what matters to you
    allow_headers=["sentry-trace", "baggage", "*"],  # Include sentry-trace and baggage
)
  1. Add a python tunnelling example here
  • CORS issues also pop up with adding tunneling
from fastapi import FastAPI, Request, Response
from fastapi.responses import JSONResponse
import json
from urllib.parse import urlparse
import aiohttp

app = FastAPI()

# Replace with your actual Sentry host and project IDs
SENTRY_HOST = "oXXXXXX.ingest.sentry.io"
SENTRY_PROJECT_IDS = ["123456"]

@app.post("/tunnel")
async def sentry_tunnel(request: Request):
    try:
        # Read the raw bytes from the request body
        envelope_bytes = await request.body()
        
        # Decode bytes to string and split into lines
        envelope = envelope_bytes.decode('utf-8')
        pieces = envelope.split('\n')
        header_str = pieces[0]
        
        # Parse the envelope header as JSON
        header = json.loads(header_str)
        dsn = header.get('dsn', '')
        
        # Parse the DSN to extract hostname and project ID
        dsn_parsed = urlparse(dsn)
        hostname = dsn_parsed.hostname
        project_id = dsn_parsed.path.strip('/')
        
        # Validate the hostname and project ID
        if hostname != SENTRY_HOST:
            raise Exception(f"Invalid Sentry hostname: {hostname}")
        
        if not project_id or project_id not in SENTRY_PROJECT_IDS:
            raise Exception(f"Invalid Sentry project ID: {project_id}")
        
        # Construct the upstream Sentry URL
        upstream_sentry_url = f"https://{SENTRY_HOST}/api/{project_id}/envelope/"
        
        # Forward the envelope to Sentry
        async with aiohttp.ClientSession() as session:
            async with session.post(
                upstream_sentry_url,
                data=envelope_bytes,
                headers={'Content-Type': 'application/x-sentry-envelope'}
            ) as resp:
                if resp.status != 200:
                    raise Exception(f"Upstream Sentry returned status {resp.status}")
        
        # Return success response
        return Response(status_code=200)
    except Exception as e:
        print("Error tunneling to Sentry:", e)
        return JSONResponse(content={'error': 'Error tunneling to Sentry'}, status_code=500)

  • provide an example of what a blocked request will look like in console

https://xxxx.ingest.us.sentry.io/api/xxxx/envelope/?sentry_key=xxx&sentry_version=7&sentry_client=sentry.SDK net::ERR_BLOCKED_BY_CLIENT

@smeubank smeubank added SDKs Type: Content Issues about the contents of our docs labels Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: JavaScript SDKs Type: Content Issues about the contents of our docs
Projects
None yet
Development

No branches or pull requests

1 participant