@grow-codes/grafana-loki 0.1.0 → 0.2.0

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/lib/index.js CHANGED
@@ -1,10 +1,12 @@
1
- import { ConfigError, LokiError, ErrorCode, sleep, CancelledError, TimeoutError, errorForStatus, UnavailableError, logRecordByteSize, toAttributes, severityText, normalizeHexId, parseSeverity, ValidationError, BaseLogQueryClient, UnsupportedError } from './chunk-KTHP35QG.js';
2
- export { AuthenticationError, BaseLogQueryClient, CancelledError, ConfigError, ConflictError, ErrorCode, LokiError, NotFoundError, PermissionDeniedError, RateLimitError, Severity, TimeoutError, UnavailableError, UnsupportedError, UnsupportedLogQueryClient, ValidationError, canonicalSeverity, codeForStatus, errorForCode, errorForStatus, isLokiError, logRecordByteSize, logRecordFromJson, logRecordToJson, parseLogRecord, parseSeverity, pollTail, pollUntil, reasonForCode, serializeLogRecord, severityText, sleep, throwIfAborted, toAttrValue, toAttributes, waitForLog } from './chunk-KTHP35QG.js';
1
+ import { ConfigError, LokiError, ErrorCode, sleep, CancelledError, TimeoutError, errorForStatus, UnavailableError, logRecordByteSize, toAttributes, severityText, normalizeHexId, parseSeverity, ValidationError, BaseLogQueryClient, UnsupportedError } from './chunk-ZH5T5C4R.js';
2
+ export { AuthenticationError, BaseLogQueryClient, CancelledError, ConfigError, ConflictError, ErrorCode, LokiError, NotFoundError, PermissionDeniedError, RateLimitError, Severity, TimeoutError, UnavailableError, UnsupportedError, UnsupportedLogQueryClient, ValidationError, canonicalSeverity, codeForStatus, errorForCode, errorForStatus, isLokiError, logRecordByteSize, logRecordFromJson, logRecordToJson, parseLogRecord, parseSeverity, pollTail, pollUntil, reasonForCode, serializeLogRecord, severityText, sleep, throwIfAborted, toAttrValue, toAttributes, waitForLog } from './chunk-ZH5T5C4R.js';
3
3
  import { readFileSync } from 'fs';
4
4
  import { ProxyAgent, Agent, fetch } from 'undici';
5
5
  import { gzipSync } from 'zlib';
6
6
  import { trace, context, isSpanContextValid } from '@opentelemetry/api';
7
7
  import { Writable } from 'stream';
8
+ import { AsyncLocalStorage } from 'async_hooks';
9
+ import { randomUUID } from 'crypto';
8
10
  import { compress } from 'snappyjs';
9
11
  import WebSocket from 'ws';
10
12
 
@@ -101,7 +103,7 @@ function redactUrl(url) {
101
103
  }
