Skip to content

Commit

Permalink
feat(graph-service): better observability for worker (#15238)
Browse files Browse the repository at this point in the history
better observability for worker
  • Loading branch information
julien51 authored Dec 3, 2024
1 parent b9cb807 commit 1f49f4f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 84 deletions.
2 changes: 1 addition & 1 deletion governance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
"lint:code": "eslint .",
"lint": "yarn lint:code"
}
}
}
1 change: 0 additions & 1 deletion graph-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"dependencies": {
"@cloudflare/workers-types": "4.20241127.0",
"@sentry/node": "8.41.0",
"toucan-js": "4.0.0",
"wrangler": "3.91.0"
},
"devDependencies": {
Expand Down
79 changes: 36 additions & 43 deletions graph-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { Toucan } from 'toucan-js'
import { getSubgraphUrl } from './networks'
import { Env, GraphQLRequest } from './types'
import networks from '@unlock-protocol/networks'

export default {
async fetch(request: Request, env: Env): Promise<Response> {
// Initialize Sentry for error tracking
const sentry = new Toucan({
dsn: env.SENTRY_DSN,
request,
})

// Define CORS headers to allow cross-origin requests
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
Expand Down Expand Up @@ -65,49 +58,49 @@ export default {
})
}

try {
// Parse the incoming JSON request body
const { query, variables }: GraphQLRequest = await request.json()
// Parse the incoming JSON request body
const { query, variables }: GraphQLRequest = await request.json()

// Validate that a query is provided
if (!query) {
return new Response('Bad Request: query is required', {
status: 400,
headers: corsHeaders,
})
}

// Forward the GraphQL request to the subgraph
const graphResponse = await fetch(subgraphUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query, variables }),
// Validate that a query is provided
if (!query) {
return new Response('Bad Request: query is required', {
status: 400,
headers: corsHeaders,
})
}

// Extract response data and headers
const responseData = await graphResponse.text()
const responseHeaders = new Headers(graphResponse.headers)
// Forward the GraphQL request to the subgraph
const graphResponse = await fetch(subgraphUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query, variables }),
})

// Append CORS headers to the response
Object.entries(corsHeaders).forEach(([key, value]) => {
responseHeaders.set(key, value)
})
// Extract response data and headers
const responseData = await graphResponse.text()
const responseHeaders = new Headers(graphResponse.headers)

// Append CORS headers to the response
Object.entries(corsHeaders).forEach(([key, value]) => {
responseHeaders.set(key, value)
})

// Return the response from the subgraph
return new Response(responseData, {
if (graphResponse.status !== 200) {
console.log({
status: graphResponse.status,
headers: responseHeaders,
})
} catch (error) {
// Capture and log any errors that occur during processing
sentry.captureException(error)
console.error('Error processing request:', error)
return new Response('Internal Server Error', {
status: 500,
headers: corsHeaders,
subgraphUrl,
query,
variables,
responseData,
})
}

// Return the response from the subgraph
return new Response(responseData, {
status: graphResponse.status,
headers: responseHeaders,
})
},
}
9 changes: 8 additions & 1 deletion graph-service/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ compatibility_date = "2024-07-29"
tail_consumers = [{service = "graph-service-tail"}]

[vars]
SENTRY_DSN="https://[email protected]/4507855281979392"
SENTRY_DSN="https://[email protected]/4507855281979392"

[observability]
enabled = true
head_sampling_rate = 1 # optional. default = 1.

[observability.logs]
enabled = true
38 changes: 0 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13701,16 +13701,6 @@ __metadata:
languageName: node
linkType: hard

"@sentry/core@npm:8.9.2":
version: 8.9.2
resolution: "@sentry/core@npm:8.9.2"
dependencies:
"@sentry/types": "npm:8.9.2"
"@sentry/utils": "npm:8.9.2"
checksum: 10/f11532b9f0cceceb40f1934a23360c98d3dd7ae8a05f14e2ed5f5a247932716f5f7367aa1f6cba7dc2736b063ceaefe27d4ebe55af6e6ca593a9b39d75d1ca4e
languageName: node
linkType: hard

"@sentry/hub@npm:5.30.0":
version: 5.30.0
resolution: "@sentry/hub@npm:5.30.0"
Expand Down Expand Up @@ -13972,13 +13962,6 @@ __metadata:
languageName: node
linkType: hard

"@sentry/types@npm:8.9.2":
version: 8.9.2
resolution: "@sentry/types@npm:8.9.2"
checksum: 10/a38f4dd21330a3d4c848f94ba678ce24ce5489236286247bae351db1d97d49733cd4888b59e1b193077fe38a1bd8e04e72067e749897bfd15d657cc551333657
languageName: node
linkType: hard

"@sentry/utils@npm:5.30.0":
version: 5.30.0
resolution: "@sentry/utils@npm:5.30.0"
Expand Down Expand Up @@ -14016,15 +13999,6 @@ __metadata:
languageName: node
linkType: hard

"@sentry/utils@npm:8.9.2":
version: 8.9.2
resolution: "@sentry/utils@npm:8.9.2"
dependencies:
"@sentry/types": "npm:8.9.2"
checksum: 10/4933dbbb837ce61006ea867fea061d610c72330d04b33fd1871a9a327e8d11d8d150db34054d5fd7c316270167c17179f5bfe7eddded49cb5010b14cdffb5e75
languageName: node
linkType: hard

"@sentry/vercel-edge@npm:7.119.2":
version: 7.119.2
resolution: "@sentry/vercel-edge@npm:7.119.2"
Expand Down Expand Up @@ -31114,7 +31088,6 @@ __metadata:
dependencies:
"@cloudflare/workers-types": "npm:4.20241127.0"
"@sentry/node": "npm:8.41.0"
toucan-js: "npm:4.0.0"
vitest: "npm:2.1.8"
wrangler: "npm:3.91.0"
languageName: unknown
Expand Down Expand Up @@ -48972,17 +48945,6 @@ __metadata:
languageName: node
linkType: hard

"toucan-js@npm:4.0.0":
version: 4.0.0
resolution: "toucan-js@npm:4.0.0"
dependencies:
"@sentry/core": "npm:8.9.2"
"@sentry/types": "npm:8.9.2"
"@sentry/utils": "npm:8.9.2"
checksum: 10/900b09b5da8e3a5f19ccffa20f10de1ea2f538a2a3f5847ecfb75683d4ff161ea7d10fcf610f585be8ec7271ec8b3e3a10f44598c70e1296229bae3b11b75da7
languageName: node
linkType: hard

"tough-cookie@npm:^5.0.0":
version: 5.0.0
resolution: "tough-cookie@npm:5.0.0"
Expand Down

0 comments on commit 1f49f4f

Please sign in to comment.