GraphQL Severity Filter
Anonymous
TRIALOP

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

Anonymous
TRIALOP

2 years ago

N/A


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?


Anonymous
TRIALOP

2 years 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: ""
    filter: "@level:error"
  ) {
    message
    severity
  }
}

Anonymous
TRIALOP

2 years ago

The filtering level syntax i got from this part of the doc https://docs.railway.app/guides/logs

1239939085897044000


Anonymous
TRIALOP

2 years ago

This is also checked against Observerability’s network tab in my railway’s dashboard which confirms that the syntax im using is correct


Anonymous
TRIALOP

2 years ago

1239939134257102800


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?


Anonymous
TRIALOP

2 years ago

Im trying to make my own notification system to link with discord / telegram


2 years ago

like this?

1239939745262600200


Anonymous
TRIALOP

2 years ago

Something like that i suppose


Anonymous
TRIALOP

2 years ago

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 }


Anonymous
TRIALOP

2 years ago

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?


Anonymous
TRIALOP

2 years ago

no sorry i dont


Anonymous
TRIALOP

2 years ago

What are they?


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


Anonymous
TRIALOP

2 years ago

Sure thing, would love to give it a try 🙏


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


Anonymous
TRIALOP

2 years 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


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


Anonymous
TRIALOP

2 years ago

Oh ok sure thing


Anonymous
TRIALOP

2 years ago

sounds good


2 years ago

perhaps i should go into more details about the template in question


Anonymous
TRIALOP

2 years ago

Hahah that would be nice


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.


Anonymous
TRIALOP

2 years ago

Yep that sounds pretty close to wat im trying to do tbh


2 years ago

sample screenshot of deploying the template and how you fill out the needed info

1239944285097296000


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


Anonymous
TRIALOP

2 years ago

This looks very well made


Anonymous
TRIALOP

2 years ago


2 years ago

yep


Anonymous
TRIALOP

2 years ago

btw i saw this in the observability network tab too, whats the difference environmentLogs and deploymentLogs?


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

maybe theres a bug in the deploymentLogs for filtering then?


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!


Anonymous
TRIALOP

2 years ago

Yessir sure thing thanks alot for the help. I will check it out at work tmr 🙏


Anonymous
TRIALOP

2 years ago

I see u have a cron job repo to hahha


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

I see, seems like a useful repo for me too 😂


2 years ago

both locomotive and my cron service come as templates, you wouldnt need to use the repo directly


Anonymous
TRIALOP

2 years ago

this one right? for the locomotive

1239948020070486000


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?


Anonymous
TRIALOP

2 years ago

Oh yes please


2 years ago

i assume you know where to get your discord webhook?


Anonymous
TRIALOP

2 years ago

yep


2 years ago

okay go ahead and put that in a note


Anonymous
TRIALOP

2 years ago

ok got it


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


Anonymous
TRIALOP

2 years ago

Ok found both


2 years ago

hence why i had no problem showing you them here


Anonymous
TRIALOP

2 years ago

ah i see, thts good to know


2 years 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


2 years ago

quick question for you, have you heard of axiom or betterstack?


Anonymous
TRIALOP

2 years ago

tbh no for both


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


Anonymous
TRIALOP

2 years 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}`,
        },
      });
    }
  },

Anonymous
TRIALOP

2 years ago

this actually solved my problem allowing my code to run the query


2 years ago

yeah i figured, but i thought locomotive would be a ready to use solution for you


Anonymous
TRIALOP

2 years ago

actually i was searching for environmentLog


Anonymous
TRIALOP

2 years ago

btu found that


Anonymous
TRIALOP

2 years ago

hahaha


2 years ago

i dont know how closely you looked into locomotive's code but it uses the environment logs


Anonymous
TRIALOP

2 years 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;
  }
}

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?


Anonymous
TRIALOP

2 years ago

Will try this out fully tmr with the team and let u know ✌️


Anonymous
TRIALOP

2 years ago

btw where can i get the environmentId from railway?


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

1239956912997662700


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

Ok sure thing, will get u the feedback when i work on it


2 years ago

thanks!


Anonymous
TRIALOP

2 years 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


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

e.g. cron job automated testing services


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

@level:error AND @service:${serviceId} smth like that


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


Anonymous
TRIALOP

2 years ago

Hahah thats actually on my ticket for tmr, but now tht uve mentioned it, do u mind pointing me in teh right direction


Anonymous
TRIALOP

2 years ago

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


Anonymous
TRIALOP

2 years ago

Ok sure thing ill dig into that


Loading...