Skip to content

Transport crash when specifying alternate timestamp format #171

@clemarc

Description

@clemarc

Great work on this lib @Vunovati !

Using it on my project and found a bug related to timestamp format.

How to reproduce

I just tweaked the minimalist example to specify an alternative timestamp format which gives an isoTime (2024-05-09T11:12:49.897Z)

"use strict";

const path = require("path");
const pino = require("pino");

const logger = pino({
  transport: {
    target: path.join(
      __dirname,
      "..",
      "..",
      "lib",
      "pino-opentelemetry-transport"
    ),
  },
  timestamp: pino.stdTimeFunctions.isoTime,
});

logger.info("test log");

Error

node:events:497
      throw er; // Unhandled 'error' event
      ^

Error: the worker has exited

Root cause

The root cause comes from the fact that OpenTelemetry doesn't like a string timestamp

Locally I patched the library to check if we have a string, in which case I convert to a Date in the mapper but that's still a bit wonky. Technically the timestamp option can even be a function that could return a format not recognised by new Date(time) so it might need to be more defensive and documented.

In case it doesn't parse it could drop the timestamp to let the SDK generate a new one?

Happy to get some thoughts and send a MR to improve this.

function toOpenTelemetry(..) {
  ...
  return {
    timestamp: typeof time === "string" ? new Date(time) : time,
    body: msg,
    severityNumber,
    attributes,
    severityText,
  };

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions