@softistx/telemetry-hono 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js.map CHANGED
@@ -6,7 +6,7 @@
6
6
  "import {\n\tcontinuing,\n\tcreateTelemetry,\n\ttype SpanScope,\n\ttype Telemetry,\n\ttype TelemetryOptions,\n\twithTelemetry,\n} from '@softistx/telemetry';\nimport type { Context, MiddlewareHandler } from 'hono';\nimport { matchedRoutes } from 'hono/route';\nimport {\n\tHTTP_ROUTE,\n\tHTTP_STATUS,\n\trequestAttributes,\n\tserverFailed,\n} from '../attributes/http';\nimport { SPAN_VARIABLE, TELEMETRY_VARIABLE } from './variables';\n\n/** The hooks both shapes of the middleware share. */\ninterface Hooks {\n\t/**\n\t * Whether this request gets a span at all. Default: every request does.\n\t *\n\t * This is the health-check hook. Nothing is skipped by default, because a\n\t * library that decides for you which requests do not matter is a library\n\t * that hides the one that did.\n\t */\n\treadonly traced?: (c: Context) => boolean;\n\t/**\n\t * The span's name **before** routing. Default `\"<METHOD> <path>\"`, which is\n\t * replaced by `\"<METHOD> <route>\"` once the router has matched.\n\t */\n\treadonly spanName?: (c: Context) => string;\n\t/**\n\t * The route template, once the handler has run. Default `routePath(c, -1)`.\n\t * Return `undefined` to leave the span named as it was.\n\t */\n\treadonly route?: (c: Context) => string | undefined;\n}\n\n/**\n * Either build a telemetry from `service` and the usual options, or hand one\n * over as `instance`. The compiler refuses both at once: `service` would be\n * silently ignored beside an instance that already has one.\n */\nexport type TelemetryMiddlewareOptions =\n\t| (Hooks &\n\t\t\tTelemetryOptions & {\n\t\t\t\t/** The service name. Everything groups by it. */\n\t\t\t\treadonly service: string;\n\t\t\t\treadonly instance?: undefined;\n\t\t\t})\n\t| (Hooks & {\n\t\t\t/** An existing telemetry. It is **adopted, not closed**. */\n\t\t\treadonly instance: Telemetry;\n\t\t\treadonly service?: undefined;\n\t });\n\n/** The middleware, with the telemetry it is writing to. */\nexport interface TelemetryMiddleware extends MiddlewareHandler {\n\t/**\n\t * The telemetry this middleware uses — the one it built, or the one it was\n\t * handed. `close()` it on shutdown; nothing here does that for you, because\n\t * a middleware has no shutdown to hook.\n\t */\n\treadonly telemetry: Telemetry;\n}\n\n/**\n * One server span per request, around the whole handler.\n *\n * ```ts\n * const tracing = telemetry({\n * service: 'checkout',\n * exporters: [otlpExporter({ endpoint: 'http://localhost:4318' })],\n * traced: (c) => c.req.path !== '/health',\n * });\n *\n * app.use('*', tracing);\n * process.on('SIGTERM', () => void tracing.telemetry.close());\n * ```\n *\n * The span **wraps `next()`** rather than being two hooks. That is what puts\n * the whole handler — and everything it awaits, and everything it throws —\n * inside the span's context, so `log.info()` in a service three calls down\n * carries this request's `traceId` without anything being passed to it.\n *\n * An inbound `traceparent` continues its trace. An unusable one is not an\n * error: it starts a fresh trace, because the header came from a stranger.\n */\nexport function telemetry(\n\toptions: TelemetryMiddlewareOptions,\n): TelemetryMiddleware {\n\tconst instance =\n\t\toptions.instance ?? createTelemetry(options.service, options).install();\n\n\t// Every hook is application code, and every hook is on the path that decides\n\t// whether a request is observed. One that throws must cost its answer, not\n\t// the request: a predicate that raises would otherwise turn observability\n\t// into an outage.\n\tconst traced = guarded(options.traced ?? always, always);\n\tconst spanName = guarded(options.spanName ?? defaultName, defaultName);\n\tconst route = guarded(options.route ?? defaultRoute, nothing);\n\n\tconst handler: MiddlewareHandler = async (c, next) => {\n\t\tif (!traced(c)) return next();\n\n\t\treturn withTelemetry(instance, () =>\n\t\t\tcontinuing(\n\t\t\t\tc.req.header('traceparent'),\n\t\t\t\tspanName(c),\n\t\t\t\t{ kind: 'server', attributes: requestAttributes(c.req.raw) },\n\t\t\t\tasync (scope) => {\n\t\t\t\t\tc.set(TELEMETRY_VARIABLE, instance);\n\t\t\t\t\tc.set(SPAN_VARIABLE, scope);\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait next();\n\t\t\t\t\t} finally {\n\t\t\t\t\t\t// Even a handler that threw was routed, and the route is\n\t\t\t\t\t\t// what the span should be called either way.\n\t\t\t\t\t\trename(c, scope, route);\n\t\t\t\t\t}\n\n\t\t\t\t\trecord(c, scope);\n\t\t\t\t},\n\t\t\t),\n\t\t);\n\t};\n\n\treturn Object.assign(handler, { telemetry: instance });\n}\n\n/**\n * The route template, which is only known **after** the router has matched.\n *\n * It is the last *handler* among the matched routes, not simply the last match:\n * `routePath(c)` answers whichever route ran last, which is a middleware\n * registered after the routes, and `routePath(c, -1)` — besides taking a second\n * argument only since hono 4.10 — answers the last entry whatever it is. Hono's\n * own `matchedRoutes` example tells a handler from a middleware by its arity,\n * because a middleware takes `(c, next)` and a handler takes `(c)`.\n *\n * Nothing matched means there is no route to report: a 404, or a request that\n * only ever reached `app.use('*', …)`.\n */\nfunction defaultRoute(c: Context): string | undefined {\n\tconst handled = matchedRoutes(c).filter(\n\t\t(matched) =>\n\t\t\t(matched.handler as (...args: unknown[]) => unknown).length < 2,\n\t);\n\n\treturn handled.at(-1)?.path;\n}\n\nfunction defaultName(c: Context): string {\n\treturn `${c.req.method} ${c.req.path}`;\n}\n\n/** A hook, and what to ask instead when it throws. */\nfunction guarded<T>(\n\thook: (c: Context) => T,\n\tfallback: (c: Context) => T,\n): (c: Context) => T {\n\treturn (c) => {\n\t\ttry {\n\t\t\treturn hook(c);\n\t\t} catch {\n\t\t\treturn fallback(c);\n\t\t}\n\t};\n}\n\nfunction always(): true {\n\treturn true;\n}\n\nfunction nothing(): undefined {\n\treturn undefined;\n}\n\n/**\n * A span named for the path it arrived at gives a dashboard one row per order\n * id. The template is the name; the path stays as `url.path`.\n */\nfunction rename(\n\tc: Context,\n\tscope: SpanScope,\n\troute: (c: Context) => string | undefined,\n): void {\n\tconst template = route(c);\n\n\t// There is no wildcard to filter out here: the template comes from a\n\t// handler, and a handler's registered path is a route the application\n\t// chose — `app.get('/files/*')` included. The patterns worth dropping are\n\t// `app.use`'s, and those never reach this.\n\tif (template === undefined || template === '') return;\n\n\tscope.name = `${c.req.method} ${template}`;\n\tscope.attribute(HTTP_ROUTE, template);\n}\n\n/**\n * The status, the failure, and which of them decides how the span reads.\n *\n * **Hono catches.** A handler that throws does not reject `next()`: the router\n * turns the exception into a reply and leaves it on `c.error`, so the only\n * thing that knows what went wrong is that field. Reading it is what puts\n * `exception.type` on the span; without it the span says \"error\" and nothing\n * about why.\n *\n * **The reply decides the status, not the exception.** `HTTPException` is how\n * a Hono application says `401` — it is what `basicAuth`, `bearerAuth`, `jwt`\n * and the validators all throw — so letting a thrown failure mark the span\n * would put every rejected login in the error rate. A `4xx` is the server\n * working. Only a `5xx` is the server's fault, and only then does an `ok` span\n * become an `error` one.\n *\n * A status the scope already decided is left alone: an abort answered with a\n * `500` stays `cancelled`, which is the whole reason that status exists.\n */\nfunction record(c: Context, scope: SpanScope): void {\n\tconst status = statusOf(c);\n\n\tif (c.error !== undefined) {\n\t\tconst before = scope.status;\n\t\t// Records `exception.*`. It also sets a status, which is right when the\n\t\t// exception is the reply and wrong when the reply is a 4xx.\n\t\tscope.fail(c.error);\n\t\tif (status !== undefined && !serverFailed(status)) scope.status = before;\n\t}\n\n\tif (status === undefined) return;\n\n\tscope.attribute(HTTP_STATUS, status);\n\tif (serverFailed(status) && scope.status === 'ok') scope.status = 'error';\n}\n\nfunction statusOf(c: Context): number | undefined {\n\ttry {\n\t\treturn c.res.status;\n\t} catch {\n\t\t// A response this runtime will not build is not worth losing the span\n\t\t// over.\n\t\treturn undefined;\n\t}\n}\n",
