2 years ago
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.
94 Replies
2 years ago
I'm sorry this has slipped through the cracks, did you still need help with this?
2 years ago
and either way, I would love to hear your usecase, it sounds like you are trying to subscribe to the logs?
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: ""
filter: "@level:error"
) {
message
severity
}
}The filtering level syntax i got from this part of the doc https://docs.railway.app/guides/logs

This is also checked against Observerability’s network tab in my railway’s dashboard which confirms that the syntax im using is correct
2 years 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?
Im trying to make my own notification system to link with discord / telegram
2 years ago
like this?

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 }
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;
}
}2 years ago
do you know what templates are?
2 years ago
templates are ready to use projects that you can easily deploy on railway
2 years ago
this bot is a template my friend and i worked on, would you want to give it a try? maybe it covers your usecase
2 years 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
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
2 years ago
no no i dont want you to give them to me
2 years ago
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
2 years ago
perhaps i should go into more details about the template in question
2 years 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.
2 years ago
sample screenshot of deploying the template and how you fill out the needed info

2 years 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
https://github.com/FerretCode/locomotive/tree/main
this repo right?
2 years ago
yep
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?
2 years 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.
2 years ago
correct, the playground uses the public api endpoint and that subscription is not available on the public api endpoint yet
2 years ago
im not sure tbh, but i do know that the dashboard does not call deploymentLogs instead it calls environmentLogs with a deploymentId filter
2 years ago
but if youd like to try locomotive, let me know and ill help you find the info it needs to work, its simple!
Yessir sure thing thanks alot for the help. I will check it out at work tmr 🙏
i've actually resorted to making my service a cron job for tday lol just so the log notifies
2 years 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
2 years ago
both locomotive and my cron service come as templates, you wouldnt need to use the repo directly
2 years ago
yep, but make sure its deployed into a pre-existing project
2 years ago
did you want to know where to find the info it needs now?
2 years ago
i assume you know where to get your discord webhook?
2 years ago
okay go ahead and put that in a note
2 years 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 - and copy that token down.
open your desiered service and copy down just the serviceId found in the url
/service/
2 years ago
for context, the account token is very sensitive, but the project, environment, service ids, deployment ids are not in the least bit sensitive
2 years ago
hence why i had no problem showing you them here
2 years ago
okay go into the same project that your desired service is in and deploy the locomotive templateRAILWAY_API_KEY -> self explanatory 🤣 TRAIN -> your service id of the service you want to monitorDISCORD_WEBHOOK_URL -> also self explanatoryINGEST_URL -> not needed as you are only doing discord right now, but it would be used for axiom, betterstack, etcADDITIONAL_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
2 years ago
quick question for you, have you heard of axiom or betterstack?
2 years ago
they are both logging platforms where you can sends logs to and then set up observability on, for stuff like error levels
2 years ago
arguably better suited for this task than discord haha
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}`,
},
});
}
},2 years ago
yeah i figured, but i thought locomotive would be a ready to use solution for you
2 years ago
i dont know how closely you looked into locomotive's code but it uses the environment logs
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;
}
}2 years ago
yeah you need to use the internal api endpoint
2 years ago
2 years ago
where you able to give locomotive a try?
2 years 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

tysm for the help tho, do u want any specific feedback from using ur template?
2 years 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
2 years ago
thanks!
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
I did not get to test did but ont sure if the locomotive template can have conditonal fitlers AND | OR as i had multiple deployments on the same railway project and had to target only 1
2 years ago
okay makes sense, I'm glad it still did work though!
2 years ago
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
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
2 years ago
the best help I could give you would be to tell you to look at locomotive's subscription code

