Skip to content

Commit c945bed

Browse files
authored
Visualize NAP logs in mock collector grafana (#939)
1 parent 20ee486 commit c945bed

File tree

14 files changed

+321
-28
lines changed

14 files changed

+321
-28
lines changed

Makefile

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ OLD_BENCHMARK_RESULTS_FILE ?= $(TEST_BUILD_DIR)/benchmark.txt
6969
uname_m := $(shell uname -m)
7070

7171
ifeq ($(uname_m),aarch64)
72-
OSARCH = arm64
72+
OSARCH ?= arm64
7373
else
7474
ifeq ($(uname_m),x86_64)
75-
OSARCH = amd64
75+
OSARCH ?= amd64
7676
else
77-
OSARCH = $(uname_m)
77+
OSARCH ?= $(uname_m)
7878
endif
7979
endif
8080

@@ -150,9 +150,9 @@ build-mock-management-plane-grpc:
150150
mkdir -p $(BUILD_DIR)/mock-management-plane-grpc
151151
@CGO_ENABLED=0 GOARCH=$(OSARCH) GOOS=linux $(GOBUILD) -o $(BUILD_DIR)/mock-management-plane-grpc/server test/mock/grpc/cmd/main.go
152152

153-
build-mock-management-plane-collector:
154-
mkdir -p $(BUILD_DIR)/mock-management-plane-collector
155-
@CGO_ENABLED=0 GOARCH=$(OSARCH) GOOS=linux $(GOBUILD) -o $(BUILD_DIR)/mock-management-plane-collector/collector test/mock/collector/mock-collector/main.go
153+
build-mock-management-otel-collector:
154+
mkdir -p $(BUILD_DIR)/mock-management-otel-collector
155+
@CGO_ENABLED=0 GOARCH=$(OSARCH) GOOS=linux $(GOBUILD) -o $(BUILD_DIR)/mock-management-otel-collector/collector test/mock/collector/mock-collector/main.go
156156

157157
integration-test: $(SELECTED_PACKAGE) build-mock-management-plane-grpc
158158
TEST_ENV="Container" CONTAINER_OS_TYPE=$(CONTAINER_OS_TYPE) BUILD_TARGET="install-agent-local" CONTAINER_NGINX_IMAGE_REGISTRY=${CONTAINER_NGINX_IMAGE_REGISTRY} \
@@ -190,6 +190,18 @@ run-mock-management-grpc-server: ## Run mock management plane gRPC server
190190
@echo "🖲️ Running mock management plane gRPC server"
191191
$(GORUN) test/mock/grpc/cmd/main.go -configDirectory=$(MOCK_MANAGEMENT_PLANE_CONFIG_DIRECTORY) -logLevel=$(MOCK_MANAGEMENT_PLANE_LOG_LEVEL) -grpcAddress=$(MOCK_MANAGEMENT_PLANE_GRPC_ADDRESS) -apiAddress=$(MOCK_MANAGEMENT_PLANE_API_ADDRESS)
192192

193+
194+
.PHONY: build-test-nginx-plus-and-nap-image
195+
build-test-nginx-plus-and-nap-image:
196+
$(CONTAINER_BUILDENV) $(CONTAINER_CLITOOL) build -t nginx_plus_and_nap_$(IMAGE_TAG) . \
197+
--no-cache -f ./test/docker/nginx-plus-and-nap/deb/Dockerfile \
198+
--secret id=nginx-crt,src=$(CERTS_DIR)/nginx-repo.crt \
199+
--secret id=nginx-key,src=$(CERTS_DIR)/nginx-repo.key \
200+
--build-arg PACKAGE_NAME=$(PACKAGE_NAME) \
201+
--build-arg PACKAGES_REPO=$(OSS_PACKAGES_REPO) \
202+
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
203+
--build-arg ENTRY_POINT=./test/docker/entrypoint.sh
204+
193205
.PHONY: build-test-plus-image
194206
build-test-plus-image:
195207
$(CONTAINER_BUILDENV) $(CONTAINER_CLITOOL) build -t nginx_plus_$(IMAGE_TAG) . \
@@ -211,20 +223,20 @@ build-test-oss-image:
211223
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
212224
--build-arg ENTRY_POINT=./test/docker/entrypoint.sh
213225

214-
.PHONY: build-mock-collector-image
215-
build-mock-collector-image: build-mock-management-plane-collector
226+
.PHONY: build-mock-management-otel-collector-image
227+
build-mock-management-otel-collector-image: build-mock-management-otel-collector
216228
$(CONTAINER_BUILDENV) $(CONTAINER_CLITOOL) build -t mock-collector . \
217229
--no-cache -f ./test/mock/collector/mock-collector/Dockerfile
218230

219231
.PHONY: run-mock-management-otel-collector
220232
run-mock-management-otel-collector: ## Run mock management plane OTel collector
221233
@echo "🚀 Running mock management plane OTel collector"
222-
AGENT_IMAGE_WITH_NGINX_PLUS=nginx_plus_$(IMAGE_TAG):latest AGENT_IMAGE_WITH_NGINX_OSS=nginx_oss_$(IMAGE_TAG):latest $(CONTAINER_COMPOSE) -f ./test/mock/collector/docker-compose.yaml up -d
234+
AGENT_IMAGE_WITH_NGINX_PLUS=nginx_plus_$(IMAGE_TAG):latest AGENT_IMAGE_WITH_NGINX_OSS=nginx_oss_$(IMAGE_TAG):latest AGENT_IMAGE_WITH_NGINX_PLUS_AND_NAP=nginx_plus_and_nap_$(IMAGE_TAG):latest $(CONTAINER_COMPOSE) -f ./test/mock/collector/docker-compose.yaml up -d
223235

224236
.PHONY: stop-mock-management-otel-collector
225237
stop-mock-management-otel-collector: ## Stop running mock management plane OTel collector
226238
@echo "Stopping mock management plane OTel collector"
227-
AGENT_IMAGE_WITH_NGINX_PLUS=nginx_plus_$(IMAGE_TAG):latest AGENT_IMAGE_WITH_NGINX_OSS=nginx_oss_$(IMAGE_TAG):latest $(CONTAINER_COMPOSE) -f ./test/mock/collector/docker-compose.yaml down
239+
AGENT_IMAGE_WITH_NGINX_PLUS=nginx_plus_$(IMAGE_TAG):latest AGENT_IMAGE_WITH_NGINX_OSS=nginx_oss_$(IMAGE_TAG):latest AGENT_IMAGE_WITH_NGINX_PLUS_AND_NAP=nginx_plus_and_nap_$(IMAGE_TAG):latest $(CONTAINER_COMPOSE) -f ./test/mock/collector/docker-compose.yaml down
228240

229241
generate: ## Generate golang code
230242
@echo "🗄️ Generating proto files"

test/docker/entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ trap 'handle_term' TERM
1515

1616
# Launch nginx
1717
echo "starting nginx ..."
18+
/bin/su -s /bin/sh -c "/usr/share/ts/bin/bd-socket-plugin tmm_count 4 proc_cpuinfo_cpu_mhz 2000000 total_xml_memory 307200000 total_umu_max_size 3129344 sys_max_account_id 1024 no_static_config 2>&1 >> /var/log/app_protect/bd-socket-plugin.log &" nginx
1819
/usr/sbin/nginx -g "daemon off;" &
1920

2021
nginx_pid=$!
2122

2223
SECONDS=0
2324

2425
while ! ps -ef | grep "nginx: master process" | grep -v grep; do
25-
if (( SECONDS > 5 )); then
26+
if (( SECONDS > 30 )); then
2627
echo "couldn't find nginx master process"
2728
exit 1
2829
fi
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
ARG BASE_IMAGE
2+
FROM --platform=linux/amd64 ${BASE_IMAGE} as install-nginx
3+
LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
4+
5+
ARG DEBIAN_FRONTEND=noninteractive
6+
ARG ENTRY_POINT
7+
ARG PACKAGE_NAME
8+
ARG PACKAGES_REPO
9+
10+
WORKDIR /agent
11+
COPY ./build /agent/build
12+
COPY $ENTRY_POINT /agent/entrypoint.sh
13+
14+
ENV PLUS_VERSION=R32
15+
16+
RUN --mount=type=secret,id=nginx-crt,dst=nginx-repo.crt \
17+
--mount=type=secret,id=nginx-key,dst=nginx-repo.key \
18+
set -x \
19+
# Create nginx user/group first, to be consistent throughout Docker variants
20+
&& groupadd --system --gid 101 nginx \
21+
&& useradd --system --gid nginx --no-create-home --home-dir /nonexistent --uid 101 nginx \
22+
&& apt-get update --allow-releaseinfo-change \
23+
&& apt-get install --no-install-recommends --no-install-suggests -y \
24+
ca-certificates \
25+
gnupg1 \
26+
lsb-release \
27+
apt-transport-https \
28+
git \
29+
wget \
30+
make \
31+
gnupg2 \
32+
ubuntu-keyring \
33+
&& wget -qO - https://cs.nginx.com/static/keys/app-protect-security-updates.key | gpg --dearmor | tee /usr/share/keyrings/app-protect-security-updates.gpg >/dev/null \
34+
&& wget -qO - https://cs.nginx.com/static/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null \
35+
&& \
36+
NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
37+
found=''; \
38+
for server in \
39+
hkp://keyserver.ubuntu.com:80 \
40+
pgp.mit.edu \
41+
; do \
42+
echo "Fetching GPG key $NGINX_GPGKEY from $server"; \
43+
apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
44+
done; \
45+
test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
46+
apt-get remove --purge --auto-remove -y gnupg1 && rm -rf /var/lib/apt/lists/* \
47+
# Install the latest release of NGINX Plus and/or NGINX Plus modules
48+
# Uncomment individual modules if necessary
49+
# Use versioned packages over defaults to specify a release
50+
&& nginxPackages=" \
51+
app-protect \
52+
" \
53+
&& echo "Acquire::https::pkgs.nginx.com::Verify-Peer \"true\";" > /etc/apt/apt.conf.d/90nginx \
54+
&& echo "Acquire::https::pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \
55+
&& echo "Acquire::https::pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \
56+
&& echo "Acquire::https::pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \
57+
&& printf "deb https://pkgs.nginx.com/plus/${PLUS_VERSION}/ubuntu/ `lsb_release -cs` nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \
58+
&& printf "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://pkgs.nginx.com/app-protect/${PLUS_VERSION}/ubuntu `lsb_release -cs` nginx-plus\n" | tee /etc/apt/sources.list.d/nginx-app-protect.list \
59+
&& printf "deb [signed-by=/usr/share/keyrings/app-protect-security-updates.gpg] https://pkgs.nginx.com/app-protect-security-updates/ubuntu `lsb_release -cs` nginx-plus\n" | tee -a /etc/apt/sources.list.d/nginx-app-protect.list \
60+
&& mkdir -p /etc/ssl/nginx \
61+
&& cat nginx-repo.crt > /etc/ssl/nginx/nginx-repo.crt \
62+
&& cat nginx-repo.key > /etc/ssl/nginx/nginx-repo.key \
63+
&& apt-get update \
64+
&& apt-get install --no-install-recommends --no-install-suggests -y \
65+
$nginxPackages \
66+
curl \
67+
gettext-base \
68+
jq \
69+
&& apt-get remove --purge -y lsb-release \
70+
&& apt-get remove --purge --auto-remove -y && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/nginx-plus.list /etc/apt/sources.list.d/nginx-app-protect.list \
71+
&& rm -rf /etc/apt/apt.conf.d/90nginx /etc/ssl/nginx
72+
73+
EXPOSE 80
74+
75+
STOPSIGNAL SIGQUIT
76+
77+
RUN chmod +x /agent/entrypoint.sh
78+
RUN apt install -y /agent/build/${PACKAGE_NAME}.deb
79+
80+
STOPSIGNAL SIGTERM
81+
82+
EXPOSE 80 443
83+
84+
ENTRYPOINT ["/agent/entrypoint.sh"]

test/mock/collector/README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
# Mock Management OTel Collector
22

3-
There are 3 images that need to be built in order to use the mock management OTel collector
3+
There are 4 images that need to be built in order to use the mock management OTel collector
44
* Agent with NGINX Plus image
5+
* Agent with NGINX Plus & NGINX App Protect image
56
* Agent with NGINX OSS image
67
* Custom OTel collector image
78

89
To build these images run the following
910
```
10-
make local-deb-package build-test-plus-image build-test-oss-image build-mock-collector-image
11+
OSARCH=amd64 make local-deb-package build-test-nginx-plus-and-nap-image
12+
make local-deb-package build-test-oss-image build-test-plus-image build-mock-management-otel-collector-image
1113
```
1214

15+
[**Note:** We need to build the test NGINX Plus with NAP image with the environment variable `OSARCH=amd64` since NGINX App Protect doesn't support ARM yet.]
16+
1317
To start run everything run the following
1418
```
1519
make run-mock-management-otel-collector
1620
```
1721

18-
Once everything is started there should be 5 containers running
19-
```
20-
8e6df6d0bc73 localhost/nginx_plus_agent_ubuntu_22.04:latest 4 minutes ago Up 4 minutes 80/tcp, 443/tcp mock-collector-agent-with-nginx-plus
21-
a65a7efaf2b3 localhost/nginx_oss_agent_ubuntu_22.04:latest 4 minutes ago Up 4 minutes 80/tcp, 443/tcp mock-collector-agent-with-nginx-oss
22-
bf0f247991c0 localhost/mock-collector:latest go run main.go 4 minutes ago Up 4 minutes 0.0.0.0:4320->4317/tcp, 0.0.0.0:9775->9090/tcp mock-collector-otel-collector
23-
67bb7bde6392 docker.io/prom/prometheus:latest --config.file=/et... 4 minutes ago Up 4 minutes 0.0.0.0:9090->9090/tcp, 9090/tcp mock-collector-prometheus
24-
a83a997eb652 docker.io/grafana/grafana:latest 4 minutes ago Up 4 minutes 0.0.0.0:3002->3000/tcp, 3000/tcp mock-collector-grafana
22+
Once everything is started there should be 7 containers running
23+
```
24+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25+
e0e57897223d localhost/nginx_plus_and_nap_agent_ubuntu_22.04:latest 17 seconds ago Up 17 seconds 80/tcp, 443/tcp mock-collector-agent-with-nginx-plus-and-nap
26+
f4e7264ceb0b localhost/nginx_plus_agent_ubuntu_22.04:latest 15 seconds ago Up 16 seconds 80/tcp, 443/tcp mock-collector-agent-with-nginx-plus
27+
97e9e84679a2 localhost/nginx_oss_agent_ubuntu_22.04:latest 14 seconds ago Up 14 seconds 80/tcp, 443/tcp mock-collector-agent-with-nginx-oss
28+
04a3bce14c7a localhost/mock-collector:latest /mock-management-... 12 seconds ago Up 13 seconds 0.0.0.0:4320->4317/tcp, 0.0.0.0:9775->9090/tcp mock-collector-otel-collector
29+
8efe150cbd96 docker.io/prom/prometheus:latest --config.file=/et... 11 seconds ago Up 11 seconds 0.0.0.0:9090->9090/tcp, 9090/tcp mock-collector-prometheus
30+
e55572131d2a docker.io/grafana/grafana:latest 9 seconds ago Up 10 seconds 0.0.0.0:3002->3000/tcp, 3000/tcp mock-collector-grafana
31+
69c8efab591b docker.io/grafana/loki:latest -config.file=/etc... 8 seconds ago Up 8 seconds 0.0.0.0:3100->3100/tcp, 3100/tcp mock-collector-loki
32+
2533
```
2634

27-
To view the metrics, the grafana UI can be used by accessing this URL http://localhost:3002/login (Note: username/password is admin/admin)
35+
To view the metrics & logs, the grafana UI can be used by accessing this URL http://localhost:3002/login (Note: username/password is admin/admin)
2836

2937
To stop everything run the following
3038
```

test/mock/collector/docker-compose.yaml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@ volumes:
77
grafana-storage:
88

99
services:
10+
agent-with-nginx-plus-and-nap:
11+
image: ${AGENT_IMAGE_WITH_NGINX_PLUS_AND_NAP}
12+
container_name: mock-collector-agent-with-nginx-plus-and-nap
13+
volumes:
14+
- ./nginx-agent.conf:/etc/nginx-agent/nginx-agent.conf
15+
- ./nginx-plus-and-nap/nginx.conf:/etc/nginx/nginx.conf
16+
- ./nginx-plus-and-nap/conf.d/default.conf:/etc/nginx/conf.d/default.conf
17+
networks:
18+
- metrics
19+
1020
agent-with-nginx-plus:
1121
image: ${AGENT_IMAGE_WITH_NGINX_PLUS}
1222
container_name: mock-collector-agent-with-nginx-plus
1323
volumes:
1424
- ./nginx-agent.conf:/etc/nginx-agent/nginx-agent.conf
15-
- ./nginx-plus:/etc/nginx/
25+
- ./nginx-plus/nginx.conf:/etc/nginx/nginx.conf
26+
- ./nginx-plus/conf.d/default.conf:/etc/nginx/conf.d/default.conf
1627
networks:
1728
- metrics
1829

@@ -53,12 +64,27 @@ services:
5364
image: grafana/grafana:latest
5465
container_name: mock-collector-grafana
5566
restart: unless-stopped
67+
environment:
68+
GF_INSTALL_PLUGINS: "grafana-lokiexplore-app"
5669
ports:
5770
- "3002:3000"
5871
volumes:
5972
- grafana-storage:/var/lib/grafana
6073
- ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
6174
- ./grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards
75+
- ./grafana/provisioning/plugins:/etc/grafana/provisioning/plugins
6276
- ./grafana/provisioning/dashboards:/var/lib/grafana/dashboards
6377
networks:
6478
- metrics
79+
80+
loki:
81+
image: grafana/loki:latest
82+
container_name: mock-collector-loki
83+
restart: unless-stopped
84+
ports:
85+
- "3100:3100"
86+
volumes:
87+
- ./loki-config.yaml:/etc/loki/local-config.yaml
88+
command: -config.file=/etc/loki/local-config.yaml
89+
networks:
90+
- metrics

test/mock/collector/grafana/provisioning/datasources/datasource.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ datasources:
77
access: proxy
88
url: http://prometheus:9090
99
isDefault: true
10+
- name: Loki
11+
type: loki
12+
uid: otel-loki-scraper
13+
access: proxy
14+
url: http://loki:3100
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: 1
2+
3+
apps:
4+
- type: grafana-lokiexplore-app
5+
org_id: 1
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
auth_enabled: false
2+
3+
limits_config:
4+
allow_structured_metadata: true
5+
volume_enabled: true
6+
7+
server:
8+
http_listen_port: 3100
9+
10+
common:
11+
ring:
12+
instance_addr: 0.0.0.0
13+
kvstore:
14+
store: inmemory
15+
replication_factor: 1
16+
path_prefix: /tmp/loki
17+
18+
schema_config:
19+
configs:
20+
- from: 2020-05-15
21+
store: tsdb
22+
object_store: filesystem
23+
schema: v13
24+
index:
25+
prefix: index_
26+
period: 24h
27+
28+
storage_config:
29+
tsdb_shipper:
30+
active_index_directory: /tmp/loki/index
31+
cache_location: /tmp/loki/index_cache
32+
filesystem:
33+
directory: /tmp/loki/chunks
34+
35+
pattern_ingester:
36+
enabled: true
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM golang:bookworm
22

3-
WORKDIR /mock-management-plane-collector
4-
COPY ./build/mock-management-plane-collector ./
3+
WORKDIR /mock-management-otel-collector
4+
COPY ./build/mock-management-otel-collector ./
55

6-
CMD ["/mock-management-plane-collector/collector"]
6+
CMD ["/mock-management-otel-collector/collector"]

test/mock/collector/mock-collector/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616

1717
"github.com/nginx/agent/v3/test/mock/collector/mock-collector/auth"
1818
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusexporter"
19+
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor"
1920
"go.opentelemetry.io/collector/connector"
2021
"go.opentelemetry.io/collector/exporter"
2122
"go.opentelemetry.io/collector/exporter/debugexporter"
2223
"go.opentelemetry.io/collector/exporter/otlpexporter"
24+
"go.opentelemetry.io/collector/exporter/otlphttpexporter"
2325
"go.opentelemetry.io/collector/extension"
2426
"go.opentelemetry.io/collector/processor"
2527
"go.opentelemetry.io/collector/processor/batchprocessor"
@@ -85,13 +87,15 @@ func components() (otelcol.Factories, error) {
8587
debugexporter.NewFactory(),
8688
otlpexporter.NewFactory(),
8789
prometheusexporter.NewFactory(),
90+
otlphttpexporter.NewFactory(),
8891
)
8992
if err != nil {
9093
return otelcol.Factories{}, err
9194
}
9295

9396
factories.Processors, err = processor.MakeFactoryMap(
9497
batchprocessor.NewFactory(),
98+
resourceprocessor.NewFactory(),
9599
)
96100
if err != nil {
97101
return otelcol.Factories{}, err

0 commit comments

Comments
 (0)