JaniAnttonen / winston-loki

Grafana Loki transport for the nodejs logging library Winston.
MIT License
141 stars 53 forks source link

Winston-loki not working with Grafana Cloud Loki #108

Open danmdinu opened 2 years ago

danmdinu commented 2 years ago

If you're running Grafana & Loki in the Cloud, Winston fails to send logs to the right URL.

Cloud Logs uses authentication via an API key which is set in the URL It looks like this: https://12345:@logs-prod-eu-west-0.grafana.net/loki/api/v1/push

The API Key gets stripped off because winston-loki uses just the hostname and path to form the URL. so the URL becomes: https://logs-prod-eu-west-0.grafana.net/loki/api/v1/push

This happens here: https://github.com/JaniAnttonen/winston-loki/blob/6c7b3a1f2a22f882785e4915a4356cfcd2fef4c6/src/requests.js#L19

Outcome The correct link should be used.

MatthewPattell commented 2 years ago

@danmdinu try this:

logger.add(new LokiTransport({
  host: 'https://logs-prod-eu-west-0.grafana.net',
  json: true,
  basicAuth: '123456:.....',
  labels: { job: 'winston-loki-example' },
  replaceTimestamp: true,
}))

I tested it for Grafana & Loki in the Cloud.

terchris commented 1 year ago

I had some problems getting this working. I think that my problem was that I had my parameters wrong (URL and probably password). There are probably return codes that can be inspected to help in case it does not work. I was looking for a simple code to test- I could not find one so I created it.


/* The siplest test ever to see if grafana cloud and winston is working 
A small node app that sends a log message to Grafana Cloud Loki using the winston-loki transport.
To you by terchris and friends.
*/
import winston from "winston";
import LokiTransport from "winston-loki"

// https://grafana.com/orgs/urbalurba (change urbalurba to your own org)
// in the Loki card. Click on the Details button. On the next page you will se the Grafana Data Source settings.
// Create a password  and replace the const variables below with your own values. 
const GRAFANA_HOST = "https://logs-prod-eu-west-0.grafana.net";
const MY_APP_NAME = "urbalurba";

const GRAFANA_USERID = "333666";
const GRAFANA_PASSWORD = "eyJr1234567890FjYzZlOTg2NTE5ZDIyY12345678900ODdlZjAx1234567890IsIm4iOiJ1234567890iwi1234567890cyM30=";

// start leave this as is
const GRAFANA_BASICAUTH = GRAFANA_USERID + ":" + GRAFANA_PASSWORD;
const logger = winston.createLogger({

    level: 'debug',

    transports: [
      new LokiTransport({
        host: GRAFANA_HOST,
          labels: { app: MY_APP_NAME },
          json: true,
          basicAuth: GRAFANA_BASICAUTH,
          format: winston.format.json(),
          replaceTimestamp: true,
          onConnectionError: (err) => console.error(err)
        }),
      new winston.transports.Console({}),      
    ],
  });
// end leave this as is  

// send some log messages
logger.info("Starting test");
logger.debug("sending debug message");
logger.warn("sending warn message");
logger.error("sending error message");
logger.info("done testing");

// To see the logging in grafana cloud, go to Explore https://urbalurba.grafana.net/explore (change urbalurba to your own org)
// At the top there is a dropdown. Select the one with the Loki logo (in my case grafanacloud-urbalurba-logs) 
// In the "Select label" dropdown select "app" and in the "Select value" dropdown select "urbalurba" (change urbalurba to your own app name)
// Click on the "Run Query" button in the upper right corner. You should see the log messages you sent above.
lesimoes commented 1 year ago

I really appreciate your snippet. In my case my host was wrong. Thank you

moxious commented 3 days ago

Hey hey hey! Folks in this thread are doing a work-around: (passing basicAuth). @MatthewPattell put his finger exactly on what the problem was. The PR linked above fixes, and makes it so people can do it both ways. If you adopted the work-around on this issue, it'll keep working. If you want to put the basic auth information in the URL, that'll work too if the PR is merged. Hope this helps.