From 970c2166dab3ecb32bfe36c38c9b4501f9b24e70 Mon Sep 17 00:00:00 2001 From: Andrew Steurer Date: Wed, 14 Aug 2024 13:47:23 -0600 Subject: [PATCH 1/3] fixing otel-config path bug Signed-off-by: Andrew Steurer --- cmd/cleanup.go | 2 +- cmd/root.go | 20 +++++++++++++++++++- cmd/setup.go | 2 +- spin-pluginify.toml | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/cleanup.go b/cmd/cleanup.go index b83d8bc..ffbd372 100644 --- a/cmd/cleanup.go +++ b/cmd/cleanup.go @@ -25,7 +25,7 @@ func getIDs(dockerOutput []byte) []string { for _, entry := range outputArray { // Each container in the Docker Compose stack will have the name of the directory - if strings.Contains(entry, otelConfigDir) { + if strings.Contains(entry, otelConfigDirName) { fields := strings.Fields(entry) if len(fields) > 0 { result = append(result, fields[0]) diff --git a/cmd/root.go b/cmd/root.go index 605a226..04e7b6a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "path" open "github.com/fermyon/otel-plugin/cmd/open" "github.com/spf13/cobra" @@ -14,9 +15,26 @@ var rootCmd = &cobra.Command{ Long: "A plugin that makes using Spin with OTel easy by automatically standing up dependencies, sourcing environment variables, and linking to dashboards.", } -var otelConfigDir = "otel-config" +var otelConfigDirName = "otel-config" +var otelConfigPath string + +// setOtelConfigPath allows for someone to run the otel plugin directly from source or via the Spin plugin installation +func setOtelConfigPath() error { + executablePath, err := os.Executable() + if err != nil { + return err + } + + otelConfigPath = path.Join(path.Dir(executablePath), otelConfigDirName) + + return nil +} func Execute() { + if err := setOtelConfigPath(); err != nil { + fmt.Fprintln(os.Stderr, fmt.Errorf("error finding the otel-config directory: %w", err)) + os.Exit(1) + } if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) diff --git a/cmd/setup.go b/cmd/setup.go index 04e77e5..7b22635 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -20,7 +20,7 @@ var setUpCmd = &cobra.Command{ } func setUp() error { - cmd := exec.Command("docker", "compose", "-f", path.Join(otelConfigDir, "compose.yaml"), "up", "-d") + cmd := exec.Command("docker", "compose", "-f", path.Join(otelConfigPath, "compose.yaml"), "up", "-d") fmt.Println("Pulling and running Spin OTel resources...") diff --git a/spin-pluginify.toml b/spin-pluginify.toml index f0b6622..fd245c9 100644 --- a/spin-pluginify.toml +++ b/spin-pluginify.toml @@ -1,5 +1,5 @@ name = "otel" -version = "0.1.0" +version = "0.1.1" spin_compatibility = ">=2.5" license = "Apache-2.0" package = "otel" From e0e5e892c1d185b66c40a328659109e5e4a2d580 Mon Sep 17 00:00:00 2001 From: Andrew Steurer Date: Wed, 14 Aug 2024 13:49:14 -0600 Subject: [PATCH 2/3] adjusting whitespace Signed-off-by: Andrew Steurer --- cmd/root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/root.go b/cmd/root.go index 04e7b6a..99aa7b9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,6 +35,7 @@ func Execute() { fmt.Fprintln(os.Stderr, fmt.Errorf("error finding the otel-config directory: %w", err)) os.Exit(1) } + if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) From 29697f4fac81a8a859858541b68d6d7cbbafd264 Mon Sep 17 00:00:00 2001 From: Andrew Steurer Date: Wed, 14 Aug 2024 15:41:45 -0600 Subject: [PATCH 3/3] improving error handling Signed-off-by: Andrew Steurer --- cmd/root.go | 4 ++++ cmd/setup.go | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 99aa7b9..2430ad0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,6 +27,10 @@ func setOtelConfigPath() error { otelConfigPath = path.Join(path.Dir(executablePath), otelConfigDirName) + if _, err := os.Stat(otelConfigPath); os.IsNotExist(err) { + return fmt.Errorf("the directory in which the plugin binary is executed is missing necessary files, so please make sure the plugin was installed using \"spin plugins install otel\"") + } + return nil } diff --git a/cmd/setup.go b/cmd/setup.go index 7b22635..921b23a 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "os/exec" "path" @@ -20,7 +21,12 @@ var setUpCmd = &cobra.Command{ } func setUp() error { - cmd := exec.Command("docker", "compose", "-f", path.Join(otelConfigPath, "compose.yaml"), "up", "-d") + composeFile := path.Join(otelConfigPath, "compose.yaml") + if _, err := os.Stat(composeFile); os.IsNotExist(err) { + return fmt.Errorf("the otel-config directory is missing the \"compose.yaml\" file, so please consider removing and re-installing the otel plugin") + } + + cmd := exec.Command("docker", "compose", "-f", composeFile, "up", "-d") fmt.Println("Pulling and running Spin OTel resources...")