102
104
  function redactQueryString(text) {
103
105
  return text.replace(
104
- /([?&])([^=&#\s]+)=([^&#\s]*)/g,
106
+ /([?&])([^=&#?\s]+)=([^&#\s]*)/g,
105
107
  (m, sep, k) => isSensitiveField(k) ? `${sep}${k}=${REDACTED}` : m
106
108
  );
107
109
  }
@@ -206,20 +208,33 @@ var UNIT_MS = {
206
208
  m: 6e4,
207
209
  h: 36e5
208
210
  };
209
- var DURATION_PART = /(\d+(?:\.\d+)?|\.\d+)(ns|us|µs|ms|s|m|h)/gy;
211
+ var DURATION_UNITS = ["ns", "us", "\xB5s", "ms", "s", "m", "h"];
210
212
  function parseDuration(raw) {
211
213
  const text = raw.trim();
212
214
  if (/^\d+$/.test(text)) return Number(text);
213
215
  if (text === "") return void 0;
214
- DURATION_PART.lastIndex = 0;
215
216
  let total = 0;
216
- let pos = 0;
217
- let match;
218
- while ((match = DURATION_PART.exec(text)) !== null) {
219
- total += Number(match[1]) * UNIT_MS[match[2]];
220
- pos = DURATION_PART.lastIndex;
217
+ let i = 0;
218
+ const isDigit = (at) => at < text.length && text.charCodeAt(at) >= 48 && text.charCodeAt(at) <= 57;
219
+ while (i < text.length) {
220
+ const start = i;
221
+ while (isDigit(i)) i += 1;
222
+ const intDigits = i - start;
223
+ if (text[i] === ".") {
224
+ i += 1;
225
+ const fracStart = i;
226
+ while (isDigit(i)) i += 1;
227
+ if (i === fracStart) return void 0;
228
+ } else if (intDigits === 0) {
229
+ return void 0;
230
+ }
231
+ const value = Number(text.slice(start, i));
232
+ const unit = DURATION_UNITS.find((u) => text.startsWith(u, i));
233
+ if (unit === void 0) return void 0;
234
+ total += value * UNIT_MS[unit];
235
+ i += unit.length;
221
236
  }
222
- return pos === text.length && pos > 0 ? Math.round(total) : void 0;
237
+ return Math.round(total);
223
238
  }
224
239
  var TRUE_WORDS = /* @__PURE__ */ new Set(["true", "1", "yes", "on"]);
225
240
  var FALSE_WORDS = /* @__PURE__ */ new Set(["false", "0", "no", "off"]);
@@ -590,10 +605,10 @@ function secretValues(...values) {
590
605
  }
591
606
 
592
607
  // src/core/version.ts
593
- var SDK_VERSION = "0.1.0";
608
+ var SDK_VERSION = "0.2.0";
594
609
  var PACKAGE_KEY = "grafana-loki";
595
610
  var LANGUAGE = "typescript";
596
- var SDK_KIT_VERSION = "0.2.0";
611
+ var SDK_KIT_VERSION = "0.3.0";
597
612
 
598
613
  // src/core/transport.ts
599
614
  var DEFAULT_REQUEST_ID_HEADERS = [
@@ -694,6 +709,14 @@ var HttpTransport = class {
694
709
  get closed() {
695
710
  return this.#closed;
696
711
  }
712
+ /**
713
+ * Throws `CancelledError` (`"<operation>: client is closed"`, not retryable) once {@link close}
714
+ * was called (SPEC §2.4). Requests check this themselves; service methods that send no request
715
+ * (cached reads, local helpers) call it first.
716
+ */
717
+ ensureOpen(operation) {
718
+ if (this.#closed) throw new CancelledError(`${operation}: client is closed`);
719
+ }
697
720
  /** Builds the absolute URL for a path and query. */
698
721
  buildUrl(path, query) {
699
722
  let url;
@@ -756,7 +779,7 @@ var HttpTransport = class {
756
779
  async #request(options) {
757
780
  const method = (options.method ?? "GET").toUpperCase();
758
781
  const operation = options.operation ?? `${method} ${options.path.split("?")[0] ?? ""}`;
759
- if (this.#closed) throw new CancelledError(`${operation}: client is closed`);
782
+ this.ensureOpen(operation);
760
783
  const signal = options.signal;
761
784
  if (signal?.aborted === true) throw new CancelledError(`${operation}: cancelled`);
762
785
  const url = this.buildUrl(options.path, options.query);
@@ -2016,6 +2039,225 @@ var WinstonTransport = class extends Writable {
2016
2039
  }
2017
2040
  };
2018
2041
 
2042
+ // src/logging-connect.ts
2043
+ var connectLogClient = ({
2044
+ resource,
2045
+ onDropped
2046
+ }) => {
2047
+ const client = LokiClient.fromEnv();
2048
+ const factory = client.logClient;
2049
+ if (typeof factory !== "function") {
2050
+ void client.close();
2051
+ return void 0;
2052
+ }
2053
+ const logs = factory.call(client, { resource, batch: { onDrop: onDropped } });
2054
+ return {
2055
+ logs,
2056
+ close: async () => {
2057
+ await logs.close();
2058
+ await client.close();
2059
+ }
2060
+ };
2061
+ };
2062
+
2063
+ // src/logging/init.ts
2064
+ var LOG_SHIPPED_FIELD = "log_shipped";
2065
+ var requestContext = new AsyncLocalStorage();
2066
+ var GLOBAL_KEY = /* @__PURE__ */ Symbol.for(`grow-codes.${PACKAGE_KEY}.logging`);
2067
+ function withRequestId(requestId, fn) {
2068
+ return requestContext.run({ requestId }, fn);
2069
+ }
2070
+ function currentRequestId() {
2071
+ return requestContext.getStore()?.requestId;
2072
+ }
2073
+ function getLogging() {
2074
+ return globalThis[GLOBAL_KEY];
2075
+ }
2076
+ function first(env, ...names) {
2077
+ for (const name of names) {
2078
+ const value = env[name]?.trim();
2079
+ if (value !== void 0 && value !== "") return value;
2080
+ }
2081
+ return void 0;
2082
+ }
2083
+ function stdoutLine(record, marker) {
2084
+ const line = {
2085
+ time: record.timestamp.toISOString(),
2086
+ level: severityText(record.severity).toLowerCase(),
2087
+ msg: record.body
2088
+ };
2089
+ const resource = record.resource;
2090
+ if (resource["service.name"] !== void 0) line["service"] = resource["service.name"];
2091
+ if (resource["deployment.environment"] !== void 0)
2092
+ line["environment"] = resource["deployment.environment"];
2093
+ if (record.traceId !== void 0) line["trace_id"] = record.traceId;
2094
+ if (record.spanId !== void 0) line["span_id"] = record.spanId;
2095
+ for (const [key, value] of Object.entries(record.attributes)) if (!(key in line)) line[key] = value;
2096
+ if (marker !== void 0) line[LOG_SHIPPED_FIELD] = marker;
2097
+ return JSON.stringify(line);
2098
+ }
2099
+ function initLogging(opts = {}) {
2100
+ const existing = getLogging();
2101
+ if (existing !== void 0) return existing;
2102
+ const env = opts.env ?? process.env;
2103
+ const service = opts.service ?? first(env, "GROW_SERVICE_NAME", "OTEL_SERVICE_NAME") ?? "unknown_service";
2104
+ const environment = first(env, "GROW_ENVIRONMENT", "DEPLOYMENT_ENVIRONMENT");
2105
+ const resource = { "service.name": service };
2106
+ if (environment !== void 0) resource["deployment.environment"] = environment;
2107
+ const levelText = opts.level ?? first(env, "LOG_LEVEL", "GROW_LOG_LEVEL") ?? "info";
2108
+ const minSeverity = parseSeverity(levelText) ?? 9 /* Info */;
2109
+ const write = opts.write ?? ((line) => void process.stdout.write(`${line}
2110
+ `));
2111
+ const redaction = new RedactionProcessor(opts.redaction ?? {});
2112
+ const ship = opts.ship ?? first(env, "GROW_LOGGING_PROVIDER") === PACKAGE_KEY;
2113
+ let connection;
2114
+ if (ship) {
2115
+ try {
2116
+ const onDropped = (records) => {
2117
+ for (const record of records) {
2118
+ try {
2119
+ write(stdoutLine(record, "sdk_failed"));
2120
+ } catch {
2121
+ }
2122
+ }
2123
+ };
2124
+ connection = (opts.connect ?? connectLogClient)({ resource, env, onDropped });
2125
+ } catch (err) {
2126
+ write(
2127
+ stdoutLine(
2128
+ {
2129
+ timestamp: /* @__PURE__ */ new Date(),
2130
+ severity: 13 /* Warn */,
2131
+ body: `logging: ${PACKAGE_KEY} transport not available, stdout only (${err.name})`,
2132
+ attributes: {},
2133
+ resource
2134
+ }
2135
+ )
2136
+ );
2137
+ }
2138
+ }
2139
+ const logs = connection?.logs;
2140
+ const shipped = logs !== void 0;
2141
+ const toStdout = opts.stdout ?? true;
2142
+ const sink = {
2143
+ send(input) {
2144
+ const requestId = currentRequestId();
2145
+ const attributes = toAttributes({
2146
+ ...requestId === void 0 ? {} : { request_id: requestId },
2147
+ ...input.attributes
2148
+ });
2149
+ const severity = input.severity ?? 9 /* Info */;
2150
+ const record = {
2151
+ timestamp: input.timestamp ?? /* @__PURE__ */ new Date(),
2152
+ severity,
2153
+ severityText: severityText(severity),
2154
+ body: input.message,
2155
+ attributes,
2156
+ resource
2157
+ };
2158
+ if (input.traceId !== void 0) record.traceId = input.traceId;
2159
+ if (input.spanId !== void 0) record.spanId = input.spanId;
2160
+ if (input.traceFlags !== void 0) record.traceFlags = input.traceFlags;
2161
+ if (toStdout) {
2162
+ try {
2163
+ write(stdoutLine(redaction.process(record), shipped ? "sdk" : void 0));
2164
+ } catch {
2165
+ }
2166
+ }
2167
+ logs?.send(record);
2168
+ }
2169
+ };
2170
+ let closing;
2171
+ const handle = {
2172
+ logger: new Logger(sink, { minSeverity }),
2173
+ logs,
2174
+ shipped,
2175
+ service,
2176
+ flush: (timeoutMs) => logs === void 0 ? Promise.resolve() : logs.flush(timeoutMs),
2177
+ close: () => {
2178
+ closing ??= (async () => {
2179
+ if (globalThis[GLOBAL_KEY] === handle) globalThis[GLOBAL_KEY] = void 0;
2180
+ await connection?.close();
2181
+ })();
2182
+ return closing;
2183
+ },
2184
+ canary: async (message, attributes = {}) => {
2185
+ if (logs === void 0) return false;
2186
+ logs.send({ message, severity: 9 /* Info */, attributes: { "foundation.canary": true, ...attributes } });
2187
+ await logs.flush(5e3).catch(() => void 0);
2188
+ return true;
2189
+ },
2190
+ pinoStream: () => pinoTee(write, logs, shipped, toStdout)
2191
+ };
2192
+ globalThis[GLOBAL_KEY] = handle;
2193
+ return handle;
2194
+ }
2195
+ function pinoTee(write, logs, shipped, toStdout) {
2196
+ const transport = logs === void 0 ? void 0 : createPinoTransport(logs, { flushOnEnd: false });
2197
+ return new Writable({
2198
+ write(chunk, _encoding, callback) {
2199
+ const text = chunk.toString();
2200
+ if (toStdout) {
2201
+ for (const raw of text.split("\n")) {
2202
+ if (raw.trim() === "") continue;
2203
+ let out = raw;
2204
+ if (shipped) {
2205
+ try {
2206
+ out = JSON.stringify({ ...JSON.parse(raw), [LOG_SHIPPED_FIELD]: "sdk" });
2207
+ } catch {
2208
+ }
2209
+ }
2210
+ write(out);
2211
+ }
2212
+ }
2213
+ if (transport === void 0) callback();
2214
+ else transport.write(chunk, callback);
2215
+ },
2216
+ final(callback) {
2217
+ if (transport === void 0) callback();
2218
+ else transport.end(callback);
2219
+ }
2220
+ });
2221
+ }
2222
+ function headerValue(value) {
2223
+ const v = Array.isArray(value) ? value[0] : value;
2224
+ return v === void 0 || v.trim() === "" ? void 0 : v.trim().slice(0, 128);
2225
+ }
2226
+ function requestLogger(opts = {}) {
2227
+ const header = (opts.header ?? "x-request-id").toLowerCase();
2228
+ const skip = opts.skip ?? ["/health/live", "/health/ready"];
2229
+ return (req, res, next) => {
2230
+ const requestId = headerValue(req.headers[header]) ?? randomUUID();
2231
+ res.setHeader(header, requestId);
2232
+ const path = (req.originalUrl ?? req.url ?? "/").split("?")[0] ?? "/";
2233
+ const start = performance.now();
2234
+ if (!skip.some((p) => path.startsWith(p))) {
2235
+ let done = false;
2236
+ const finish = () => {
2237
+ if (done) return;
2238
+ done = true;
2239
+ const logger = opts.logger ?? getLogging()?.logger;
2240
+ const status = res.statusCode;
2241
+ const attributes = {
2242
+ request_id: requestId,
2243
+ "http.method": req.method ?? "GET",
2244
+ "http.route": path,
2245
+ "http.status_code": status,
2246
+ duration_ms: Math.round((performance.now() - start) * 100) / 100
2247
+ };
2248
+ if (status >= 500) logger?.error("request completed", attributes);
2249
+ else if (status >= 400) logger?.warn("request completed", attributes);
2250
+ else logger?.info("request completed", attributes);
2251
+ };
2252
+ res.once("finish", finish);
2253
+ res.once("close", finish);
2254
+ }
2255
+ withRequestId(requestId, () => {
2256
+ next();
2257
+ });
2258
+ };
2259
+ }
2260
+
2019
2261
  // src/time.ts
2020
2262
  var NS_PER_MS = 1000000n;
2021
2263
  var DIGITS = /^\d+$/;
@@ -3616,6 +3858,6 @@ var LokiClient = class _LokiClient {
3616
3858
  }
3617
3859
  };
3618
3860
 
3619
- export { ALL_STREAMS_SELECTOR, BatchProcessor, BatchingLogClient, CardinalityGuard, DEFAULT_ALLOWED_LABELS, DEFAULT_BATCH_OPTIONS, DEFAULT_REDACT_KEYS, DEFAULT_REQUEST_ID_HEADERS, DEFAULT_TIMEOUTS, ENCODING_FLAGS_HEADER, EnvReader, HttpTransport, LABEL_NAME_PATTERN, LANGUAGE, LogQL, Logger, LokiClient, LokiConfig, LokiLogExporter, LokiLogQueryClient, LokiOtlpExporter, MAX_QUERY_LIMIT, MAX_TENANT_ID_LENGTH, MAX_VENDOR_TEXT, MIN_SECRET_LENGTH, OTLP_SCOPE_NAME, PACKAGE_KEY, REDACTED, RESERVED_METADATA_KEYS, RedactionProcessor, RetryPolicy, SDK_KIT_VERSION, SDK_VERSION, SENSITIVE_FIELD_PARTS, Secret, TENANT_HEADER, VENDOR_ENV, WinstonTransport, attrToString, createDispatcher, createPinoTransport, dateToNanos, decodeBuildInfo, decodeCursor, decodeEntry, decodeIndexStats, decodeQueryResult, decodeRpcStatusMessage, decodeSeries, decodeStreams, decodeStringList, decodeTailFrame, definedOnly, describeCommonConfig, encodeCursor, encodePushRequest, entryToLogRecord, formatLabelSet, healthStatus, isHealthyWord, isIdempotentMethod, isRetryableStatus, isSensitiveField, isSensitiveHeader, isValidLabelName, mergeOptions, mergeStreams, msToDuration, nanosToDate, normalizeLabelName, optionalNanos, parseBool, parseDuration, parseInteger, parseRetryAfter, pinoLevelToSeverity, pinoLineToRecord, probeHealth, quoteLogQL, readCommonEnv, recordsToOtlpJson, recordsToStreams, redactError, redactFields, redactHeaders, redactSecretsInText, redactText, redactUrl, resolveBatchOptions, resolveCommonConfig, safeCall, safeLogger, secondsToDate, secretValues, silentLogger, snappyCompress, stringMap, tenantIdProblem, toNanos, toOtlpValue, toSearchLogQL, validateCommonConfig, validateLabelName, validateLabels, winstonLevelToSeverity };
3861
+ export { ALL_STREAMS_SELECTOR, BatchProcessor, BatchingLogClient, CardinalityGuard, DEFAULT_ALLOWED_LABELS, DEFAULT_BATCH_OPTIONS, DEFAULT_REDACT_KEYS, DEFAULT_REQUEST_ID_HEADERS, DEFAULT_TIMEOUTS, ENCODING_FLAGS_HEADER, EnvReader, HttpTransport, LABEL_NAME_PATTERN, LANGUAGE, LOG_SHIPPED_FIELD, LogQL, Logger, LokiClient, LokiConfig, LokiLogExporter, LokiLogQueryClient, LokiOtlpExporter, MAX_QUERY_LIMIT, MAX_TENANT_ID_LENGTH, MAX_VENDOR_TEXT, MIN_SECRET_LENGTH, OTLP_SCOPE_NAME, PACKAGE_KEY, REDACTED, RESERVED_METADATA_KEYS, RedactionProcessor, RetryPolicy, SDK_KIT_VERSION, SDK_VERSION, SENSITIVE_FIELD_PARTS, Secret, TENANT_HEADER, VENDOR_ENV, WinstonTransport, attrToString, createDispatcher, createPinoTransport, currentRequestId, dateToNanos, decodeBuildInfo, decodeCursor, decodeEntry, decodeIndexStats, decodeQueryResult, decodeRpcStatusMessage, decodeSeries, decodeStreams, decodeStringList, decodeTailFrame, definedOnly, describeCommonConfig, encodeCursor, encodePushRequest, entryToLogRecord, formatLabelSet, getLogging, healthStatus, initLogging, isHealthyWord, isIdempotentMethod, isRetryableStatus, isSensitiveField, isSensitiveHeader, isValidLabelName, mergeOptions, mergeStreams, msToDuration, nanosToDate, normalizeLabelName, optionalNanos, parseBool, parseDuration, parseInteger, parseRetryAfter, pinoLevelToSeverity, pinoLineToRecord, probeHealth, quoteLogQL, readCommonEnv, recordsToOtlpJson, recordsToStreams, redactError, redactFields, redactHeaders, redactSecretsInText, redactText, redactUrl, requestLogger, resolveBatchOptions, resolveCommonConfig, safeCall, safeLogger, secondsToDate, secretValues, silentLogger, snappyCompress, stdoutLine, stringMap, tenantIdProblem, toNanos, toOtlpValue, toSearchLogQL, validateCommonConfig, validateLabelName, validateLabels, winstonLevelToSeverity, withRequestId };
3620
3862
  //# sourceMappingURL=index.js.map
3621
3863
  //# sourceMappingURL=index.js.map