7
7
  "import type { SpanScope, Telemetry } from '@softistx/telemetry';\n\n/**\n * What the middleware puts on the request context.\n *\n * Augmenting `ContextVariableMap` rather than asking for a typed `Env` is what\n * makes `c.get('span')` typed in a handler that knows nothing about this\n * package — which is the whole point of putting it there.\n *\n * It is declared here and re-exported from the entry point, so importing\n * anything from `@softistx/telemetry-hono` is what turns it on.\n */\ndeclare module 'hono' {\n\tinterface ContextVariableMap {\n\t\t/**\n\t\t * The telemetry this request is being written to.\n\t\t *\n\t\t * **Optional**, and the compiler enforces it: a request `traced` refused\n\t\t * never had a middleware set it, so `c.get('telemetry')` is `undefined`\n\t\t * there. Type safety is what the compiler rejects, not what a README\n\t\t * claims.\n\t\t */\n\t\ttelemetry?: Telemetry;\n\t\t/** The server span around this request. `undefined` when `traced` said no. */\n\t\tspan?: SpanScope;\n\t}\n}\n\n/** The keys, as values, for code that sets them by name. */\nexport const TELEMETRY_VARIABLE = 'telemetry';\nexport const SPAN_VARIABLE = 'span';\n"
8
8
  ],
