Skip to content

Commit

Permalink
feat: Built a graalvm native image and separate x86, arm64 docker images
Browse files Browse the repository at this point in the history
Closes #222, Closes #176
  • Loading branch information
devatherock committed Jan 20, 2024
1 parent b69d74d commit 5383c94
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 39 deletions.
77 changes: 65 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ docker_auth: &docker_auth
username: $DOCKER_USERNAME
password: $DOCKER_PASSWORD

docker_executor_image: &docker_executor_image
docker:
- image: cimg/openjdk:17.0
<<: *docker_auth

vm_executor_image: &vm_executor_image
machine:
image: ubuntu-2204:2023.07.1

work_directory: &work_directory
working_directory: ~/kafka-lag-monitor

Expand Down Expand Up @@ -32,24 +41,33 @@ parameters:
executors:
jdk-executor:
docker:
- image: devatherock/graalvm:ol8-java17-22.1.0-6
- image: devatherock/graalvm:17-ol8
<<: *docker_auth
<<: *resource_class
<<: *work_directory
environment:
# To generate swagger UI
JAVA_TOOL_OPTIONS: '-Xmx1536m -Dmicronaut.openapi.views.spec=swagger-ui.enabled=true,swagger-ui.theme=flattop'
docker-executor:
docker:
- image: cimg/openjdk:17.0
<<: *docker_auth
<<: *docker_executor_image
<<: *resource_class
<<: *work_directory
native-image-executor:
<<: *docker_executor_image
resource_class: large
<<: *work_directory
native-image-executor-arm:
<<: *vm_executor_image
resource_class: arm.large
<<: *work_directory
machine-executor:
machine:
image: ubuntu-2204:2023.04.2
<<: *vm_executor_image
resource_class: medium
<<: *work_directory
arm-executor:
<<: *vm_executor_image
resource_class: arm.medium
<<: *work_directory

version: 2.1
jobs:
Expand All @@ -76,6 +94,7 @@ jobs:
executor: jdk-executor
steps:
- checkout
- setup_remote_docker
- attach_workspace:
at: ~/kafka-lag-monitor
- restore_cache:
Expand All @@ -99,7 +118,7 @@ jobs:
path: build/test-results

