Skip to content

Support for util.inspect and util.inspect.custom #1257

@nabetti1720

Description

@nabetti1720

From what I've observed, some packages on @opentelemetry are built on the premise of util.inspect.

  • @opentelemetry/resources
  • @opentelemetry/sdk-metrics
  • @opentelemetry/sdk-trace-base
  • @opentelemetry/resource-detector-aws
  • @opentelemetry/resource-detector-container

To avoid any misunderstanding, I believe that the components necessary for implementation already exist in llrt_logging, and simply implementing the following in the util module will work.

However, it completely ignores the options, so the output is not what you expect.

fn inspect<'js>(ctx: Ctx<'js>, args: Rest<Value<'js>>) -> Result<String> {
    let mut result = String::new();
    let mut options = llrt_logging::FormatOptions::new(&ctx, false, true)?;
    llrt_logging::build_formatted_string(&mut result, &ctx, args, &mut options)?;
    result.push(NEWLINE);
    Ok(result)
}

impl ModuleDef for UtilModule {
    // ...
    fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
        export_default(ctx, exports, |default| {
            let globals = ctx.globals();
            // ...
            let inspect_fn = Function::new(ctx.clone(), inspect)?.with_name("inspect")?;
            inspect_fn.prop("custom", primordials.symbol_custom_inspect.clone())?;
            default.set("inspect", inspect_fn)?;
            Ok(())
        })
    }
}

I find it difficult to support the util.inspect options without breaking the existing llrt_logging implementation, so I hope that someone knowledgeable will take on the challenge of implementing it. :)

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