Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/desktop/scripts/electron-launcher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export function makeDevelopmentEnvironmentScript(environment) {
["T3CODE_COMMIT_HASH", environment.T3CODE_COMMIT_HASH],
["T3CODE_OTLP_TRACES_URL", environment.T3CODE_OTLP_TRACES_URL],
["T3CODE_OTLP_EXPORT_INTERVAL_MS", environment.T3CODE_OTLP_EXPORT_INTERVAL_MS],
["T3CODE_OTLP_HEADERS", environment.T3CODE_OTLP_HEADERS],
["T3CODE_DESKTOP_APP_USER_MODEL_ID", APP_BUNDLE_ID],
].filter((entry) => typeof entry[1] === "string" && entry[1].trim().length > 0);
return [
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/app/DesktopConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OtlpHeadersFromString } from "@t3tools/shared/observability";
import * as Config from "effect/Config";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Option from "effect/Option";
Expand Down Expand Up @@ -48,6 +49,7 @@ export const DesktopConfig = Config.all({
otlpExportIntervalMs: Config.int("T3CODE_OTLP_EXPORT_INTERVAL_MS").pipe(
Config.withDefault(10_000),
),
otlpHeaders: Config.schema(OtlpHeadersFromString, "T3CODE_OTLP_HEADERS").pipe(Config.option),
appImagePath: trimmedString("APPIMAGE"),
disableAutoUpdate: optionalBoolean("T3CODE_DISABLE_AUTO_UPDATE"),
mockUpdates: optionalBoolean("T3CODE_DESKTOP_MOCK_UPDATES"),
Expand Down
8 changes: 8 additions & 0 deletions apps/desktop/src/app/DesktopEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe("DesktopEnvironment", () => {
T3CODE_DEV_REMOTE_T3_SERVER_ENTRY_PATH: " /remote/server.mjs ",
T3CODE_OTLP_TRACES_URL: " http://127.0.0.1:4318/v1/traces ",
T3CODE_OTLP_EXPORT_INTERVAL_MS: "2500",
T3CODE_OTLP_HEADERS: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
},
);

Expand Down Expand Up @@ -85,6 +86,13 @@ describe("DesktopEnvironment", () => {
assert.deepEqual(environment.commitHashOverride, Option.some("0123456789abcdef"));
assert.deepEqual(environment.otlpTracesUrl, Option.some("http://127.0.0.1:4318/v1/traces"));
assert.equal(environment.otlpExportIntervalMs, 2500);
assert.deepEqual(
environment.otlpHeaders,
Option.some({
authorization: "Basic abc==",
"x-tenant": "t3",
}),
);
}),
);

Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/app/DesktopEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class DesktopEnvironment extends Context.Service<
readonly commitHashOverride: Option.Option<string>;
readonly otlpTracesUrl: Option.Option<string>;
readonly otlpExportIntervalMs: number;
readonly otlpHeaders: Option.Option<Record<string, string>>;
readonly branding: DesktopAppBranding;
readonly displayName: string;
readonly appUserModelId: string;
Expand Down Expand Up @@ -225,6 +226,7 @@ const make = Effect.fn("desktop.environment.make")(function* (
commitHashOverride: config.commitHashOverride,
otlpTracesUrl: config.otlpTracesUrl,
otlpExportIntervalMs: config.otlpExportIntervalMs,
otlpHeaders: config.otlpHeaders,
branding,
displayName,
appUserModelId: Option.getOrElse(config.appUserModelIdOverride, () =>
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/app/DesktopObservability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ const tracerLayer = Layer.unwrap(
: yield* OtlpTracer.make({
url: otlpTracesUrl.value,
exportInterval: `${environment.otlpExportIntervalMs} millis`,
headers: Option.getOrUndefined(environment.otlpHeaders),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
resource: {
serviceName: "desktop",
attributes: {
Expand Down
5 changes: 4 additions & 1 deletion apps/desktop/src/backend/DesktopBackendConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,12 @@ describe("DesktopBackendConfiguration", () => {
const previousWslEnv = process.env.WSLENV;
const previousOpenAiKey = process.env.OPENAI_API_KEY;
const previousAnthropicKey = process.env.ANTHROPIC_API_KEY;
const previousOtlpHeaders = process.env.T3CODE_OTLP_HEADERS;
try {
process.env.WSLENV = "GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u";
process.env.OPENAI_API_KEY = "openai-key";
process.env.ANTHROPIC_API_KEY = "anthropic-key";
process.env.T3CODE_OTLP_HEADERS = 'authorization="Bearer%20my-token"';

yield* Effect.gen(function* () {
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
Expand All @@ -882,7 +884,7 @@ describe("DesktopBackendConfiguration", () => {
// already declared, so it isn't forwarded twice.
assert.equal(
config.env.WSLENV,
"GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u:ANTHROPIC_API_KEY",
"GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u:ANTHROPIC_API_KEY:T3CODE_OTLP_HEADERS",
);
}).pipe(
Effect.provide(
Expand All @@ -905,6 +907,7 @@ describe("DesktopBackendConfiguration", () => {
restoreEnv("WSLENV", previousWslEnv);
restoreEnv("OPENAI_API_KEY", previousOpenAiKey);
restoreEnv("ANTHROPIC_API_KEY", previousAnthropicKey);
restoreEnv("T3CODE_OTLP_HEADERS", previousOtlpHeaders);
}
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
Expand Down
6 changes: 5 additions & 1 deletion apps/desktop/src/backend/DesktopBackendConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ const DESKTOP_BACKEND_ENV_NAMES = [
// forward across the wsl.exe boundary without WSLENV. The dev-server URL is
// handled separately via a `--dev-url` CLI flag because WSLENV translation of
// URL-shaped values (colons / slashes) is unreliable.
const WSL_FORWARDED_ENV_NAMES = ["OPENAI_API_KEY", "ANTHROPIC_API_KEY"] as const;
const WSL_FORWARDED_ENV_NAMES = [
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"T3CODE_OTLP_HEADERS",
] as const;

const WSL_SERVER_SYSTEM_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";

Expand Down
1 change: 1 addition & 0 deletions apps/server/src/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const makeCliTestServerConfig = (baseDir: string) =>
otlpMetricsUrl: undefined,
otlpExportIntervalMs: 10_000,
otlpServiceName: "t3-server",
otlpHeaders: undefined,
mode: "web",
port: 0,
host: "127.0.0.1",
Expand Down
89 changes: 89 additions & 0 deletions apps/server/src/cli/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
otlpMetricsUrl: undefined,
otlpExportIntervalMs: 10_000,
otlpServiceName: "t3-server",
otlpHeaders: undefined,
devAllowedOrigins: [],
} as const;

Expand Down Expand Up @@ -719,4 +720,92 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
});
}),
);

it.effect("decodes percent-encoded OTLP headers from env", () =>
Effect.gen(function* () {
const { join } = yield* Path.Path;
const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-base");

const resolved = yield* resolveServerConfig(
{
mode: Option.some("web"),
port: Option.some(3773),
host: Option.none(),
baseDir: Option.some(baseDir),
cwd: Option.none(),
devUrl: Option.none(),
noBrowser: Option.none(),
bootstrapFd: Option.none(),
autoBootstrapProjectFromCwd: Option.none(),
logWebSocketEvents: Option.none(),
tailscaleServeEnabled: Option.none(),
tailscaleServePort: Option.none(),
},
Option.none(),
).pipe(
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(
ConfigProvider.fromEnv({
env: {
T3CODE_OTLP_HEADERS: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
},
}),
),
NetService.layer,
),
),
);

expect(resolved.otlpHeaders).toEqual({
authorization: "Basic abc==",
"x-tenant": "t3",
});
}),
);

it.effect("keeps whitespace-separated pairs and literal equals signs in OTLP headers", () =>
Effect.gen(function* () {
const { join } = yield* Path.Path;
const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-loose-base");

const resolved = yield* resolveServerConfig(
{
mode: Option.some("web"),
port: Option.some(3773),
host: Option.none(),
baseDir: Option.some(baseDir),
cwd: Option.none(),
devUrl: Option.none(),
noBrowser: Option.none(),
bootstrapFd: Option.none(),
autoBootstrapProjectFromCwd: Option.none(),
logWebSocketEvents: Option.none(),
tailscaleServeEnabled: Option.none(),
tailscaleServePort: Option.none(),
},
Option.none(),
).pipe(
Effect.provide(
Layer.mergeAll(
ConfigProvider.layer(
ConfigProvider.fromEnv({
env: {
T3CODE_OTLP_HEADERS: "authorization=Bearer abc==, x-tenant=t3",
T3CODE_OTLP_TRACES_URL: "http://collector.internal:4318",
},
}),
),
NetService.layer,
),
),
);

expect(resolved.otlpHeaders).toEqual({
authorization: "Bearer abc==",
"x-tenant": "t3",
});
expect(resolved.otlpTracesUrl).toBe("http://collector.internal:4318");
}),
);
});
6 changes: 6 additions & 0 deletions apps/server/src/cli/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as NetService from "@t3tools/shared/Net";
import { OtlpHeadersFromString } from "@t3tools/shared/observability";
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
import { DesktopBackendBootstrap, PortSchema } from "@t3tools/contracts";
import * as Config from "effect/Config";
Expand Down Expand Up @@ -99,6 +100,10 @@ const EnvServerConfig = Config.all({
Config.withDefault(10_000),
),
otlpServiceName: Config.string("T3CODE_OTLP_SERVICE_NAME").pipe(Config.withDefault("t3-server")),
otlpHeaders: Config.schema(OtlpHeadersFromString, "T3CODE_OTLP_HEADERS").pipe(
Config.option,
Config.map(Option.getOrUndefined),
),
mode: Config.schema(ServerConfig.RuntimeMode, "T3CODE_MODE").pipe(
Config.option,
Config.map(Option.getOrUndefined),
Expand Down Expand Up @@ -387,6 +392,7 @@ export const resolveServerConfig = (
persistedObservabilitySettings.otlpMetricsUrl,
otlpExportIntervalMs: env.otlpExportIntervalMs,
otlpServiceName: env.otlpServiceName,
otlpHeaders: env.otlpHeaders,
mode,
port,
cwd,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/cli/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ const makePairServerConfig = Effect.fn(function* (input: {
otlpMetricsUrl: undefined,
otlpExportIntervalMs: 10_000,
otlpServiceName: "t3-server",
otlpHeaders: undefined,
mode: "web",
port: state.port,
host: state.host,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class ServerConfig extends Context.Service<
readonly otlpMetricsUrl: string | undefined;
readonly otlpExportIntervalMs: number;
readonly otlpServiceName: string;
readonly otlpHeaders: Readonly<Record<string, string>> | undefined;
readonly mode: RuntimeMode;
readonly port: number;
readonly host: string | undefined;
Expand Down Expand Up @@ -197,6 +198,7 @@ const makeTest = Effect.fn("ServerConfig.makeTest")(function* (
otlpMetricsUrl: undefined,
otlpExportIntervalMs: 10_000,
otlpServiceName: "t3-server",
otlpHeaders: undefined,
cwd,
baseDir,
...derivedPaths,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/environment/ServerEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const makeServerConfig = Effect.fn(function* (baseDir: string) {
otlpMetricsUrl: undefined,
otlpExportIntervalMs: 10_000,
otlpServiceName: "t3-server",
otlpHeaders: undefined,
cwd: process.cwd(),
baseDir,
mode: "web",
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export const otlpTracesProxyRouteLayer = HttpRouter.add(
const request = yield* HttpServerRequest.HttpServerRequest;
const config = yield* ServerConfig.ServerConfig;
const otlpTracesUrl = config.otlpTracesUrl;
const otlpHeaders = config.otlpHeaders;
const browserTraceCollector = yield* BrowserTraceCollector.BrowserTraceCollector;
const httpClient = yield* HttpClient.HttpClient;
const bodyJson = cast<unknown, OtlpTracer.TraceData>(yield* request.json);
Expand All @@ -343,6 +344,7 @@ export const otlpTracesProxyRouteLayer = HttpRouter.add(
return yield* httpClient
.post(otlpTracesUrl, {
body: HttpBody.jsonUnsafe(bodyJson),
headers: otlpHeaders,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})
.pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/observability/Layers/Observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const ObservabilityLive = Layer.unwrap(
: yield* OtlpTracer.make({
url: config.otlpTracesUrl,
exportInterval: `${config.otlpExportIntervalMs} millis`,
headers: config.otlpHeaders,
resource: {
serviceName: config.otlpServiceName,
attributes: {
Expand Down Expand Up @@ -80,6 +81,7 @@ export const ObservabilityLive = Layer.unwrap(
: OtlpMetrics.layer({
url: config.otlpMetricsUrl,
exportInterval: `${config.otlpExportIntervalMs} millis`,
headers: config.otlpHeaders,
resource: {
serviceName: config.otlpServiceName,
attributes: {
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ const buildAppUnderTest = (options?: {
otlpMetricsUrl: undefined,
otlpExportIntervalMs: 10_000,
otlpServiceName: "t3-server",
otlpHeaders: undefined,
mode: "desktop",
port: 0,
host: "127.0.0.1",
Expand Down
2 changes: 2 additions & 0 deletions docs/operations/observability.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ OTLP export:
- `T3CODE_OTLP_METRICS_URL`: OTLP metric endpoint
- `T3CODE_OTLP_EXPORT_INTERVAL_MS`: export interval, default `10000`
- `T3CODE_OTLP_SERVICE_NAME`: service name, default `t3-server`
- `T3CODE_OTLP_HEADERS`: extra headers for both exporters, same format as
`OTEL_EXPORTER_OTLP_HEADERS`: comma-separated `key=value` pairs with percent-encoded values.

If the OTLP URLs are unset, local tracing still works and metrics stay in-process only.

Expand Down
40 changes: 39 additions & 1 deletion packages/shared/src/observability.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, describe, it } from "@effect/vitest";
import { assert, describe, expect, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Arr from "effect/Array";
import * as Cause from "effect/Cause";
Expand All @@ -21,6 +21,7 @@ import {
makeTraceSink,
type TraceRecord,
type TraceSinkFlushStats,
OtlpHeadersFromString,
truncateTraceAttributes,
} from "./observability.ts";

Expand Down Expand Up @@ -459,3 +460,40 @@ describe("observability", () => {
);
});
});

describe("OtlpHeadersFromString", () => {
const decode = Schema.decodeUnknownSync(OtlpHeadersFromString);

it.each([
{
name: "decodes percent-encoded values",
input: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
expected: { authorization: "Basic abc==", "x-tenant": "t3" },
},
{
name: "ignores whitespace around separators",
input: "authorization=Basic%20abc%3D%3D, x-tenant = t3 ,",
expected: { authorization: "Basic abc==", "x-tenant": "t3" },
},
{
name: "keeps literal equals signs inside a value",
input: "authorization=Bearer abc==",
expected: { authorization: "Bearer abc==" },
},
{
name: "keeps an empty value",
input: "x-empty=",
expected: { "x-empty": "" },
},
])("$name", ({ input, expected }) => {
expect(decode(input)).toEqual(expected);
});

it.each([
{ name: "rejects a pair without a separator", input: "authorization" },
{ name: "rejects a pair without a key", input: "=value" },
{ name: "rejects a malformed percent-encoding", input: "authorization=%E0" },
])("$name", ({ input }) => {
expect(() => decode(input)).toThrow();
});
});
Loading
Loading