A continuation of HomeBox the inventory and organization system built for the Home User
Enable frontend telemetry automatically when backend telemetry is enabled via status endpoint
copilot-swe-agent[bot]ea84a16d300f
diff --git a/backend/app/api/handlers/v1/controller.go b/backend/app/api/handlers/v1/controller.goindex 5b154d24..af1a05b0 100644--- a/backend/app/api/handlers/v1/controller.go+++ b/backend/app/api/handlers/v1/controller.go@@ -98,6 +98,7 @@ type (AllowRegistration bool `json:"allowRegistration"`LabelPrinting bool `json:"labelPrinting"`OIDC OIDCStatus `json:"oidc"`+ Telemetry TelemetryStatus `json:"telemetry"`}OIDCStatus struct {@@ -106,6 +107,10 @@ type (AutoRedirect bool `json:"autoRedirect,omitempty"`AllowLocal bool `json:"allowLocal"`}++ TelemetryStatus struct {+ Enabled bool `json:"enabled"`+ })func NewControllerV1(svc *services.AllServices, repos *repo.AllRepos, bus *eventbus.EventBus, config *config.Config, options ...func(*V1Controller)) *V1Controller {@@ -162,6 +167,9 @@ func (ctrl *V1Controller) HandleBase(ready ReadyFunc, build Build) errchain.HandAutoRedirect: ctrl.config.OIDC.AutoRedirect,AllowLocal: ctrl.config.Options.AllowLocalLogin,},+ Telemetry: TelemetryStatus{+ Enabled: ctrl.config.OTel.Enabled,+ },})}}diff --git a/backend/app/api/static/docs/docs.go b/backend/app/api/static/docs/docs.goindex ea89e3fb..a53d023a 100644--- a/backend/app/api/static/docs/docs.go+++ b/backend/app/api/static/docs/docs.go@@ -5352,6 +5352,9 @@ const docTemplate = `{"oidc": {"$ref": "#/definitions/v1.OIDCStatus"},+ "telemetry": {+ "$ref": "#/definitions/v1.TelemetryStatus"+ },"title": {"type": "string"},@@ -5533,6 +5536,14 @@ const docTemplate = `{}}},+ "v1.TelemetryStatus": {+ "type": "object",+ "properties": {+ "enabled": {+ "type": "boolean"+ }+ }+ },"v1.TokenResponse": {"type": "object","properties": {diff --git a/backend/app/api/static/docs/openapi-3.json b/backend/app/api/static/docs/openapi-3.jsonindex 2f694ac7..07fc124d 100644--- a/backend/app/api/static/docs/openapi-3.json+++ b/backend/app/api/static/docs/openapi-3.json@@ -5549,6 +5549,9 @@"oidc": {"$ref": "#/components/schemas/v1.OIDCStatus"},+ "telemetry": {+ "$ref": "#/components/schemas/v1.TelemetryStatus"+ },"title": {"type": "string"},@@ -5730,6 +5733,14 @@}}},+ "v1.TelemetryStatus": {+ "type": "object",+ "properties": {+ "enabled": {+ "type": "boolean"+ }+ }+ },"v1.TokenResponse": {"type": "object","properties": {diff --git a/backend/app/api/static/docs/openapi-3.yaml b/backend/app/api/static/docs/openapi-3.yamlindex ff303844..d6908610 100644--- a/backend/app/api/static/docs/openapi-3.yaml+++ b/backend/app/api/static/docs/openapi-3.yaml@@ -3540,6 +3540,8 @@ components:type: stringoidc:$ref: "#/components/schemas/v1.OIDCStatus"+ telemetry:+ $ref: "#/components/schemas/v1.TelemetryStatus"title:type: stringversions:@@ -3658,6 +3660,11 @@ components:type: stringenabled:type: boolean+ v1.TelemetryStatus:+ type: object+ properties:+ enabled:+ type: booleanv1.TokenResponse:type: objectproperties:diff --git a/backend/app/api/static/docs/swagger.json b/backend/app/api/static/docs/swagger.jsonindex 8662b78e..a460a9b9 100644--- a/backend/app/api/static/docs/swagger.json+++ b/backend/app/api/static/docs/swagger.json@@ -5350,6 +5350,9 @@"oidc": {"$ref": "#/definitions/v1.OIDCStatus"},+ "telemetry": {+ "$ref": "#/definitions/v1.TelemetryStatus"+ },"title": {"type": "string"},@@ -5531,6 +5534,14 @@}}},+ "v1.TelemetryStatus": {+ "type": "object",+ "properties": {+ "enabled": {+ "type": "boolean"+ }+ }+ },"v1.TokenResponse": {"type": "object","properties": {diff --git a/backend/app/api/static/docs/swagger.yaml b/backend/app/api/static/docs/swagger.yamlindex a340de23..45415b7d 100644--- a/backend/app/api/static/docs/swagger.yaml+++ b/backend/app/api/static/docs/swagger.yaml@@ -1790,6 +1790,8 @@ definitions:type: stringoidc:$ref: '#/definitions/v1.OIDCStatus'+ telemetry:+ $ref: '#/definitions/v1.TelemetryStatus'title:type: stringversions:@@ -1909,6 +1911,11 @@ definitions:enabled:type: booleantype: object+ v1.TelemetryStatus:+ properties:+ enabled:+ type: boolean+ type: objectv1.TokenResponse:properties:attachmentToken:diff --git a/docs/en/api/openapi-2.0.json b/docs/en/api/openapi-2.0.jsonindex 8662b78e..a460a9b9 100644--- a/docs/en/api/openapi-2.0.json+++ b/docs/en/api/openapi-2.0.json@@ -5350,6 +5350,9 @@"oidc": {"$ref": "#/definitions/v1.OIDCStatus"},+ "telemetry": {+ "$ref": "#/definitions/v1.TelemetryStatus"+ },"title": {"type": "string"},@@ -5531,6 +5534,14 @@}}},+ "v1.TelemetryStatus": {+ "type": "object",+ "properties": {+ "enabled": {+ "type": "boolean"+ }+ }+ },"v1.TokenResponse": {"type": "object","properties": {diff --git a/docs/en/api/openapi-2.0.yaml b/docs/en/api/openapi-2.0.yamlindex a340de23..45415b7d 100644--- a/docs/en/api/openapi-2.0.yaml+++ b/docs/en/api/openapi-2.0.yaml@@ -1790,6 +1790,8 @@ definitions:type: stringoidc:$ref: '#/definitions/v1.OIDCStatus'+ telemetry:+ $ref: '#/definitions/v1.TelemetryStatus'title:type: stringversions:@@ -1909,6 +1911,11 @@ definitions:enabled:type: booleantype: object+ v1.TelemetryStatus:+ properties:+ enabled:+ type: boolean+ type: objectv1.TokenResponse:properties:attachmentToken:diff --git a/docs/en/api/openapi-3.0.json b/docs/en/api/openapi-3.0.jsonindex 2f694ac7..07fc124d 100644--- a/docs/en/api/openapi-3.0.json+++ b/docs/en/api/openapi-3.0.json@@ -5549,6 +5549,9 @@"oidc": {"$ref": "#/components/schemas/v1.OIDCStatus"},+ "telemetry": {+ "$ref": "#/components/schemas/v1.TelemetryStatus"+ },"title": {"type": "string"},@@ -5730,6 +5733,14 @@}}},+ "v1.TelemetryStatus": {+ "type": "object",+ "properties": {+ "enabled": {+ "type": "boolean"+ }+ }+ },"v1.TokenResponse": {"type": "object","properties": {diff --git a/docs/en/api/openapi-3.0.yaml b/docs/en/api/openapi-3.0.yamlindex ff303844..d6908610 100644--- a/docs/en/api/openapi-3.0.yaml+++ b/docs/en/api/openapi-3.0.yaml@@ -3540,6 +3540,8 @@ components:type: stringoidc:$ref: "#/components/schemas/v1.OIDCStatus"+ telemetry:+ $ref: "#/components/schemas/v1.TelemetryStatus"title:type: stringversions:@@ -3658,6 +3660,11 @@ components:type: stringenabled:type: boolean+ v1.TelemetryStatus:+ type: object+ properties:+ enabled:+ type: booleanv1.TokenResponse:type: objectproperties:diff --git a/docs/en/api/swagger-2.0.json b/docs/en/api/swagger-2.0.jsonindex 8662b78e..a460a9b9 100644--- a/docs/en/api/swagger-2.0.json+++ b/docs/en/api/swagger-2.0.json@@ -5350,6 +5350,9 @@"oidc": {"$ref": "#/definitions/v1.OIDCStatus"},+ "telemetry": {+ "$ref": "#/definitions/v1.TelemetryStatus"+ },"title": {"type": "string"},@@ -5531,6 +5534,14 @@}}},+ "v1.TelemetryStatus": {+ "type": "object",+ "properties": {+ "enabled": {+ "type": "boolean"+ }+ }+ },"v1.TokenResponse": {"type": "object","properties": {diff --git a/docs/en/api/swagger-2.0.yaml b/docs/en/api/swagger-2.0.yamlindex a340de23..45415b7d 100644--- a/docs/en/api/swagger-2.0.yaml+++ b/docs/en/api/swagger-2.0.yaml@@ -1790,6 +1790,8 @@ definitions:type: stringoidc:$ref: '#/definitions/v1.OIDCStatus'+ telemetry:+ $ref: '#/definitions/v1.TelemetryStatus'title:type: stringversions:@@ -1909,6 +1911,11 @@ definitions:enabled:type: booleantype: object+ v1.TelemetryStatus:+ properties:+ enabled:+ type: boolean+ type: objectv1.TokenResponse:properties:attachmentToken:diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.tsindex 442f09fa..c54c5936 100644--- a/frontend/lib/api/types/data-contracts.ts+++ b/frontend/lib/api/types/data-contracts.ts@@ -1094,6 +1094,7 @@ export interface APISummary {latest: Latest;message: string;oidc: OIDCStatus;+ telemetry: TelemetryStatus;title: string;versions: string[];}@@ -1174,6 +1175,10 @@ export interface OIDCStatus {enabled: boolean;}+export interface TelemetryStatus {+ enabled: boolean;+}+export interface TokenResponse {attachmentToken: string;expiresAt: Date | string;diff --git a/frontend/nuxt.config.ts b/frontend/nuxt.config.tsindex 02c7d647..2cfa9425 100644--- a/frontend/nuxt.config.ts+++ b/frontend/nuxt.config.ts@@ -27,11 +27,11 @@ export default defineNuxtConfig({},// Runtime config for OpenTelemetry+ // Note: otelEnabled is determined automatically by querying the backend status endpoint.+ // When the backend has telemetry enabled, the frontend will automatically enable it.runtimeConfig: {public: {// OpenTelemetry configuration (can be overridden by environment variables)- // NUXT_PUBLIC_OTEL_ENABLED, NUXT_PUBLIC_OTEL_SERVICE_NAME, etc.- otelEnabled: process.env.NUXT_PUBLIC_OTEL_ENABLED || "false",otelServiceName: process.env.NUXT_PUBLIC_OTEL_SERVICE_NAME || "homebox-frontend",otelServiceVersion: process.env.NUXT_PUBLIC_OTEL_SERVICE_VERSION || "1.0.0",otelSampleRate: process.env.NUXT_PUBLIC_OTEL_SAMPLE_RATE || "1.0",diff --git a/frontend/plugins/otel.client.ts b/frontend/plugins/otel.client.tsindex 393536d0..8cf08550 100644--- a/frontend/plugins/otel.client.ts+++ b/frontend/plugins/otel.client.ts@@ -1,37 +1,81 @@/*** Nuxt plugin to initialize OpenTelemetry on the client side.* This ensures tracing is set up early in the application lifecycle.+ *+ * The plugin automatically enables telemetry when the backend has it enabled,+ * by querying the /api/v1/status endpoint.*/import { initializeOTel } from "~~/lib/otel";+import { usePublicApi } from "~~/composables/use-api";-export default defineNuxtPlugin(() => {+// Timeout for the status API call to prevent blocking the app+const STATUS_API_TIMEOUT_MS = 3000;++/**+ * Fetch backend status with a timeout to prevent blocking app startup.+ */+async function fetchBackendTelemetryStatus(debug: boolean): Promise<boolean> {+ const api = usePublicApi();++ // Create a promise that rejects after timeout+ const timeoutPromise = new Promise<never>((_, reject) => {+ setTimeout(() => reject(new Error("Status API timeout")), STATUS_API_TIMEOUT_MS);+ });++ try {+ // Race between the API call and the timeout+ const { data } = await Promise.race([api.status(), timeoutPromise]);+ const enabled = data?.telemetry?.enabled ?? false;++ if (debug) {+ console.log("[OTel] Backend telemetry status:", enabled);+ }+ return enabled;+ } catch (error) {+ if (debug) {+ console.warn("[OTel] Failed to fetch backend status:", error);+ }+ return false;+ }+}++export default defineNuxtPlugin(async () => {// Only run on client sideif (import.meta.client) {- // Check for runtime config for OTel settings- // These can be set via environment variables or nuxt.config.tsconst runtimeConfig = useRuntimeConfig();+ const otelDebug = String(runtimeConfig.public?.otelDebug || "false") === "true";++ // Check if backend has telemetry enabled via the status endpoint+ const backendTelemetryEnabled = await fetchBackendTelemetryStatus(otelDebug);- // Get OTel configuration from runtime config or use defaults- const otelEnabled = String(runtimeConfig.public?.otelEnabled || "false");- const otelDebug = String(runtimeConfig.public?.otelDebug || "false");+ // Only enable if backend telemetry is enabled+ if (!backendTelemetryEnabled) {+ if (otelDebug) {+ console.log("[OTel] Telemetry disabled (backend telemetry not enabled)");+ }+ return {+ provide: {+ otelEnabled: false,+ },+ };+ }const otelConfig = {- enabled: otelEnabled === "true",+ enabled: true,serviceName: String(runtimeConfig.public?.otelServiceName || "homebox-frontend"),serviceVersion: String(runtimeConfig.public?.otelServiceVersion || "1.0.0"),useBackendProxy: true, // Always use backend proxy for securitysampleRate: parseFloat(String(runtimeConfig.public?.otelSampleRate || "1.0")),- debug: otelDebug === "true",+ debug: otelDebug,};// Initialize OpenTelemetryinitializeOTel(otelConfig);- // Provide composable for accessing OTel in componentsreturn {provide: {- otelEnabled: otelConfig.enabled,+ otelEnabled: true,},};}