feat(plugin): expose app metadata (#38179)

This commit is contained in:
Dax 2026-07-21 18:16:25 -04:00 committed by GitHub
commit 8de40be6ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
71 changed files with 477 additions and 286 deletions

View file

@ -1,7 +1,6 @@
import { Formatter, Logger, type LogLevel } from "effect"
import path from "path"
import { Global } from "../global.js"
import { InstallationChannel, InstallationLocal } from "../installation/version.js"
import { runID } from "./shared.js"
function formatter(id: string = runID) {
@ -47,7 +46,7 @@ function format(input: unknown) {
return /^[^\s="\\]+$/.test(value) ? value : JSON.stringify(value)
}
export function file(local = InstallationLocal, channel = InstallationChannel) {
export function file(local = true, channel = "local") {
if (!local) return path.join(Global.Path.log, "opencode.log")
return path.join(Global.Path.log, `opencode-${channel.replace(/[^a-zA-Z0-9._-]/g, "-")}.log`)
}
@ -70,8 +69,9 @@ export function minimumLogLevel() {
return value && value in levels ? levels[value as keyof typeof levels] : levels.INFO
}
export function loggers() {
return process.env.OPENCODE_PRINT_LOGS === "1" ? [fileLogger(), stderrLogger] : [fileLogger()]
export function loggers(local = true, channel = "local") {
const logger = fileLogger(file(local, channel))
return process.env.OPENCODE_PRINT_LOGS === "1" ? [logger, stderrLogger] : [logger]
}
export * as Logging from "./logging.js"

View file

@ -1,6 +1,5 @@
import { Layer } from "effect"
import { OtlpLogger } from "effect/unstable/observability"
import { InstallationChannel, InstallationVersion } from "../installation/version.js"
import { runID } from "./shared.js"
export interface Options {
@ -8,6 +7,12 @@ export interface Options {
readonly headers?: string
}
export interface App {
readonly client: string
readonly version: string
readonly channel: string
}
function parseHeaders(value?: string) {
return value
? value.split(",").reduce(
@ -37,36 +42,36 @@ function resourceAttributes() {
}
}
export function resource(client = "cli"): {
export function resource(app: App = { client: "opencode", version: "unknown", channel: "local" }): {
serviceName: string
serviceVersion: string
attributes: Record<string, string>
} {
return {
serviceName: "opencode",
serviceVersion: InstallationVersion,
serviceVersion: app.version,
attributes: {
...resourceAttributes(),
"deployment.environment.name": InstallationChannel,
"opencode.client": client,
"deployment.environment.name": app.channel,
"opencode.client": app.client,
"opencode.run": runID,
"service.instance.id": runID,
},
}
}
export function loggers(options: Options | undefined, client: string) {
export function loggers(options: Options | undefined, app: App) {
if (!options?.endpoint) return []
return [
OtlpLogger.make({
url: `${options.endpoint}/v1/logs`,
resource: resource(client),
resource: resource(app),
headers: parseHeaders(options.headers),
}),
]
}
export async function tracingLayer(options: Options | undefined, client: string) {
export async function tracingLayer(options: Options | undefined, app: App) {
if (!options?.endpoint) return Layer.empty
const NodeSdk = await import("@effect/opentelemetry/NodeSdk")
const OTLP = await import("@opentelemetry/exporter-trace-otlp-http")
@ -80,7 +85,7 @@ export async function tracingLayer(options: Options | undefined, client: string)
context.setGlobalContextManager(manager)
return NodeSdk.layer(() => ({
resource: resource(client),
resource: resource(app),
spanProcessor: new SdkBase.BatchSpanProcessor(
new OTLP.OTLPTraceExporter({
url: `${options.endpoint}/v1/traces`,