publish:
executor: docker-executor
executor: native-image-executor
steps:
- checkout
- setup_remote_docker
Expand All @@ -110,9 +129,24 @@ jobs:
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker build \
-t devatherock/<< pipeline.parameters.repo_name >>:$TAG \
-t devatherock/<< pipeline.parameters.repo_name >>:amd64 \
-t devatherock/<< pipeline.parameters.repo_name >>:latest .
docker image push --all-tags devatherock/<< pipeline.parameters.repo_name >>
publish_arm:
executor: native-image-executor-arm
steps:
- checkout
- attach_workspace:
at: ~/<< pipeline.parameters.repo_name >>
- run: |
TAG=${CIRCLE_SHA1:0:8}-arm64
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker build \
-t devatherock/<< pipeline.parameters.repo_name >>:$TAG \
-t devatherock/<< pipeline.parameters.repo_name >>:arm64 .
docker image push --all-tags devatherock/<< pipeline.parameters.repo_name >>
integration_test:
parameters:
command_args:
Expand Down Expand Up @@ -143,14 +177,19 @@ jobs:
- checkout
- setup_remote_docker
- attach_workspace:
at: ~/kafka-lag-monitor
at: ~/<< pipeline.parameters.repo_name >>
- run: |
TAG=${CIRCLE_TAG#v}
DEV_TAG=${CIRCLE_SHA1:0:8}
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker pull devatherock/kafka-lag-monitor:$DEV_TAG
docker tag devatherock/kafka-lag-monitor:$DEV_TAG devatherock/kafka-lag-monitor:$TAG
docker push devatherock/kafka-lag-monitor:$TAG
docker pull devatherock/<< pipeline.parameters.repo_name >>:$DEV_TAG
docker tag devatherock/<< pipeline.parameters.repo_name >>:$DEV_TAG devatherock/<< pipeline.parameters.repo_name >>:$TAG
docker tag devatherock/<< pipeline.parameters.repo_name >>:$DEV_TAG devatherock/<< pipeline.parameters.repo_name >>:${TAG}-amd64
docker pull devatherock/<< pipeline.parameters.repo_name >>:${DEV_TAG}-arm64
docker tag devatherock/<< pipeline.parameters.repo_name >>:${DEV_TAG}-arm64 devatherock/<< pipeline.parameters.repo_name >>:${TAG}-arm64
docker push devatherock/<< pipeline.parameters.repo_name >>:$TAG
docker push devatherock/<< pipeline.parameters.repo_name >>:${TAG}-amd64
docker push devatherock/<< pipeline.parameters.repo_name >>:${TAG}-arm64
workflows:
version: 2.1
Expand All @@ -175,6 +214,11 @@ workflows:
- docker-credentials
requires:
- build
- publish_arm:
context:
- docker-credentials
requires:
- build
- templates/dockerhub_readme_update:
name: dockerhub_readme
context:
Expand All @@ -184,13 +228,21 @@ workflows:
image_description: 'Monitors kafka lag and publishes the metrics to different metrics backends'
work_dir: '~/<< pipeline.parameters.repo_name >>'
requires:
- publish
- publish_arm
- integration_test:
name: integration_test_amd
context:
- docker-credentials
runner: machine-executor
command_args: ''
requires:
- publish
- integration_test:
name: integration_test_arm
context:
- docker-credentials
runner: arm-executor
command_args: 'docker_tag=arm64'
requires:
- dockerhub_readme
- templates/slack:
Expand All @@ -200,6 +252,7 @@ workflows:
- slack-webhook
requires:
- integration_test_amd
- integration_test_arm

release:
jobs:
Expand Down
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
- [#73](https://github.com/devatherock/kafka-lag-monitor/issues/73): Documented configurable properties and environment variables
- Used `circleci-templates` orb to simplify CI pipeline
- [#172](https://github.com/devatherock/kafka-lag-monitor/issues/172): Added integration tests
- Unit test with kafka, to capture reflection config
- [#222](https://github.com/devatherock/kafka-lag-monitor/issues/222): Built a graalvm native image
- [#176](https://github.com/devatherock/kafka-lag-monitor/issues/176): Built separate x86 and arm64 docker images

### Changed
- chore: Added changelog-updater for creating missed changelog entries
Expand All @@ -17,16 +18,11 @@
- chore(deps): update devatherock/simple-slack docker tag to v1
- fix(deps): update dependency net.logstash.logback:logstash-logback-encoder to v7.4
- fix(deps): update dependency org.apache.kafka:kafka-clients to v3.5.1
- chore(deps): update eclipse-temurin docker tag to v17.0.8_7-jre-alpine
- fix(deps): update dependency ch.qos.logback:logback-classic to v1.4.11
- chore(deps): update dependency gradle to v8.3
- chore(deps): update plugin org.sonarqube to v4.3.1.3277
- chore(deps): update plugin com.diffplug.spotless to v6.21.0
- fix(deps): update dependency org.projectlombok:lombok to v1.18.30
- fix(deps): update dependency org.xerial.snappy:snappy-java to v1.1.10.5
- fix(deps): update dependency com.fasterxml.jackson.core:jackson-databind to v2.16.0
- chore(deps): update eclipse-temurin docker tag to v17.0.9_9-jre-alpine
- [#224](https://github.com/devatherock/kafka-lag-monitor/issues/224): Used kafka without zookeeper, in tests
- fix(deps): update dependency ch.qos.logback:logback-classic to v1.4.14
- fix(deps): update dependency com.fasterxml.jackson.core:jackson-databind to v2.16.1
- fix(deps): update dependency net.bytebuddy:byte-buddy to v1.14.11
Expand Down
20 changes: 17 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
FROM eclipse-temurin:17.0.9_9-jre-alpine
COPY build/libs/kafka-lag-monitor-*-all.jar kafka-lag-monitor.jar
FROM ghcr.io/graalvm/native-image-community:17-ol8 as graalvm

COPY . /home/app/micronaut-graal-app
WORKDIR /home/app/micronaut-graal-app

RUN native-image -cp build/libs/*-all.jar



FROM gcr.io/distroless/base-debian11:latest

LABEL maintainer="devatherock@gmail.com"
LABEL io.github.devatherock.version="1.0.0"

EXPOSE 8080
CMD java -Dcom.sun.management.jmxremote -noverify ${JAVA_OPTS} -jar kafka-lag-monitor.jar

COPY --from=graalvm /home/app/micronaut-graal-app/micronautgraalapp /micronaut-graal-app/micronautgraalapp
ENTRYPOINT ["/micronaut-graal-app/micronautgraalapp"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
docker_tag=latest

clean:
./gradlew clean
rm -rf build
integration-test:
DOCKER_TAG=$(docker_tag) docker-compose -f docker-compose-integration.yml up &
./gradlew integrationTest
Expand All @@ -10,6 +10,6 @@ test:
./unit-tests.sh
build-all:
./unit-tests.sh
./gradlew build -x test -Dgraalvm=true
./gradlew build -x test
docker-build:
docker build -t devatherock/kafka-lag-monitor:$(docker_tag) .
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,26 @@ micronaut:

### Environment variables

| Environment Variable Name | Required | Default | Description |
|---------------------------------------|--------------|------------------|----------------------------------------------------------------|
| KAFKA_LAG_MONITOR_THREADPOOL_SIZE | false | 5 | Size of the thread pool used by the lag monitor |
| KAFKA_LAG_MONITOR_TIMEOUT_SECONDS | false | 5 | Timeout for the requests to Kafka, in seconds |
| LOGGER_LEVELS_ROOT | false | INFO | [SLF4J](http://www.slf4j.org/api/org/apache/commons/logging/Log.html) log level, for all(framework and custom) code |
| LOGGER_LEVELS_IO_GITHUB_DEVATHEROCK | false | INFO | [SLF4J](http://www.slf4j.org/api/org/apache/commons/logging/Log.html) log level, for custom code |
| MICRONAUT_SERVER_PORT | false | 8080 | Port in which the app listens on |
| MICRONAUT_CONFIG_FILES | true | (None) | Path to YAML config files. The YAML files can be used to specify complex, object and array properties |
| JAVA_OPTS | false | (None) | Additional JVM arguments to be passed to the container's java process |
| Environment Variable Name | Required | Default | Description |
|---------------------------------------|----------|---------|---------------------------------------------------------------------------------------------------------------------|
| KAFKA_LAG_MONITOR_THREADPOOL_SIZE | false | 5 | Size of the thread pool used by the lag monitor |
| KAFKA_LAG_MONITOR_TIMEOUT_SECONDS | false | 5 | Timeout for the requests to Kafka, in seconds |
| LOGGER_LEVELS_ROOT | false | INFO | [SLF4J](http://www.slf4j.org/api/org/apache/commons/logging/Log.html) log level, for all(framework and custom) code |
| LOGGER_LEVELS_IO_GITHUB_DEVATHEROCK | false | INFO | [SLF4J](http://www.slf4j.org/api/org/apache/commons/logging/Log.html) log level, for custom code |
| MICRONAUT_SERVER_PORT | false | 8080 | Port in which the app listens on |
| MICRONAUT_CONFIG_FILES | true | (None) | Path to YAML config files. The YAML files can be used to specify complex, object and array properties |
| JAVA_OPTS | false | (None) | Additional JVM arguments to be passed to the container's java process |
| LOGBACK_CONFIGURATION_FILE | false | (None) | Path to logback configuration file |

## Troubleshooting
### Enabling debug logs
- Set the environment variable `LOGGER_LEVELS_ROOT` to `DEBUG` to enable all debug logs - custom and framework
- Set the environment variable `LOGGER_LEVELS_IO_GITHUB_DEVATHEROCK` to `DEBUG` to enable debug logs only in custom code
- For fine-grained logging control, supply a custom [logback.xml](http://logback.qos.ch/manual/configuration.html) file
and set the environment variable `JAVA_OPTS` to `-Dlogback.configurationFile=/path/to/custom/logback.xml`
and set the environment variable `LOGBACK_CONFIGURATION_FILE` to `/path/to/custom/logback.xml`

### JSON logs

To output logs as JSON, set the environment variable `JAVA_OPTS` to `-Dlogback.configurationFile=logback-json.xml`. Refer
To output logs as JSON, set the environment variable `LOGBACK_CONFIGURATION_FILE` to `logback-json.xml`. Refer
[logstash-logback-encoder](https://github.com/logstash/logstash-logback-encoder) documentation to customize the field names and
formats in the log
24 changes: 21 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ dependencies {
implementation group: 'org.apache.kafka', name: 'kafka-clients', version: '3.5.1'
implementation group: 'org.xerial.snappy', name: 'snappy-java', version: '1.1.10.5'
implementation group: 'javax.inject', name: 'javax.inject', version: '1'

runtimeOnly("ch.qos.logback:logback-classic:1.4.14")
implementation("ch.qos.logback:logback-classic:1.4.14")

testImplementation group: 'org.spockframework', name: 'spock-core', version: '2.3-groovy-3.0'
testImplementation group: 'net.bytebuddy', name: 'byte-buddy', version: '1.14.11'
Expand All @@ -74,6 +73,17 @@ application {
mainClass.set("io.github.devatherock.Application")
}

test {
doFirst {
if (Boolean.getBoolean('graalvm')) {
jvmArgs("-agentlib:native-image-agent=access-filter-file=$projectDir/src/main/resources/graal/access-filter.json,config-output-dir=$projectDir/build/graal")
}
}
}

// Generates resource-config.json file for graalvm
build.dependsOn(generateResourcesConfigFile)

tasks.withType(Test) {
useJUnitPlatform()
environment('DOCKER_NETWORK_IP', System.getenv('DOCKER_NETWORK_IP') ?: 'localhost')
Expand All @@ -88,7 +98,15 @@ run {
ext.jacoco = [
exclusions: [
'io/github/devatherock/Application.class'
]
],
coverageThresholds: [
'io.github.devatherock.logback.LogbackConfigInitializer': [
'BRANCH': 0.75,
'COMPLEXITY': 0.62,
'INSTRUCTION': 0.89,
'LINE': 0.88
],
],
]

apply from: "${getPropertyValue('gradle.includes.location', 'https://raw.githubusercontent.com/devatherock/gradle-includes/master')}/integration.gradle"
Expand Down
Loading

0 comments on commit 5383c94

Please sign in to comment.