Skip to content

Add agent api example with prometheus and grafana #118

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 6 commits into from
Nov 28, 2022
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/
**/build/
bin/
whitesource/
.vscode/
Expand Down
44 changes: 44 additions & 0 deletions examples/grafana-metrics/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM ubuntu:22.04 as install
LABEL maintainer="NGINX Agent Maintainers <[email protected]>"

WORKDIR /agent
COPY ./build/nginx-agent.deb /agent/nginx-agent.deb
COPY ./entrypoint.sh /agent/entrypoint.sh
COPY ./nginx-agent.conf /agent/nginx-agent.conf
COPY ./nginx.conf /agent/nginx.conf

RUN set -x \
&& addgroup --system --gid 101 nginx \
&& adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false --uid 101 nginx \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
ca-certificates \
gnupg1 \
lsb-release \
git \
wget \
make \
curl \
vim \
&& apt-get update \
&& apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev \
&& wget http://nginx.org/download/nginx-1.23.2.tar.gz \
&& tar xfz nginx-1.23.2.tar.gz \
&& cd nginx-1.23.2 \
&& cp /agent/nginx.conf ./conf/nginx.conf \
&& ./configure --with-http_stub_status_module \
&& make \
&& make install \
&& apt-get install -y -f /agent/nginx-agent.deb

# run the nginx and agent
FROM install as runtime

COPY --from=install /agent/entrypoint.sh /agent/entrypoint.sh
COPY --from=install /agent/nginx-agent.conf /etc/nginx-agent/nginx-agent.conf

RUN chmod +x /agent/entrypoint.sh
STOPSIGNAL SIGTERM
EXPOSE 80 443

ENTRYPOINT ["/agent/entrypoint.sh"]
15 changes: 15 additions & 0 deletions examples/grafana-metrics/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

help: ## Show help message
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-24s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

clean: ## Remove docker containers and agent package
docker-compose down
rm -rf ./build

build: ## Build agent package
mkdir ./build
cd ../../ && GOWORK=off CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o ./build/nginx-agent
cd ../../ && nfpm pkg --config ./scripts/.local-nfpm.yaml --packager deb --target ./examples/grafana-metrics/build/nginx-agent.deb

run: ## Start docker containers
docker-compose up --build
12 changes: 12 additions & 0 deletions examples/grafana-metrics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Grafana Metrics Example
This example demonstrates how the NGINX agent can be used to report metrics using prometheus and grafana servers.

## Run Example

```
make clean build run
```

## Example of Grafana Dashboard

![Dashboard](grafana-dashboard-example.png)
45 changes: 45 additions & 0 deletions examples/grafana-metrics/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.9'

networks:
monitoring:
driver: bridge

volumes:
prometheus_data: {}

services:
agent:
build: ./
image: nginx/agent-example
container_name: agent
ports:
- 9091:9091
networks:
- monitoring
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: unless-stopped
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.enable-lifecycle'
ports:
- 9090:9090
networks:
- monitoring
grafana:
image: grafana/grafana-oss:latest
volumes:
- ./grafana-datasources.yml:/etc/grafana/provisioning/datasources/grafana-datasources.yml
- ./grafana-dashboards.yml:/etc/grafana/provisioning/dashboards/grafana-dashboards.yml
- ./nginx-agent-dashboard.json:/var/lib/grafana/dashboards/nginx-agent-dashboard.json
ports:
- 3000:3000
networks:
- monitoring
49 changes: 49 additions & 0 deletions examples/grafana-metrics/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

set -e
set -x
set -euxo pipefail

handle_term()
{
echo "received TERM signal"
echo "stopping nginx-agent ..."
kill -TERM "${agent_pid}" 2>/dev/null
echo "stopping nginx ..."
kill -TERM "${nginx_pid}" 2>/dev/null
}

trap 'handle_term' TERM

# Launch nginx
echo "starting nginx ..."
/usr/local/nginx/sbin/nginx -g "daemon off;" &

nginx_pid=$!

cp /agent/nginx-agent.conf /etc/nginx-agent/nginx-agent.conf
cat /etc/nginx-agent/nginx-agent.conf

# start nginx-agent, pass args
echo "starting nginx-agent ..."
nginx-agent "$@" &

agent_pid=$!

if [ $? != 0 ]; then
echo "couldn't start the agent, please check the log file"
exit 1
fi

wait_term()
{
wait ${agent_pid}
trap - TERM
kill -QUIT "${nginx_pid}" 2>/dev/null
echo "waiting for nginx to stop..."
wait ${nginx_pid}
}

wait_term

echo "nginx-agent process has stopped, exiting."
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/grafana-metrics/grafana-dashboards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: 1

providers:
- name: 'dashboards'
type: file
disableDeletion: false
editable: true
allowUiUpdates: true
updateIntervalSeconds: 10
folder: "NGINX"
options:
path: /var/lib/grafana/dashboards
foldersFromFilesStructure: false
20 changes: 20 additions & 0 deletions examples/grafana-metrics/grafana-datasources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: 1

deleteDatasources:
- name: Prometheus
orgId: 1

datasources:
- name: Prometheus
type: prometheus
typeName: Prometheus
typeLogoUrl: public/app/plugins/datasource/prometheus/img/prometheus_logo.svg
access: proxy
url: http://prometheus:9090
user: ""
database: ""
basicAuth: false
isDefault: true
jsonData:
httpMethod: POST
readOnly: false
Loading