From d5b6af5167a2791b3e14f7c0c9d50a9d00ec9e0a Mon Sep 17 00:00:00 2001 From: Matt Cotter Date: Thu, 24 Oct 2024 15:36:29 -0500 Subject: [PATCH 1/2] feat: validate the otel configuration in diagnose --- internal/commands/diagnose/diagnose.go | 1 + internal/commands/diagnose/otelconfigcheck.go | 56 +++++++++++++++++++ .../commands/diagnose/otelconfigcheck.tmpl | 5 ++ 3 files changed, 62 insertions(+) create mode 100644 internal/commands/diagnose/otelconfigcheck.go create mode 100644 internal/commands/diagnose/otelconfigcheck.tmpl diff --git a/internal/commands/diagnose/diagnose.go b/internal/commands/diagnose/diagnose.go index 2e7a36825..16d65ae19 100644 --- a/internal/commands/diagnose/diagnose.go +++ b/internal/commands/diagnose/diagnose.go @@ -23,6 +23,7 @@ type Diagnostic struct { var diagnostics = []Diagnostic{ configDiagnostic(), + otelconfigDiagnostic(), authDiagnostic(), } diff --git a/internal/commands/diagnose/otelconfigcheck.go b/internal/commands/diagnose/otelconfigcheck.go new file mode 100644 index 000000000..1a214a4d5 --- /dev/null +++ b/internal/commands/diagnose/otelconfigcheck.go @@ -0,0 +1,56 @@ +package diagnose + +import ( + "context" + "embed" + + "github.com/observeinc/observe-agent/internal/commands/start" + "github.com/spf13/viper" + "go.opentelemetry.io/collector/otelcol" +) + +type OtelConfigTestResult struct { + Passed bool + Error string +} + +func checkOtelConfig(_ *viper.Viper) (any, error) { + colSettings, cleanup, err := start.SetupAndGenerateCollectorSettings() + if err != nil { + return nil, err + } + if cleanup != nil { + defer cleanup() + } + // Copying the implementation from the `otelcol validate` command + col, err := otelcol.NewCollector(*colSettings) + if err != nil { + return nil, err + } + err = col.DryRun(context.Background()) + if err != nil { + return OtelConfigTestResult{ + Passed: false, + Error: err.Error(), + }, nil + } + return OtelConfigTestResult{ + Passed: true, + }, nil +} + +const otelconfigcheckTemplate = "otelconfigcheck.tmpl" + +var ( + //go:embed otelconfigcheck.tmpl + otelconfigcheckTemplateFS embed.FS +) + +func otelconfigDiagnostic() Diagnostic { + return Diagnostic{ + check: checkOtelConfig, + checkName: "OTEL Config Check", + templateName: otelconfigcheckTemplate, + templateFS: otelconfigcheckTemplateFS, + } +} diff --git a/internal/commands/diagnose/otelconfigcheck.tmpl b/internal/commands/diagnose/otelconfigcheck.tmpl new file mode 100644 index 000000000..dbe90e51d --- /dev/null +++ b/internal/commands/diagnose/otelconfigcheck.tmpl @@ -0,0 +1,5 @@ +{{- if .Passed }} +OTEL configuration is valid. +{{- else }} +⚠️ OTEL configuration validation failed with error {{ .Error }} +{{- end }} From a1532ba77418210e98a258b930944dc48e7db612 Mon Sep 17 00:00:00 2001 From: Matt Cotter Date: Mon, 28 Oct 2024 15:00:43 -0500 Subject: [PATCH 2/2] link to the otel validate command --- internal/commands/diagnose/otelconfigcheck.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/commands/diagnose/otelconfigcheck.go b/internal/commands/diagnose/otelconfigcheck.go index 1a214a4d5..5a33976b5 100644 --- a/internal/commands/diagnose/otelconfigcheck.go +++ b/internal/commands/diagnose/otelconfigcheck.go @@ -22,7 +22,8 @@ func checkOtelConfig(_ *viper.Viper) (any, error) { if cleanup != nil { defer cleanup() } - // Copying the implementation from the `otelcol validate` command + // These are the same checks as the `otelcol validate` command: + // https://github.com/open-telemetry/opentelemetry-collector/blob/main/otelcol/command_validate.go col, err := otelcol.NewCollector(*colSettings) if err != nil { return nil, err