diff --git a/.bra.toml b/.bra.toml index c6b4191..b5e0038 100644 --- a/.bra.toml +++ b/.bra.toml @@ -3,7 +3,7 @@ # see https://github.com/unknwon/bra/blob/master/templates/default.bra.toml for more configuration options. [run] init_cmds = [ - ["mage", "-v", "build:backend"], + ["mage", "-v", "build:debug"], ["mage", "-v" , "reloadPlugin"] ] watch_all = true @@ -17,6 +17,6 @@ watch_dirs = [ watch_exts = [".go", ".json"] build_delay = 2000 cmds = [ - ["mage", "-v", "build:backend"], + ["mage", "-v", "build:debug"], ["mage", "-v" , "reloadPlugin"] ] \ No newline at end of file diff --git a/.config/Dockerfile b/.config/Dockerfile index 2dc26fa..379ee78 100644 --- a/.config/Dockerfile +++ b/.config/Dockerfile @@ -7,7 +7,8 @@ ARG anonymous_auth_enabled=true ARG development=false ARG TARGETARCH - +ARG GO_VERSION=1.25.6 +ARG GO_ARCH=${TARGETARCH:-amd64} ENV DEV "${development}" # Make it as simple as possible to access the grafana instance for development purposes @@ -43,7 +44,27 @@ RUN if [ "${development}" = "true" ]; then \ COPY supervisord/supervisord.conf /etc/supervisor.d/supervisord.ini COPY supervisord/supervisord.conf /etc/supervisor/conf.d/supervisord.conf +# Installing Go +RUN if [ "${development}" = "true" ]; then \ + curl -O -L https://golang.org/dl/go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \ + rm -rf /usr/local/go && \ + tar -C /usr/local -xzf go${GO_VERSION}.linux-${GO_ARCH}.tar.gz && \ + echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bashrc && \ + rm -f go${GO_VERSION}.linux-${GO_ARCH}.tar.gz; \ + fi +# Installing delve for debugging +RUN if [ "${development}" = "true" ]; then \ + /usr/local/go/bin/go install github.com/go-delve/delve/cmd/dlv@latest; \ + fi + +# Installing mage for plugin (re)building +RUN if [ "${development}" = "true" ]; then \ + git clone https://github.com/magefile/mage; \ + cd mage; \ + export PATH=$PATH:/usr/local/go/bin; \ + go run bootstrap.go; \ + fi # Inject livereload script into grafana index.html RUN sed -i 's|||g' /usr/share/grafana/public/views/index.html diff --git a/.config/docker-compose-base.yaml b/.config/docker-compose-base.yaml index 6940a39..226ada4 100644 --- a/.config/docker-compose-base.yaml +++ b/.config/docker-compose-base.yaml @@ -7,11 +7,17 @@ services: context: . args: grafana_image: ${GRAFANA_IMAGE:-grafana-enterprise} - grafana_version: ${GRAFANA_VERSION:-12.2.0} + grafana_version: ${GRAFANA_VERSION:-12.3.1} development: ${DEVELOPMENT:-false} anonymous_auth_enabled: ${ANONYMOUS_AUTH_ENABLED:-true} ports: - 3000:3000/tcp + - 2345:2345/tcp # delve + security_opt: + - 'apparmor:unconfined' + - 'seccomp:unconfined' + cap_add: + - SYS_PTRACE volumes: - ../dist:/var/lib/grafana/plugins/alexanderzobnin-zabbix-app - ../provisioning:/etc/grafana/provisioning diff --git a/.config/supervisord/supervisord.conf b/.config/supervisord/supervisord.conf index 47624f0..48dfea3 100644 --- a/.config/supervisord/supervisord.conf +++ b/.config/supervisord/supervisord.conf @@ -5,7 +5,7 @@ user=root [program:grafana] user=root directory=/var/lib/grafana -command=/run.sh +command=bash -c 'while [ ! -f /root/alexanderzobnin-zabbix-app/dist/datasource/gpx_zabbix-datasource* ]; do sleep 1; done; /run.sh' stdout_logfile=/dev/fd/1 stdout_logfile_maxbytes=0 redirect_stderr=true @@ -13,3 +13,35 @@ killasgroup=true stopasgroup=true autostart=true +[program:delve] +user=root +command=/bin/bash -c 'pid=""; while [ -z "$pid" ]; do pid=$(pgrep -f gpx_zabbix-datasource); done; /root/go/bin/dlv attach --api-version=2 --headless --continue --accept-multiclient --listen=:2345 $pid' +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true +killasgroup=false +stopasgroup=false +autostart=true +autorestart=true + +[program:build-watcher] +user=root +command=/bin/bash -c 'while inotifywait -e modify,create,delete -r /var/lib/grafana/plugins/alexanderzobnin-zabbix-app; do echo "Change detected, restarting delve...";supervisorctl restart delve; done' +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true +killasgroup=true +stopasgroup=true +autostart=true + +[program:mage-watcher] +user=root +environment=PATH="/usr/local/go/bin:/root/go/bin:%(ENV_PATH)s" +directory=/root/alexanderzobnin-zabbix-app +command=/bin/bash -c 'git config --global --add safe.directory /root/alexanderzobnin-zabbix-app && mage -v watch' +stdout_logfile=/dev/fd/1 +stdout_logfile_maxbytes=0 +redirect_stderr=true +killasgroup=true +stopasgroup=true +autostart=true diff --git a/.gitignore b/.gitignore index 2233553..e8c57cc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ *.sublime-project .idea/ -.vscode *.bat .DS_Store diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..607b0f4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,36 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Standalone debug mode", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceFolder}/pkg", + "env": {}, + "args": [ + "-standalone" + ] + }, + { + "name": "Attach to plugin backend in docker", + "type": "go", + "request": "attach", + "mode": "remote", + "port": 2345, + "host": "127.0.0.1", + "showLog": true, + "trace": "log", + "logOutput": "rpc", + "substitutePath": [ + { + "from": "${workspaceFolder}", + "to": "/root/alexanderzobnin-zabbix-app" + } + ] + } + ] +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 26888b2..5548021 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,11 +4,11 @@ ```sh # install frontend deps -yarn install --pure-lockfile +yarn install # build frontend yarn build -#build backend for current platform -mage -v build:backend +#build backend +mage -v ``` ## Rebuild backend on changes @@ -19,31 +19,31 @@ mage watch ## Debugging backend plugin -For debugging backend part written on Go, you should go through a few steps. First, build a plugin with special flags for debugging: +The plugin supports two debugging modes for the Go backend: + +### Standalone Debug Mode + +This mode allows you to run and debug the plugin locally: + +1. Use the **"Standalone debug mode"** configuration in VS Code (already configured in `.vscode/launch.json`) +2. Set breakpoints in your Go code +3. The plugin will start in standalone mode with the `-standalone` flag +4. Run your local grafana instance + +### Debugging with Docker (Recommended for Grafana Integration) + +For debugging the backend plugin while it's running inside Grafana in Docker: + +1. Run the docker compose file with the following command in any of the devenv folders: ```sh -make build-debug +DEVELOPMENT=true docker compose up --build -d ``` -Then, configure your editor to connect to [delve](https://github.com/go-delve/delve) debugger running in headless mode. This is an example for VS Code: - -```json -{ - "version": "0.2.0", - "configurations": [ - { - "name": "Debug backend plugin", - "type": "go", - "request": "attach", - "mode": "remote", - "port": 3222, - "host": "127.0.0.1" - } - ] -} -``` - -Finally, run grafana-server and then execute `./debug-backend.sh` from grafana-zabbix root folder. This script will attach delve to running plugin. Now you can go to the VS Code and run _Debug backend plugin_ debug config. +1. Attach VS Code debugger: + - Use the **"Attach to plugin backend in docker"** configuration in VS Code (already configured in `.vscode/launch.json`) + - Set breakpoints in your Go code + - The debugger will connect to delve running in the Docker container ## Submitting PR diff --git a/cspell.config.json b/cspell.config.json index 3507d64..94349ca 100644 --- a/cspell.config.json +++ b/cspell.config.json @@ -29,7 +29,11 @@ "src/**", "pkg/**" ], - "ignoreRegExpList": ["import\\s*\\((.|[\r\n])*?\\)", "import\\s*.*\".*?\"", "\\[@.+\\]"], + "ignoreRegExpList": [ + "import\\s*\\((.|[\r\n])*?\\)", + "import\\s*.*\".*?\"", + "\\[@.+\\]" + ], "words": [ "alexanderzobnin", "applicationids", @@ -37,6 +41,7 @@ "datapoints", "datasource", "datasources", + "devenv", "dompurify", "eventid", "eventids", diff --git a/devenv/zabbix50/docker-compose.yml b/devenv/zabbix50/docker-compose.yml index 01c3b5a..9bb48ac 100644 --- a/devenv/zabbix50/docker-compose.yml +++ b/devenv/zabbix50/docker-compose.yml @@ -19,7 +19,6 @@ services: file: ../../.config/docker-compose-base.yaml service: grafana volumes: - - ../..:/grafana-zabbix - ../dashboards:/devenv/dashboards - '../datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml' - '../dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml' diff --git a/devenv/zabbix60/docker-compose.yml b/devenv/zabbix60/docker-compose.yml index b929a9a..bad6a9f 100644 --- a/devenv/zabbix60/docker-compose.yml +++ b/devenv/zabbix60/docker-compose.yml @@ -19,7 +19,6 @@ services: file: ../../.config/docker-compose-base.yaml service: grafana volumes: - - ../..:/grafana-zabbix - ../dashboards:/devenv/dashboards - '../datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml' - '../dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml' diff --git a/devenv/zabbix70/docker-compose.yml b/devenv/zabbix70/docker-compose.yml index f0e8593..bbd9d6b 100644 --- a/devenv/zabbix70/docker-compose.yml +++ b/devenv/zabbix70/docker-compose.yml @@ -19,7 +19,6 @@ services: file: ../../.config/docker-compose-base.yaml service: grafana volumes: - - ../..:/grafana-zabbix - ../dashboards:/devenv/dashboards - '../datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml' - '../dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml' diff --git a/devenv/zabbix72/docker-compose.yml b/devenv/zabbix72/docker-compose.yml index 7cf56a7..b9e0943 100644 --- a/devenv/zabbix72/docker-compose.yml +++ b/devenv/zabbix72/docker-compose.yml @@ -5,7 +5,6 @@ services: file: ../../.config/docker-compose-base.yaml service: grafana volumes: - - ../..:/grafana-zabbix - ../dashboards:/devenv/dashboards - '../datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml' - '../dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml' diff --git a/devenv/zabbix74/docker-compose.yml b/devenv/zabbix74/docker-compose.yml index 9c1a772..4572cb7 100644 --- a/devenv/zabbix74/docker-compose.yml +++ b/devenv/zabbix74/docker-compose.yml @@ -5,7 +5,6 @@ services: file: ../../.config/docker-compose-base.yaml service: grafana volumes: - - ../..:/grafana-zabbix - ../dashboards:/devenv/dashboards - '../datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml' - '../dashboards.yaml:/etc/grafana/provisioning/dashboards/dashboards.yaml'