Skip to content
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

Dev to Main week3 #130

Merged
merged 8 commits into from
Aug 27, 2024
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
56 changes: 31 additions & 25 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
name: cicd pipeline

on:
push:
branches: [ "dev" ]

env:
DOCKER_IMAGE_TAG: logbat/logbat:latest
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}
JAR_FILE: logbat.jar
BASTION_HOST_IP: ${{ secrets.BASTION_HOST_IP }}
BACKEND_HOST_IP: ${{ secrets.BACKEND_HOST_IP }}


permissions:
contents: read

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up JDK 17
- name: Set up Liberica JDK 17
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: '17'

- name: Cleanup application.yml
run: |
cd logbat
rm -f src/main/resources/application.yml

- name: Create application-dev.yml from GitHub Secret
run: |
cd logbat
mkdir -p src/main/resources
echo "${{ secrets.APPLICATION_YML }}" > src/main/resources/application.yml

- name: Build with Gradle
env:
ORG_GRADLE_OPTS: "-Duser.timezone=Asia/Seoul"
run: |
cd logbat
./gradlew clean bootJar -x test

- name: Login to Docker Hub
run: echo ${{ env.DOCKER_HUB_PASSWORD }} | docker login -u ${{ env.DOCKER_HUB_USERNAME }} --password-stdin

- name: Build Docker Image
- name: Rename JAR file to logbat.jar
run: |
cd logbat
docker build -t ${{ env.DOCKER_IMAGE_TAG }} .
cd logbat/build/libs
JAR_FILE=$(ls *.jar)
if [ -z "$JAR_FILE" ]; then
echo "Error: No JAR files found!"
exit 1
fi
mv "$JAR_FILE" logbat.jar
echo "Renamed $JAR_FILE to logbat.jar"

- name: Push Docker Image to Docker Hub
run: docker push ${{ env.DOCKER_IMAGE_TAG }}
- name: Start SSH Agent and Add Bastion Key
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BASTION_SSH_SECRET_KEY }}

- name: Add Target Server Key to SSH Agent
run: echo "${{ secrets.PRIVATE_SSH_SECRET_KEY }}" | ssh-add -

- name: SCP JAR file to EC2 via Bastion
run: |
cd logbat/build/libs
scp -o StrictHostKeyChecking=no -o ProxyCommand="ssh -W %h:%p -o StrictHostKeyChecking=no ubuntu@${{ secrets.BASTION_HOST_IP }}" \
logbat.jar ubuntu@${{ secrets.BACKEND_HOST_IP }}:/home/ubuntu/logbat.jar

deploy:
name: Deploy to EC2
Expand All @@ -67,12 +74,11 @@ jobs:
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.BASTION_SSH_SECRET_KEY }}

- name: Add Target Server Key to SSH Agent
run: echo "${{ secrets.PRIVATE_SSH_SECRET_KEY }}" | ssh-add -

- name: Connect to Bastion and Deploy to EC2
run: |
ssh -o StrictHostKeyChecking=no -A ubuntu@${{ secrets.BASTION_HOST_IP }} \
"ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.BACKEND_HOST_IP }} 'bash /home/ubuntu/deploy.sh'"

"ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.BACKEND_HOST_IP }} 'bash -l /home/ubuntu/deploy.sh'"
Binary file not shown.
39 changes: 39 additions & 0 deletions docs/info/logbat/logbat-sdk/0.1.2/logbat-sdk-0.1.2.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>info.logbat</groupId>
<artifactId>logbat-sdk</artifactId>
<version>0.1.2</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
2 changes: 2 additions & 0 deletions logbat/src/main/java/info/logbat/LogbatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class LogbatApplication {

public static void main(String[] args) {
Expand Down
48 changes: 48 additions & 0 deletions logbat/src/main/java/info/logbat/dev/util/MemoryUsageLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package info.logbat.dev.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

@Component
public class MemoryUsageLogger {

private static final Logger logger = LoggerFactory.getLogger(MemoryUsageLogger.class);
private final MemoryMXBean memoryMXBean;

public MemoryUsageLogger() {
this.memoryMXBean = ManagementFactory.getMemoryMXBean();
}

@Scheduled(fixedRate = 1000)
public void logMemoryUsage() {
// Heap 메모리 사용량
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
long usedHeapMemory = heapMemoryUsage.getUsed();
long maxHeapMemory = heapMemoryUsage.getMax();
long committedHeapMemory = heapMemoryUsage.getCommitted();

// Non-Heap 메모리 사용량
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
long usedNonHeapMemory = nonHeapMemoryUsage.getUsed();
long maxNonHeapMemory = nonHeapMemoryUsage.getMax();
long committedNonHeapMemory = nonHeapMemoryUsage.getCommitted();

// Finalization 대기 객체 수
int pendingFinalizationCount = memoryMXBean.getObjectPendingFinalizationCount();

logger.info("Memory Usage [Heap: Used={} MB, Max={} MB, Committed={} MB | Non-Heap: Used={} MB, Max={} MB, Committed={} MB | Pending Finalizations: {}]",
usedHeapMemory / 1024 / 1024,
maxHeapMemory / 1024 / 1024,
committedHeapMemory / 1024 / 1024,
usedNonHeapMemory / 1024 / 1024,
maxNonHeapMemory / 1024 / 1024,
committedNonHeapMemory / 1024 / 1024,
pendingFinalizationCount);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package info.logbat.domain.log.application;

import info.logbat.domain.log.domain.Log;
import info.logbat.domain.log.presentation.payload.request.CreateLogRequest;
import info.logbat.domain.log.repository.LogRepository;
import info.logbat.domain.project.application.AppService;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class LogService {
Expand All @@ -16,9 +21,16 @@ public class LogService {

public void saveLogs(String appKey, List<CreateLogRequest> requests) {
Long appId = appService.getAppIdByToken(appKey);
logRepository.saveAll(requests.stream()
.map(request -> request.toEntity(appId))
.toList());
List<Log> logs = new ArrayList<>(requests.size());
requests.forEach(request -> {
try {
logs.add(request.toEntity(appId));
}
catch (Exception e) {
log.error("Failed to convert request to entity: {}", request, e);
}
});
logRepository.saveAll(logs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ private LogData(String data) {
}

private void validateData(String data) {
if (data == null || data.isBlank()) {
throw new IllegalArgumentException("log data는 null이거나 빈 문자열일 수 없습니다.");
if (data == null) {
throw new IllegalArgumentException("log data는 null일 수 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import info.logbat.dev.aop.CountTest;
import info.logbat.domain.log.application.LogService;
import info.logbat.domain.log.presentation.payload.request.CreateLogRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -27,8 +27,9 @@ public class LogController {
@ResponseStatus(HttpStatus.CREATED)
public void saveLogs(
@RequestHeader("App-Key") @NotBlank(message = "appKey가 비어있습니다.") String appKey,
@Valid @RequestBody List<CreateLogRequest> request) {
logService.saveLogs(appKey, request);
@RequestBody @NotEmpty List<CreateLogRequest> requests) {
logService.saveLogs(appKey, requests);
}

}

Loading
Loading