9
- "mappings": ";AAWO,IAAM,cAAc;AACpB,IAAM,WAAW;AACjB,IAAM,aAAa;AACnB,IAAM,aAAa;AACnB,IAAM,cAAc;AACpB,IAAM,iBAAiB;AACvB,IAAM,cAAc;AAGpB,IAAM,oBAAoB;AAU1B,SAAS,iBAAiB,CAAC,SAA8B;AAAA,EAC/D,MAAM,MAAM,QAAQ,QAAQ,GAAG;AAAA,EAE/B,OAAO;AAAA,KACL,cAAc,QAAQ;AAAA,OACnB,QAAQ,YACT,CAAC,IACD;AAAA,OACC,WAAW,IAAI;AAAA,OACf,aAAa,IAAI,SAAS,QAAQ,KAAK,EAAE;AAAA,OAKzC,iBAAiB,IAAI;AAAA,SAClB,IAAI,SAAS,KAAK,CAAC,IAAI,GAAG,cAAc,OAAO,IAAI,IAAI,EAAE;AAAA,IAC9D;AAAA,EACH;AAAA;AAUM,SAAS,YAAY,CAAC,QAAyB;AAAA,EACrD,OAAO,UAAU;AAAA;AAGlB,SAAS,OAAO,CAAC,KAA8B;AAAA,EAC9C,IAAI;AAAA,IACH,OAAO,IAAI,IAAI,GAAG;AAAA,IACjB,MAAM;AAAA,IAEP;AAAA;AAAA;;AClEF;AAAA;AAAA;AAAA;AAAA;AASA;;;ACoBO,IAAM,qBAAqB;AAC3B,IAAM,gBAAgB;;;AD4DtB,SAAS,SAAS,CACxB,SACsB;AAAA,EACtB,MAAM,WACL,QAAQ,YAAY,gBAAgB,QAAQ,SAAS,OAAO,EAAE,QAAQ;AAAA,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ACZ,MAAM;AAAA,IAGP;AAAA;AAAA;",
10
10
  "debugId": "17434F8203A338E464756E2164756E21",
11
11
  "names": []
12
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softistx/telemetry-hono",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "A Hono middleware for @softistx/telemetry: one server span per request, W3C traceparent continuation, and the route as the span name",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  ],
33
33
  "repository": {
34
34
  "type": "git",
35
- "url": "git+https://github.com/softistx/nxgt-telemetry.git",
35
+ "url": "git+https://github.com/softistx/telemetry.git",
36
36
  "directory": "packages/telemetry-hono"
37
37
  },
38
38
  "publishConfig": {
@@ -50,10 +50,10 @@
50
50
  ]
51
51
  },
52
52
  "dependencies": {
53
- "@softistx/telemetry": "^0.4.2"
53
+ "@softistx/telemetry": "^0.5.1"
54
54
  },
55
55
  "devDependencies": {
56
- "@types/bun": "^1.4.2",
56
+ "@types/bun": "^1.4.5",
57
57
  "hono": "4.13.8"
58
58
  },
59
59
  "peerDependencies": {