npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lark-apaas/observable

v1.0.4

Published

Observable SDK

Readme

@lark-apaas/observable

@lark-apaas/observable 是一个面向服务端的链路追踪与日志观测 SDK。它基于 OpenTelemetry 构建,为 Monorepo 中的 Node.js 应用提供统一的、具备上下文感知的可观测性能力。


1. Project Context (项目背景与定位)

  • Architectural Role: Core Infrastructure - 位于系统底层,作为服务端可观测性的基础库,屏蔽了 OpenTelemetry 的复杂配置。
  • Key Consumers (谁在用我):
    • 📦 @lark-apaas/nestjs-observable: NestJS 框架集成层,将其封装为 NestJS 模块和拦截器。
  • Core Dependencies (我依赖谁):
    • 🛠️ OpenTelemetry APIs & SDKs: 提供核心的 Trace 和 Logs 处理能力。
    • 🔗 rxjs: 用于处理流式数据的生命周期管理(如 Span 的自动结束)。

2. Real-world Scenarios (基于真实挖掘)

Scenario A: The Standard Usage (Middleware 自动开启上下文)

这是在 @lark-apaas/nestjs-observable 中最标准的用法,用于在请求进入时自动开启根 Span,并从 Header 中透传 Trace 信息。

import { appSdk } from '@lark-apaas/observable';

// 在中间件或拦截器中调用
appSdk.startContext(req.headers, 'GET /api/user', (span) => {
  // 此时上下文已建立,后续的 log 或 trace 会自动关联
  res.on('finish', () => {
    span.setAttributes({ 'http_status_code': res.statusCode });
    span.end();
  });
  next();
});

Scenario B: Integration Example (业务代码手动打点)

展示如何在业务 Service 中进行手动链路追踪和日志记录,SDK 会自动从当前上下文中提取 Trace ID。

import { appSdk } from '@lark-apaas/observable';

class UserService {
  async getUser(id: string) {
    // 自动关联到中间件开启的 Root Span 下
    return appSdk.trace('UserService.getUser', async (span) => {
      span.setAttribute('user.id', id);
      
      const user = await this.db.find(id);
      
      // 日志会自动带上当前 Trace 的上下文信息
      appSdk.log('info', 'Fetch user success', { userId: id });
      
      return user;
    });
  }
}

3. API Reference

Core API (appSdk)

| 函数名 | 说明 | 参数类型 | | :--- | :--- | :--- | | start() | 初始化并启动 OTel SDK。 | void | | startContext() | [框架专用] 根据 Header 开启一个活动的根上下文。 | (headers, name, callback) | | trace<T>() | [用户专用] 手动创建一个子 Span,支持同步、Async 和 Observable。 | (name, callback) | | log() | 记录结构化日志,自动关联当前 Trace 上下文。 | (level, message, extra?) | | flush() | 强制刷新并导出内存中积压的 Trace 和 Logs 数据。 | Promise<void> |

Configuration

SDK 默认采用以下配置:

  • Batch Processing: 日志和 Trace 均采用批处理(BatchProcessor),延迟 2000ms 或达到批次上限时导出。
  • Resource Detection: 内置 PlatformDetector,自动识别并添加平台相关的资源属性。
  • Log Format: 自动将所有日志属性转换为字符串,确保在 ElasticSearch 等系统中索引一致性。