fluent / fluent-logger-forward-node

A fluent forward protocol implementation for Node.js
https://fluent.github.io/fluent-logger-forward-node/
Apache License 2.0
11 stars 8 forks source link

Configure Connection Error Propagation #43

Closed Travis-Taylor closed 1 year ago

Travis-Taylor commented 1 year ago

Hi, I've just started migrating an app from fluent-logger to this tool, and I'm trying to understand how to configure propagation of connection errors to my app. For context, we have a fluent-bit server with a configurable port setting, so we need to make sure the connection is reliably established (to avoid configuration problems). I have something like this in the app:

class FluentLogger {
  logger: any;
  fluentHost: string;
  fluentPort: number;
  constructor() {
    const FluentClient = require("@fluent-org/logger").FluentClient;
    this.fluentHost = process.env.FLUENTD_ENDPOINT ?? "localhost";
    this.fluentPort = parseInt(process.env.FLUENTD_PORT) || 24224;
    this.logger = new FluentClient("teleop-frontend", {
      socket: {
        host: this.fluentHost,
        port: this.fluentPort,
      },
    });
  }

  emit(event: object, label?: string) {
    if (! this.logger.writable) {
      console.log(
      `Unable to connect to Fluent logger client at ${this.fluentHost}:${this.fluentPort}!`
      );
      return;
    }
    if (label) this.logger.emit(label, event);
    else this.logger.emit(event);
  }
}

This check of FluentClient.writable is the only thing I found in this package which provides some feedback to the status of the connection. I see that there is an internal FluentSocket.onError that serves to emit e.g. connection errors (via FluentSocket.openSocket()) but I can't find any configuration options to actually emit that event to anything. All the other functions I call return void Promises that don't raise on failure; is there some configuration option/step I'm missing?

EDIT: I actually am getting false in all cases when checking FluentClient.writable, even when the host and port are correct (i.e. removing that if (!this.logger.writable) block successfully publishes the event). So I'm not sure what the right way to do this is

I'm using @fluent-org/logger 1.0.10 in a NextJS app, for reference

jamiees2 commented 1 year ago

Hey! it sounds like you are looking for the disableAutoconnect option.

You can create your client with

    this.logger = new FluentClient("teleop-frontend", {
      socket: {
        host: this.fluentHost,
        port: this.fluentPort,
      },
      disableAutoconnect: true
    });

Then, you can call this.logger.connect(), which is a Promise that will reject if the connection fails. You can also listen to socket error events using this.logger.socketOn("error", err => { console.error("fluent socket error", err)})

Travis-Taylor commented 1 year ago

I see - that's very helpful! I had seen that disableAutoconnect option mentioned in your docs but didn't take advantage of the connect() Promise. And the socketOn callback is very useful! That might be worth highlighting in the docs.

Thanks for your quick assistance!