R
Railway7mo ago
oat.lpk

GraphQL Severity Filter

Hello! Does anyone know how to filter for severity:error on GraphQL? (its the @level:error in the railway dashboard, or any other severity) I've tried a few combinations like filter: "@severity:error" or filter: "@level:error" none of which seems to work.
67 Replies
Percy
Percy7mo ago
Project ID: N/A
oat.lpk
oat.lpkOP7mo ago
N/A
Brody
Brody7mo ago
I'm sorry this has slipped through the cracks, did you still need help with this? and either way, I would love to hear your usecase, it sounds like you are trying to subscribe to the logs?
oat.lpk
oat.lpkOP7mo ago
Hi yes, thanks for the reply, no worries about the slippage. Basically I'm trying to create a graphql subscription service that looks for @level:error from the deploymentLogs. (specifically the one in severity) From playing around in railways graphql playground https://railway.app/graphiql I can't seem to filter for any severity level. Heres the code
query MyQuery {
deploymentLogs(
deploymentId: "<your-deployment-id>"
filter: "@level:error"
) {
message
severity
}
}
query MyQuery {
deploymentLogs(
deploymentId: "<your-deployment-id>"
filter: "@level:error"
) {
message
severity
}
}
oat.lpk
oat.lpkOP7mo ago
The filtering level syntax i got from this part of the doc https://docs.railway.app/guides/logs
Railway Docs
Viewing Logs | Railway Docs
Documentation for Railway
No description
oat.lpk
oat.lpkOP7mo ago
This is also checked against Observerability’s network tab in my railway’s dashboard which confirms that the syntax im using is correct
oat.lpk
oat.lpkOP7mo ago
No description
Brody
Brody7mo ago
just checked, the syntax is indeed correct. are you trying to make your own notification system for errors in your logs? or are you trying to pipe your logs to a 3rd party logging service?
oat.lpk
oat.lpkOP7mo ago
Im trying to make my own notification system to link with discord / telegram
Brody
Brody7mo ago
like this?
No description
oat.lpk
oat.lpkOP7mo ago
Something like that i suppose This brings me to my next problem 😂 , i've got the bots to trigger now, but i can't seem to create a subscription onto railway's graphql at all through code
Received: { errors: [ { message: 'Problem processing request' } ], data: null }
Received: { errors: [ { message: 'Problem processing request' } ], data: null }
import {createClient} from 'graphql-ws';
import WebSocket from 'ws';

import Env from '@/env';

const deploymentId = Env.DEVELOPMENT_ID;
const bearerToken = Env.BEARER_TOKEN;

const client = createClient({
url: 'wss://backboard.railway.app/graphql/v2',
connectionParams: {
Authorization: `Bearer ${bearerToken}`,
},
webSocketImpl: WebSocket,
});

export async function fetchDeploymentLogs() {
const graphqlQuery = `
subscription fetchLogs {
deploymentLogs(deploymentId: "${deploymentId}") {
message
severity
}
}
`;

try {
const iterator = client.iterate({
query: graphqlQuery,
});

const {value} = await iterator.next();

console.log('Received:', value);
return value.data;
} catch (error) {
console.error('Error fetching logs:', error);
throw error;
}
}
import {createClient} from 'graphql-ws';
import WebSocket from 'ws';

import Env from '@/env';

const deploymentId = Env.DEVELOPMENT_ID;
const bearerToken = Env.BEARER_TOKEN;

const client = createClient({
url: 'wss://backboard.railway.app/graphql/v2',
connectionParams: {
Authorization: `Bearer ${bearerToken}`,
},
webSocketImpl: WebSocket,
});

