diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 8599c304f..365b297e3 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -70,7 +70,7 @@ "python/frameworks/weaviate": "0.1.2", "python/frameworks/xai": "0.1.1", "python/traceai": "0.1.0", - "typescript/packages/fi-core": "1.2.0", + "typescript/packages/fi-core": "1.2.1", "typescript/packages/fi-semantic-conventions": "1.1.0", "typescript/packages/traceai_anthropic": "0.1.5", "typescript/packages/traceai_auto": "0.1.0", diff --git a/typescript/packages/fi-core/CHANGELOG.md b/typescript/packages/fi-core/CHANGELOG.md index 03c449609..8ab205788 100644 --- a/typescript/packages/fi-core/CHANGELOG.md +++ b/typescript/packages/fi-core/CHANGELOG.md @@ -1,3 +1,8 @@ +## [1.2.1] - pending release +### Fix +- **Spans no longer carry the process command line or the OS user.** 1.2.0 added OpenTelemetry's full process detector to every provider resource, so `process.command_args`, `process.command`, `process.owner`, `process.executable.path` and `process.pid` were exported with every span. Command-line flags can hold secrets. By default fi-core now detects only `process.runtime.name`, `process.runtime.version` and `process.runtime.description`, as it detected nothing before 1.2.0. +- To opt in, set the OpenTelemetry Node variable `OTEL_NODE_RESOURCE_DETECTORS` to a comma-separated list of `host`, `os` and `process`, or `all` / `none`. `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` are always applied. + ## [1.2.0] - pending release ### Changed - 1.2.0 exports the typed session.id set with setSession; other session-named attributes remain masked. diff --git a/typescript/packages/fi-core/README.md b/typescript/packages/fi-core/README.md index 4d54771d3..c9abcebdd 100644 --- a/typescript/packages/fi-core/README.md +++ b/typescript/packages/fi-core/README.md @@ -487,6 +487,13 @@ Other provider structures have no new exception without catalog/source evidence. resource identity, `OTEL_SERVICE_NAME`, `OTEL_RESOURCE_ATTRIBUTES` and an optional application `resource` are preserved. +Resource detection is minimal by default: the provider adds the Node.js runtime +name and version (`process.runtime.*`) and nothing that identifies the machine, +the OS user or the command line (which can hold secrets passed as flags). To add +OpenTelemetry's host, OS or full process attributes, set +`OTEL_NODE_RESOURCE_DETECTORS` to a comma-separated list of `host`, `os` and +`process`, or to `all`; `none` turns detection off. + `FITracer` applies the shared privacy policy before SDK attribute-value truncation. Its library spans use the same deterministic count-limit projection as wire spans (spec A7), including late output/usage updates. Protected attributes are reserved diff --git a/typescript/packages/fi-core/package.json b/typescript/packages/fi-core/package.json index 0dba30c41..a18f3482f 100644 --- a/typescript/packages/fi-core/package.json +++ b/typescript/packages/fi-core/package.json @@ -1,6 +1,6 @@ { "name": "@traceai/fi-core", - "version": "1.2.0", + "version": "1.2.1", "description": "OpenTelemetry tracing for AI applications: tracer setup, span helpers, and exporters for Future AGI.", "main": "./dist/src/index.js", "module": "./dist/esm/index.js", diff --git a/typescript/packages/fi-core/src/__tests__/resourceDetection.test.ts b/typescript/packages/fi-core/src/__tests__/resourceDetection.test.ts new file mode 100644 index 000000000..06c271985 --- /dev/null +++ b/typescript/packages/fi-core/src/__tests__/resourceDetection.test.ts @@ -0,0 +1,86 @@ +import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals'; +import { userInfo } from 'node:os'; +import { FITracerProvider } from '../otel'; + +jest.mock('@opentelemetry/exporter-trace-otlp-http', () => ({ + OTLPTraceExporter: jest.fn().mockImplementation(() => ({ + export: jest.fn((_spans: any, callback: any) => callback({ code: 0 })), + shutdown: jest.fn(() => Promise.resolve()), + forceFlush: jest.fn(() => Promise.resolve()), + })), +})); + +// A secret passed on the command line, as `node app.js --api-key=...` would. +const SECRET_FLAG = '--api-key=sk-test-0000000000000000'; + +// The resource every exported span carries. +function exportedResourceAttributes(): Record { + const provider = new FITracerProvider(); + const span = provider.getTracer('resource-detection-test').startSpan('probe') as any; + span.end(); + return { ...span.resource.attributes }; +} + +describe('FITracerProvider resource detection', () => { + const originalEnv = process.env; + const originalArgv = process.argv; + + beforeEach(() => { + process.env = { ...originalEnv }; + delete process.env.OTEL_NODE_RESOURCE_DETECTORS; + process.argv = [...originalArgv, SECRET_FLAG]; + }); + + afterEach(() => { + process.env = originalEnv; + process.argv = originalArgv; + }); + + it('exports no command line, OS user, executable path or host by default', () => { + const attributes = exportedResourceAttributes(); + for (const key of ['process.command', 'process.command_args', 'process.owner', 'process.executable.path', 'process.executable.name', 'process.pid', 'host.name', 'host.id', 'os.type']) { + expect(attributes).not.toHaveProperty([key]); + } + // service.name keeps OpenTelemetry's own default, unknown_service:, so the + // executable path is checked by key above rather than by value. + const serialized = JSON.stringify(attributes); + expect(serialized).not.toContain('sk-test-0000000000000000'); + expect(serialized).not.toContain(JSON.stringify(userInfo().username)); + }); + + it('keeps the runtime name and version by default', () => { + const attributes = exportedResourceAttributes(); + expect(attributes['process.runtime.name']).toBe('nodejs'); + expect(attributes['process.runtime.version']).toBe(process.versions.node); + }); + + it('keeps OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES', () => { + process.env.OTEL_SERVICE_NAME = 'checkout'; + process.env.OTEL_RESOURCE_ATTRIBUTES = 'deployment.environment.name=staging'; + const attributes = exportedResourceAttributes(); + expect(attributes['service.name']).toBe('checkout'); + expect(attributes['deployment.environment.name']).toBe('staging'); + }); + + it('adds full process detection only when OTEL_NODE_RESOURCE_DETECTORS asks for it', () => { + process.env.OTEL_NODE_RESOURCE_DETECTORS = 'env,process'; + const attributes = exportedResourceAttributes(); + expect(attributes['process.command_args']).toEqual(expect.arrayContaining([SECRET_FLAG])); + expect(attributes['process.pid']).toBe(process.pid); + expect(attributes).not.toHaveProperty(['host.name']); + }); + + it('treats all as host, os and process, and none as no detection', () => { + process.env.OTEL_NODE_RESOURCE_DETECTORS = 'all'; + const all = exportedResourceAttributes(); + expect(all).toHaveProperty(['host.name']); + expect(all).toHaveProperty(['os.type']); + expect(all).toHaveProperty(['process.pid']); + + process.env.OTEL_NODE_RESOURCE_DETECTORS = 'none'; + process.env.OTEL_SERVICE_NAME = 'checkout'; + const none = exportedResourceAttributes(); + expect(Object.keys(none).filter((key) => /^(process|host|os)\./.test(key))).toEqual([]); + expect(none['service.name']).toBe('checkout'); + }); +}); diff --git a/typescript/packages/fi-core/src/otel.ts b/typescript/packages/fi-core/src/otel.ts index 4c010e38b..4ced29544 100644 --- a/typescript/packages/fi-core/src/otel.ts +++ b/typescript/packages/fi-core/src/otel.ts @@ -25,7 +25,7 @@ import { SimpleSpanProcessor as OTelSimpleSpanProcessor, IdGenerator, } from "@opentelemetry/sdk-trace-node"; -import { Resource, resourceFromAttributes, detectResources, defaultResource, envDetector, processDetector } from "@opentelemetry/resources"; +import { Resource, ResourceDetector, resourceFromAttributes, detectResources, defaultResource, envDetector, hostDetector, osDetector, processDetector } from "@opentelemetry/resources"; import { privacyFilter, PrivacyOptions } from "./export-privacy"; import { credentialSecrets } from "./privacy-runtime"; import { seedSecrets } from "./secrets"; @@ -67,9 +67,26 @@ export function withExportPrivacy(exporter: SpanExporter, options: ExportPrivacy privacyDestinations.add(guarded); return guarded; } +// The full process detector exports the command line (which can hold secrets passed as +// flags), the OS user name and the executable path. By default only the runtime is kept. +const runtimeDetector: ResourceDetector = { + detect(config) { + const attributes = processDetector.detect(config).attributes ?? {}; + return { attributes: Object.fromEntries(Object.entries(attributes).filter(([key]) => key.startsWith("process.runtime."))) }; + }, +}; +const OPT_IN_DETECTORS: Record = { host: hostDetector, os: osDetector, process: processDetector }; +// OTEL_NODE_RESOURCE_DETECTORS (the OpenTelemetry Node variable) opts in to host, os and +// process detection. OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES are always applied. +function resourceDetectors(): ResourceDetector[] { + const names = (process.env.OTEL_NODE_RESOURCE_DETECTORS ?? "").split(",").map((name) => name.trim().toLowerCase()).filter(Boolean); + if (!names.length) return [envDetector, runtimeDetector]; + const chosen = names.includes("none") ? [] : names.includes("all") ? Object.keys(OPT_IN_DETECTORS) : names; + return [envDetector, ...chosen.flatMap((name) => OPT_IN_DETECTORS[name] ?? [])]; +} function applicationResource(resource?: Resource): Resource { const application = resource && typeof (resource as any).getRawAttributes === "function" ? resource : resourceFromAttributes(resource?.attributes ?? {}); - return defaultResource().merge(detectResources({ detectors: [envDetector, processDetector] })).merge(application); + return defaultResource().merge(detectResources({ detectors: resourceDetectors() })).merge(application); } import { v4 as uuidv4 } from "uuid"; // For UUID generation