diff --git a/.gitignore b/.gitignore index 045b395..8bb18b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -otel-plugin -otel \ No newline at end of file +otel +*.json +*.tar.gz \ No newline at end of file diff --git a/README.md b/README.md index 8e1eef6..4e97b08 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ This is a plugin that makes it easy to use OTel with Spin. Spin applications have the ability to export metrics and trace data. This plugin provides dashboards for viewing the data. +## Requirements + +This plugin relies on third-party software to work properly. Please be sure you have the following installed before continuing: + +- Latest version of [Docker](https://www.docker.com/products/docker-desktop) + # Installation The trigger is installed as a Spin plugin. It can be installed from a release or build. @@ -34,7 +40,7 @@ Alternatively, use the `spin pluginify` plugin to install from a fresh build. Th ```sh spin plugins install pluginify go build -o otel -spin pluginify install +spin pluginify --install ``` # Usage diff --git a/cmd/cleanup.go b/cmd/cleanup.go index ffbd372..ff02d53 100644 --- a/cmd/cleanup.go +++ b/cmd/cleanup.go @@ -37,6 +37,10 @@ func getIDs(dockerOutput []byte) []string { } func cleanUp() error { + if err := checkDocker(); err != nil { + return err + } + fmt.Println("Stopping Spin OTel Docker containers...") getContainers := exec.Command("docker", "ps") diff --git a/cmd/root.go b/cmd/root.go index 2430ad0..2c3b8f5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "os/exec" "path" open "github.com/fermyon/otel-plugin/cmd/open" @@ -28,7 +29,22 @@ 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 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 +} + +// checkDocker checks whether Docker is installed and the Docker daemon is running +func checkDocker() error { + cmd := exec.Command("docker", "--version") + if err := cmd.Run(); err != nil { + return fmt.Errorf("Docker appears not to be installed, so please visit their install page and try again once installed: https://www.docker.com/products/docker-desktop") + } + + cmd = exec.Command("docker", "info") + if err := cmd.Run(); err != nil { + return fmt.Errorf("The Docker daemon appears not to be running. The command to start Docker depends on your operating system. For instructions, check the correct page under https://docs.docker.com/engine/install") } return nil @@ -36,12 +52,11 @@ func setOtelConfigPath() error { func Execute() { if err := setOtelConfigPath(); err != nil { - fmt.Fprintln(os.Stderr, fmt.Errorf("error finding the otel-config directory: %w", err)) + 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 921b23a..7ba17d7 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -21,9 +21,13 @@ var setUpCmd = &cobra.Command{ } func setUp() error { + if err := checkDocker(); err != nil { + return err + } + 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") + 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") @@ -36,6 +40,6 @@ func setUp() error { return err } - fmt.Println("The Spin OTel resources are now running. Be sure to run the `spin otel cleanup` command when you are finished using them.") + fmt.Println("The Spin OTel resources are now running. Be sure to run the \"spin otel cleanup\" command when you are finished using them.") return nil } diff --git a/cmd/up.go b/cmd/up.go index 9cdddff..4eaa791 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -11,7 +11,7 @@ import ( var upCmd = &cobra.Command{ Use: "up", Short: "Runs a Spin App with the default OTel environment variables.", - Long: "Runs a Spin App with the default OTel environment variables. Any flags that work with the `spin up` command, will work with the `spin otel up` command: 'spin otel up -- --help'", + Long: "Runs a Spin App with the default OTel environment variables. Any flags that work with the \"spin up\" command, will work with the \"spin otel up\" command: \"spin otel up -- --help\"", RunE: func(cmd *cobra.Command, args []string) error { if err := up(args); err != nil { return err @@ -22,9 +22,13 @@ var upCmd = &cobra.Command{ } func up(args []string) error { + if err := checkDocker(); err != nil { + return err + } + pathToSpin := os.Getenv("SPIN_BIN_PATH") if pathToSpin == "" { - return fmt.Errorf("please ensure that you are running 'spin otel up', rather than calling the OTel plugin binary directly") + return fmt.Errorf("Please ensure that you are running \"spin otel up\", rather than calling the OTel plugin binary directly") } // Passing flags and args after the '--' diff --git a/main.go b/main.go index 5272129..68b333f 100644 --- a/main.go +++ b/main.go @@ -1,32 +1,9 @@ package main import ( - "fmt" - "os" - "os/exec" - "github.com/fermyon/otel-plugin/cmd" ) func main() { - if err := checkDependencies(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } - cmd.Execute() } - -func checkDependencies() error { - cmd := exec.Command("docker", "--version") - if err := cmd.Run(); err != nil { - return fmt.Errorf("docker appears not to be installed, so please visit their install page and try again once installed: https://www.docker.com/products/docker-desktop/") - } - - cmd = exec.Command("docker", "info") - if err := cmd.Run(); err != nil { - return fmt.Errorf("the docker daemon appears not to be running, so please start the daemon and try again") - } - - return nil -}