export async function fetchDeploymentLogs() {
const graphqlQuery = `
subscription fetchLogs {
deploymentLogs(deploymentId: "${deploymentId}") {
message
severity
}
}
`;

try {
const iterator = client.iterate({
query: graphqlQuery,
});

const {value} = await iterator.next();

console.log('Received:', value);
return value.data;
} catch (error) {
console.error('Error fetching logs:', error);
throw error;
}
}
Brody
Brody7mo ago
do you know what templates are?
oat.lpk
oat.lpkOP7mo ago
no sorry i dont What are they?
Brody
Brody7mo ago
templates are ready to use projects that you can easily deploy on railway this bot is a template my friend and i worked on, would you want to give it a try? maybe it covers your usecase
oat.lpk
oat.lpkOP7mo ago
Sure thing, would love to give it a try 🙏
Brody
Brody7mo ago
okay you will need 3 things - a railway api token - your service id of the service you want to monitor - your discord webhook url open a notepad and ill walk you though how to get the first two
oat.lpk
oat.lpkOP7mo ago
Oh um hmm im sry maybe i actually can't provide these data since this isn't my personal project 🥹, ill get back to u on this once i get an answer
Brody
Brody7mo ago
no no i dont want you to give them to me you would need to put them in a note so you can use them with the template, you are the only person who will ever have access to anything i just mentioned
oat.lpk
oat.lpkOP7mo ago
Oh ok sure thing sounds good
Brody
Brody7mo ago
perhaps i should go into more details about the template in question
oat.lpk
oat.lpkOP7mo ago
Hahah that would be nice
Brody
Brody7mo ago
the template is called locomotive and it is a service you deploy into an existing project and it will connect to railway's api and forward your logs of the desired service to either discord (as ive shown) or any generic webhook url (for services like axiom or betterstack) the service subscribes to the logs the same way you are trying to do, but handles everything for you like filtering and auto re-subscriptions. you simply deploy it into your project and provide it with the 3 things i mentioned above (and a level you want to filter) and it will send you discord embeds for your logs.
oat.lpk
oat.lpkOP7mo ago
Yep that sounds pretty close to wat im trying to do tbh
Brody
Brody7mo ago
sample screenshot of deploying the template and how you fill out the needed info
No description
Brody
Brody7mo ago
youll just need to gather the needed info in a notepad so that you have all the info in a single place when you go to deploy the template, just makes it easier
oat.lpk
oat.lpkOP7mo ago
This looks very well made
oat.lpk
oat.lpkOP7mo ago
GitHub
GitHub - FerretCode/locomotive
Contribute to FerretCode/locomotive development by creating an account on GitHub.
Brody
Brody7mo ago
yep
oat.lpk
oat.lpkOP7mo ago
btw i saw this in the observability network tab too, whats the difference environmentLogs and deploymentLogs? seems like the railway's playground doesn't have access to the environmentLogs?
Brody
Brody7mo ago
good question, environmentLogs would be a subscription that returns the logs (both build and deploy) for all the services within a given environment. deploymentLogs would return only the logs for the given deployment. correct, the playground uses the public api endpoint and that subscription is not available on the public api endpoint yet
oat.lpk
oat.lpkOP7mo ago
maybe theres a bug in the deploymentLogs for filtering then?
Brody
Brody7mo ago
im not sure tbh, but i do know that the dashboard does not call deploymentLogs instead it calls environmentLogs with a deploymentId filter but if youd like to try locomotive, let me know and ill help you find the info it needs to work, its simple!
oat.lpk
oat.lpkOP7mo ago
Yessir sure thing thanks alot for the help. I will check it out at work tmr 🙏 I see u have a cron job repo to hahha i've actually resorted to making my service a cron job for tday lol just so the log notifies
Brody
Brody7mo ago
yeah its sometimes slightly more useful than railway's cron job in some aspects since it can restart long running services like web servers where as railways cron job expects the thing being run to run and then exit, otherwise if its still running the jobs are skipped
oat.lpk
oat.lpkOP7mo ago
I see, seems like a useful repo for me too 😂
Brody
Brody7mo ago
both locomotive and my cron service come as templates, you wouldnt need to use the repo directly
oat.lpk
oat.lpkOP7mo ago
this one right? for the locomotive
No description
Brody
Brody7mo ago
yep, but make sure its deployed into a pre-existing project did you want to know where to find the info it needs now?
oat.lpk
oat.lpkOP7mo ago
Oh yes please
Brody
Brody7mo ago
i assume you know where to get your discord webhook?
oat.lpk
oat.lpkOP7mo ago
yep
Brody
Brody7mo ago
okay go ahead and put that in a note
oat.lpk
oat.lpkOP7mo ago
ok got it
Brody
Brody7mo ago
an account token and a service id have the same format (UUIDv4), make sure to denote what is what when you copy them down. - generate an account token here - https://railway.app/account/tokens and copy that token down. - open your desiered service and copy down just the serviceId found in the url /service/<service id> for context, the account token is very sensitive, but the project, environment, service ids, deployment ids are not in the least bit sensitive
oat.lpk
oat.lpkOP7mo ago
Ok found both
Brody
Brody7mo ago
hence why i had no problem showing you them here
oat.lpk
oat.lpkOP7mo ago
ah i see, thts good to know
Brody
Brody7mo ago
okay go into the same project that your desired service is in and deploy the locomotive template RAILWAY_API_KEY -> self explanatory 🤣 TRAIN -> your service id of the service you want to monitor DISCORD_WEBHOOK_URL -> also self explanatory INGEST_URL -> not needed as you are only doing discord right now, but it would be used for axiom, betterstack, etc ADDITIONAL_HEADERS -> also not needed, but would be used to authenticate with axiom, betterstack, etc when using INGEST_URL expand the pre configured variables section and you can fill out the LOGS_FILTER_DISCORD to error quick question for you, have you heard of axiom or betterstack?
oat.lpk
oat.lpkOP7mo ago
tbh no for both
Brody
Brody7mo ago
they are both logging platforms where you can sends logs to and then set up observability on, for stuff like error levels arguably better suited for this task than discord haha
oat.lpk
oat.lpkOP7mo ago
Oh btw i jst digged through discord, found one of ur chats about
webSocketImpl: class MyWebSocket extends WebSocket {
constructor(address: any, protocols: any) {
super(address, protocols, {
headers: {
Authorization: `Bearer ${bearerToken}`,
},
});
}
},
webSocketImpl: class MyWebSocket extends WebSocket {
constructor(address: any, protocols: any) {
super(address, protocols, {
headers: {
Authorization: `Bearer ${bearerToken}`,
},
});
}
},
this actually solved my problem allowing my code to run the query
Brody
Brody7mo ago
yeah i figured, but i thought locomotive would be a ready to use solution for you
oat.lpk
oat.lpkOP7mo ago
actually i was searching for environmentLog btu found that hahaha
Brody
Brody7mo ago
i dont know how closely you looked into locomotive's code but it uses the environment logs
oat.lpk
oat.lpkOP7mo ago
When i tried to use environmentLog (without passing any variable 😂 ) i get this error though Error fetching logs: [ { message: 'Cannot query field "environmentLogs" on type "Subscription". Did you mean "deploymentLogs"?', locations: [ [Object] ] } ]
export async function fetchLogs() {
const graphqlQuery = `
subscription fetchLogs {
environmentLogs(deploymentId: "${deploymentId}") {
message
severity
}
}
`;

try {
const iterator = client.iterate({
query: graphqlQuery,
});

const {value} = await iterator.next();

console.log('Received:', JSON.stringify(value));
return value.data;
} catch (error) {
console.error('Error fetching logs:', error);
throw error;
}
}
export async function fetchLogs() {
const graphqlQuery = `
subscription fetchLogs {
environmentLogs(deploymentId: "${deploymentId}") {
message
severity
}
}
`;

try {
const iterator = client.iterate({
query: graphqlQuery,
});

const {value} = await iterator.next();

console.log('Received:', JSON.stringify(value));
return value.data;
} catch (error) {
console.error('Error fetching logs:', error);
throw error;
}
}
Brody
Brody7mo ago
yeah you need to use the internal api endpoint https://github.com/FerretCode/locomotive/blob/main/main.go#L41C25-L41C69 where you able to give locomotive a try?
oat.lpk
oat.lpkOP7mo ago
Will try this out fully tmr with the team and let u know ✌️ btw where can i get the environmentId from railway?
Brody
Brody7mo ago
as long as you deploy locomotive into the same project as the service you want to get logs from, it will be autofilled for you
No description
oat.lpk
oat.lpkOP7mo ago
tysm for the help tho, do u want any specific feedback from using ur template?
Brody
Brody7mo ago
its my friends template, ive just worked on it a bunch, but for feedback, id like to know if it doesnt meet your needs, and if so, what does it need to do to meet your needs
oat.lpk
oat.lpkOP7mo ago
Ok sure thing, will get u the feedback when i work on it
Brody
Brody7mo ago
thanks!
oat.lpk
oat.lpkOP7mo ago
Hey got a chance to test out the template today, i think overall it works nicely right out the box, its something id definitely use on my personal project In the end though i decided to use my own code for querying and hooking up to discord webhook as we plan to use this repo for services othen then railway log monitoring e.g. cron job automated testing services
Brody
Brody7mo ago
okay makes sense, I'm glad it still did work though! just be sure your code can handle the resubscription with log resuming otherwise you could lose a few log lines every 10 or so minutes
oat.lpk
oat.lpkOP7mo ago
Hahah thats actually on my ticket for tmr, but now tht uve mentioned it, do u mind pointing me in teh right direction rn the log continuously spits out empty array which i ignore and just dont send them as message
Brody
Brody7mo ago
the best help I could give you would be to tell you to look at locomotive's subscription code
oat.lpk
oat.lpkOP7mo ago
Ok sure thing ill dig into that
Want results from more Discord servers?
Add your server