Skip to content

Commit e974020

Browse files
authored
Merge pull request #8 from BICEP-Pump/feature/integration-BICEP
Feature/integration bicep
2 parents 6c2c77e + b086bb0 commit e974020

15 files changed

Lines changed: 791 additions & 153 deletions

.github/workflows/ghcr.yml

Lines changed: 105 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Checkout repository
2626
uses: actions/checkout@v4
2727
with:
28-
fetch-depth: 0 # Needed to push back to main
28+
fetch-depth: 0 # Needed to read branch history and push VERSION updates
2929

3030
- name: Lowercase IMAGE_NAME
3131
id: lowercase_image
@@ -34,43 +34,125 @@ jobs:
3434
3535
- name: Calculate New Version
3636
id: versioning
37+
shell: bash
3738
run: |
38-
CURRENT_VER=$(cat VERSION)
39-
BUMP_TYPE="patch"
40-
TAG_SUFFIX=""
41-
IS_RELEASE="false"
39+
set -euo pipefail
40+
41+
normalize_version() {
42+
local version="${1%%-*}"
43+
IFS='.' read -r -a parts <<< "$version"
44+
45+
while [[ "${#parts[@]}" -lt 3 ]]; do
46+
parts+=(0)
47+
done
48+
49+
printf '%s.%s.%s\n' "${parts[0]}" "${parts[1]}" "${parts[2]}"
50+
}
51+
52+
bump_version() {
53+
local current="$1"
54+
local part="$2"
55+
local normalized
56+
local major
57+
local minor
58+
local patch
59+
60+
normalized="$(normalize_version "$current")"
61+
IFS='.' read -r major minor patch <<< "$normalized"
62+
63+
case "$part" in
64+
major)
65+
((major += 1))
66+
minor=0
67+
patch=0
68+
;;
69+
minor)
70+
((minor += 1))
71+
patch=0
72+
;;
73+
patch)
74+
((patch += 1))
75+
;;
76+
*)
77+
echo "Unsupported bump type: $part" >&2
78+
exit 1
79+
;;
80+
esac
81+
82+
printf '%s.%s.%s\n' "$major" "$minor" "$patch"
83+
}
84+
85+
read_version() {
86+
tr -d '[:space:]' < "$1"
87+
}
88+
89+
read_version_from_ref() {
90+
local ref="$1"
91+
git show "${ref}:VERSION" | tr -d '[:space:]'
92+
}
93+
94+
CURRENT_VER="$(read_version VERSION)"
95+
NEW_VER="$CURRENT_VER"
96+
DOCKER_TAG="$CURRENT_VER"
4297
4398
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
4499
SOURCE_BRANCH="${{ github.event.pull_request.head.ref }}"
45-
if [[ "$SOURCE_BRANCH" == release/* ]]; then
46-
BUMP_TYPE="major"
47-
elif [[ "$SOURCE_BRANCH" == feature/* ]]; then
48-
BUMP_TYPE="minor"
100+
SOURCE_SHA="${{ github.event.pull_request.head.sha }}"
101+
BUMP_TYPE="patch"
102+
103+
if [[ "$SOURCE_BRANCH" == "dev" ]]; then
104+
if git cat-file -e "${SOURCE_SHA}:VERSION" 2>/dev/null; then
105+
NEW_VER="$(read_version_from_ref "$SOURCE_SHA")"
106+
fi
107+
else
108+
if [[ "$SOURCE_BRANCH" == release/* ]]; then
109+
BUMP_TYPE="major"
110+
elif [[ "$SOURCE_BRANCH" == feature/* ]]; then
111+
BUMP_TYPE="minor"
112+
fi
113+
114+
NEW_VER="$(bump_version "$CURRENT_VER" "$BUMP_TYPE")"
49115
fi
50-
IS_RELEASE="true"
116+
117+
DOCKER_TAG="$NEW_VER"
51118
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
52-
# For dev branch, we might not always want to bump the file in the same way,
53-
# but user wants it to work similarly.
54-
TAG_SUFFIX="-dev"
55-
# We bump patch for dev by default
56-
BUMP_TYPE="patch"
119+
NEW_VER="$(bump_version "$CURRENT_VER" "patch")"
120+
DOCKER_TAG="${NEW_VER}-dev"
121+
else
122+
NEW_VER="$(bump_version "$CURRENT_VER" "patch")"
123+
DOCKER_TAG="$NEW_VER"
57124
fi
58125
59-
NEW_VER=$(python3 scripts/bump_version.py "$CURRENT_VER" "$BUMP_TYPE")
60-
echo "NEW_VERSION=${NEW_VER}" >> $GITHUB_OUTPUT
61-
echo "DOCKER_TAG=${NEW_VER}${TAG_SUFFIX}" >> $GITHUB_OUTPUT
62-
echo "IS_RELEASE=${IS_RELEASE}" >> $GITHUB_OUTPUT
126+
echo "NEW_VERSION=${NEW_VER}" >> "$GITHUB_OUTPUT"
127+
echo "DOCKER_TAG=${DOCKER_TAG}" >> "$GITHUB_OUTPUT"
63128
64-
- name: Update VERSION file
65-
# Only update the repository file for merges to main
66-
if: github.event_name == 'pull_request' && github.event.pull_request.merged == true
129+
- name: Apply VERSION file
130+
shell: bash
67131
run: |
68132
echo "${{ steps.versioning.outputs.NEW_VERSION }}" > VERSION
133+
134+
- name: Commit VERSION file
135+
if: |
136+
(github.event_name == 'push' && github.ref_name == 'dev' && github.actor != 'github-actions[bot]') ||
137+
(github.event_name == 'pull_request' && github.event.pull_request.merged == true)
138+
shell: bash
139+
run: |
140+
set -euo pipefail
141+
142+
if git diff --quiet -- VERSION; then
143+
exit 0
144+
fi
145+
69146
git config --local user.email "action@github.com"
70147
git config --local user.name "GitHub Action"
71148
git add VERSION
72149
git commit -m "Bump version to ${{ steps.versioning.outputs.NEW_VERSION }} [skip ci]"
73-
git push origin main
150+
151+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
152+
git push origin HEAD:main
153+
else
154+
git push origin HEAD:dev
155+
fi
74156
75157
- name: Log in to the Container registry
76158
uses: docker/login-action@v2

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ COPY --from=builder /app/build/bicep-metric-service .
2828
ENV METRIC_ENDPOINT=""
2929
ENV REGISTRATION_ENDPOINT=""
3030
ENV SCRAPE_INTERVAL=10
31-
ENV BATCH_SIZE=10
31+
ENV METRIC_EXPORT_MODE="core"
3232
ENV SERVICE_NAME="bicep-metric-service"
3333
ENV SERVICE_PORT=8080
3434
ENV SERVICE_IP="127.0.0.1"

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ High-performance C++ service to monitor containerized environments via cgroups v
1111
- **Direct Scraper**: Parses `/sys/fs/cgroup` files for precision CPU (`usage_usec`) and RAM (`memory.current`) metrics.
1212
- **Docker Mapping**: Resolves container IDs to names using the Docker Engine API.
1313
- **Self-Discovered IP**: Automatically detects its host/container IP for registration if not provided.
14-
- **Batched Exporter**: Efficiently buffers and pushes JSON metrics to a configurable endpoint.
14+
- **Flexible Exporter**: Can push JSON to a core API or Prometheus text payloads directly to a Pushgateway-compatible endpoint.
1515
- **Internal Healthcheck**: Simple HTTP `/health` server for orchestration.
1616

1717
## Configuration
@@ -20,19 +20,21 @@ All settings are managed via environment variables.
2020
| Variable | Description | Default |
2121
|----------|-------------|---------|
2222
| `METRIC_ENDPOINT` | HTTP URL for metrics submission | (required) |
23-
| `REGISTRATION_ENDPOINT` | HTTP URL for service discovery | (optional) |
24-
| `SCRAPE_INTERVAL` | Seconds between scrapes | `10` |
25-
| `BATCH_SIZE` | Metric count before pushing | `10` |
23+
| `METRIC_EXPORT_MODE` | `core` for JSON-to-backend, `prometheus` for Pushgateway text export | `core` |
24+
| `REGISTRATION_ENDPOINT` | HTTP URL for service discovery, for example `http://core:8000/metric-services/register` or `http://core:8000/metric-services/register/1` | (optional) |
25+
| `SCRAPE_INTERVAL` | Seconds between scrapes; each scrape is pushed immediately | `10` |
2626
| `SERVICE_NAME` | Name reported to registry | `bicep-metric-service` |
2727
| `SERVICE_PORT` | Healthcheck port | `8080` |
2828
| `SERVICE_IP` | Service IP address | (auto-detected) |
2929

30+
When `METRIC_EXPORT_MODE=prometheus`, `METRIC_ENDPOINT` should point at a Pushgateway-compatible URL such as `http://host:9091/metrics/job/metric_service_host_1`.
31+
3032
## Development
3133
Build requirements: `CMake 3.15+`, `gcc/g++ (C++20)`, `libcurl`, `docker-compose`.
3234

3335
### Build & Test
3436
```bash
35-
mkdir build && cd build
37+
mkdir -p build && cd build
3638
cmake -DBUILD_TESTS=ON ..
3739
make -j$(nproc)
3840
./bicep_tests

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1
1+
0.0.3

docker-compose.test.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ services:
66
- /sys/fs/cgroup:/sys/fs/cgroup:ro
77
privileged: true
88
environment:
9+
- METRIC_EXPORT_MODE=core
910
- METRIC_ENDPOINT=http://mock-backend:5000/metrics
10-
- REGISTRATION_ENDPOINT=http://mock-backend:5000/register
11+
- REGISTRATION_ENDPOINT=http://mock-backend:5000/metric-services/register
1112
- SCRAPE_INTERVAL=2
12-
- BATCH_SIZE=2
1313
- SERVICE_NAME=integration-test-service
1414
- SERVICE_PORT=8080
1515
depends_on:

docker-compose.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
bicep-metric-service:
33
build: .
4-
image: ghcr.io/bicep-pump/bicep-metric-service:latest
4+
image: ghcr.io/bicep-pump/metric-service:latest
55
container_name: bicep-metric-service
66
volumes:
77
- /var/run/docker.sock:/var/run/docker.sock:ro
@@ -10,10 +10,10 @@ services:
1010
cap_add:
1111
- SYS_ADMIN # Alternative to privileged if possible
1212
environment:
13+
- METRIC_EXPORT_MODE=core
1314
- METRIC_ENDPOINT=http://your-backend-api/metrics
14-
- REGISTRATION_ENDPOINT=http://your-backend-api/register
15+
- REGISTRATION_ENDPOINT=http://your-backend-api/metric-services/register
1516
- SCRAPE_INTERVAL=5
16-
- BATCH_SIZE=10
1717
- SERVICE_NAME=bicep-metric-service
1818
- SERVICE_PORT=8080
1919
- SERVICE_IP=127.0.0.1 # Should be set to container IP or host reachable IP

scripts/bump_version.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)