Skip to content

feat: validate the otel configuration in diagnose #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2024
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 internal/commands/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Diagnostic struct {

var diagnostics = []Diagnostic{
configDiagnostic(),
otelconfigDiagnostic(),
authDiagnostic(),
}

Expand Down
57 changes: 57 additions & 0 deletions internal/commands/diagnose/otelconfigcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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()
}
// 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
}
err = col.DryRun(context.Background())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for my info, can you link to the implementation of DryRun in the otel code

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,
}
}
5 changes: 5 additions & 0 deletions internal/commands/diagnose/otelconfigcheck.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- if .Passed }}
OTEL configuration is valid.
{{- else }}
⚠️ OTEL configuration validation failed with error {{ .Error }}
{{- end }}
Loading