Skip to content

Commit

Permalink
feat : add kafka
Browse files Browse the repository at this point in the history
  • Loading branch information
JiwonKKang committed Oct 15, 2024
1 parent 505ec33 commit 172b5e2
Show file tree
Hide file tree
Showing 66 changed files with 963 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/

/kafka

### STS ###
.apt_generated
.classpath
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
start-kafka:
docker compose -f kafka/common.yml -f kafka/zookeeper.yml -f kafka/kafka_cluster.yml up -d

stop-kafka:
docker compose -f kafka/common.yml -f kafka/zookeeper.yml -f kafka/kafka_cluster.yml down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class LogAspect {

@Pointcut(
"execution(* com.univ.tracedin.domain..*(..)) && execution(* com.univ.tracedin.infra..*(..))&& !execution(* com.univ.tracedin.common..*(..))")
"execution(* com.univ.tracedin.domain..*(..)) || execution(* com.univ.tracedin.infra..*(..)) && !execution(* com.univ.tracedin.common..*(..))")
public void all() {}

@Pointcut(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.univ.tracedin.api.metric.dto.AppendServiceMetricsRequest;
import com.univ.tracedin.api.metric.dto.HttpRequestCountResponse;
import com.univ.tracedin.domain.metric.ServiceMetricsService;
import com.univ.tracedin.domain.project.ServiceNode;
import com.univ.tracedin.domain.project.Node;

@RestController
@RequestMapping("/api/v1/metrics")
Expand All @@ -29,9 +29,9 @@ public void appendMetrics(@RequestBody AppendServiceMetricsRequest requests) {
}

@GetMapping("/http-request-count")
public Response<List<HttpRequestCountResponse>> getHttpRequestCount(ServiceNode serviceNode) {
public Response<List<HttpRequestCountResponse>> getHttpRequestCount(Node node) {
List<HttpRequestCountResponse> responses =
serviceMetricService.getHttpRequestCount(serviceNode).stream()
serviceMetricService.getHttpRequestCount(node).stream()
.map(HttpRequestCountResponse::from)
.toList();
return Response.success(responses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.univ.tracedin.api.global.dto.Response;
import com.univ.tracedin.api.metric.dto.AppendServiceMetricsRequest;
import com.univ.tracedin.api.metric.dto.HttpRequestCountResponse;
import com.univ.tracedin.domain.project.ServiceNode;
import com.univ.tracedin.domain.project.Node;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -19,5 +19,5 @@ public interface ServiceMetricsApiDocs {
@Operation(
summary = "5시간 이내의 10분 별로 HTTP 요청 횟수 조회",
description = "5시간 이내의 10분 별로 HTTP 요청 횟수 조회, 개발 기간에는 5시간 이내 조건 없음")
Response<List<HttpRequestCountResponse>> getHttpRequestCount(ServiceNode serviceNode);
Response<List<HttpRequestCountResponse>> getHttpRequestCount(Node node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
Expand All @@ -17,9 +18,9 @@
import com.univ.tracedin.domain.auth.UserPrincipal;
import com.univ.tracedin.domain.project.EndTimeBucket;
import com.univ.tracedin.domain.project.NetworkTopology;
import com.univ.tracedin.domain.project.Node;
import com.univ.tracedin.domain.project.ProjectKey;
import com.univ.tracedin.domain.project.ProjectService;
import com.univ.tracedin.domain.project.ServiceNode;

@RestController
@RequiredArgsConstructor
Expand All @@ -37,7 +38,7 @@ public Response<ProjectKey> createProject(
}

@GetMapping("/{projectKey}/service-nodes")
public Response<List<ServiceNode>> serviceNodes(@PathVariable String projectKey) {
public Response<List<Node>> serviceNodes(@PathVariable String projectKey) {
return Response.success(projectService.getServiceNodeList(projectKey));
}

Expand All @@ -47,7 +48,8 @@ public Response<NetworkTopology> networkTopology(@PathVariable String projectKey
}

@GetMapping("/{projectKey}/hit-map")
public Response<List<EndTimeBucket>> hitMap(@PathVariable String projectKey) {
return Response.success(projectService.getTraceHitMap(projectKey));
public Response<List<EndTimeBucket>> hitMap(
@PathVariable String projectKey, @RequestParam(required = false) String serviceName) {
return Response.success(projectService.getTraceHitMap(projectKey, serviceName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.univ.tracedin.domain.auth.UserPrincipal;
import com.univ.tracedin.domain.project.EndTimeBucket;
import com.univ.tracedin.domain.project.NetworkTopology;
import com.univ.tracedin.domain.project.Node;
import com.univ.tracedin.domain.project.ProjectKey;
import com.univ.tracedin.domain.project.ServiceNode;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -20,11 +20,11 @@ public interface ProjectApiDocs {
Response<ProjectKey> createProject(CreateProjectRequest request, UserPrincipal currentUser);

@Operation(summary = "서비스 리스트 조회", description = "프로젝트의 서비스 노드 리스트를 조회합니다.")
Response<List<ServiceNode>> serviceNodes(String projectKey);
Response<List<Node>> serviceNodes(String projectKey);

@Operation(summary = "네트워크 토폴로지 조회", description = "프로젝트의 네트워크 토폴로지를 조회합니다.")
Response<NetworkTopology> networkTopology(String projectKey);

@Operation(summary = "히트맵 조회", description = "프로젝트의 히트맵을 조회합니다.(1시간별 트레이스의 응답시간 분포)")
Response<List<EndTimeBucket>> hitMap(String projectKey);
@Operation(summary = "히트맵 조회", description = "프로젝트의 히트맵을 조회합니다.(5분 별 트레이스의 응답시간 분포)")
Response<List<EndTimeBucket>> hitMap(String projectKey, String serviceName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.univ.tracedin.api.global.dto.Response;
import com.univ.tracedin.api.span.dto.AppendSpanRequest;
import com.univ.tracedin.api.span.dto.ReadSpanRequest;
import com.univ.tracedin.api.span.dto.ReadTraceRequest;
import com.univ.tracedin.api.span.dto.SpanTreeResponse;
import com.univ.tracedin.api.span.dto.TraceResponse;
import com.univ.tracedin.common.dto.SearchCursor;
Expand All @@ -32,14 +32,14 @@ public class SpanApi implements SpanApiDocs {
@PostMapping
public void appendSpan(@RequestBody List<AppendSpanRequest> request) {
log.info("appendSpan request: {}", request.toString());
spanService.appendSpan(request.stream().map(AppendSpanRequest::toSpan).toList());
spanService.publishSpans(request.stream().map(AppendSpanRequest::toSpan).toList());
}

@GetMapping("/traces")
public Response<SearchResult<TraceResponse>> getTraces(
ReadSpanRequest request, SearchCursor cursor) {
public Response<SearchResult<TraceResponse>> searchTraces(
ReadTraceRequest request, SearchCursor cursor) {
SearchResult<TraceResponse> responses =
spanService.getTraces(request.toServiceNode(), cursor).map(TraceResponse::from);
spanService.getTraces(request.toSearchCond(), cursor).map(TraceResponse::from);
return Response.success(responses);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.univ.tracedin.api.global.dto.Response;
import com.univ.tracedin.api.span.dto.AppendSpanRequest;
import com.univ.tracedin.api.span.dto.ReadSpanRequest;
import com.univ.tracedin.api.span.dto.ReadTraceRequest;
import com.univ.tracedin.api.span.dto.SpanTreeResponse;
import com.univ.tracedin.api.span.dto.TraceResponse;
import com.univ.tracedin.common.dto.SearchCursor;
Expand All @@ -20,7 +20,8 @@ public interface SpanApiDocs {
void appendSpan(List<AppendSpanRequest> request);

@Operation(summary = "트레이스(트랜잭션) 조회 API", description = "프로젝트의 특정 서비스의 트레이스(트랜잭션)를 조회합니다.")
Response<SearchResult<TraceResponse>> getTraces(ReadSpanRequest request, SearchCursor cursor);
Response<SearchResult<TraceResponse>> searchTraces(
ReadTraceRequest request, SearchCursor cursor);

@Operation(
summary = "트레이스(트랜잭션) 내 스팬트리 조회 API",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Span toSpan() {
}

private SpanType getSpanType() {
SpanType type = spanType == null ? null : SpanType.fromValue(spanType);
SpanType type = spanType == null ? SpanType.UNKNOWN : SpanType.fromValue(spanType);
if (attributes.data().containsKey("db.operation")) {
type = SpanType.QUERY;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.univ.tracedin.api.span.dto;

import java.time.LocalDateTime;

import com.univ.tracedin.domain.project.Node;
import com.univ.tracedin.domain.project.NodeType;
import com.univ.tracedin.domain.span.TraceSearchCond;

public record ReadTraceRequest(
String projectKey, String serviceName, LocalDateTime startTime, LocalDateTime endTime) {

public TraceSearchCond toSearchCond() {
return new TraceSearchCond(
Node.of(projectKey, serviceName, NodeType.SERVICE), startTime, endTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jwt:
access-token:
expiration: 25920000
server:
port: 8989
port: 8089
---
spring:
config:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.univ.tracedin.domain.global;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class BaseId<T> {

private T value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Metric {

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ServiceMetrics {

private String projectKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.univ.tracedin.domain.metric;

import java.util.List;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -10,7 +12,7 @@ public class ServiceMetricsAppender {

private final ServiceMetricsRepository serviceMetricsRepository;

public void append(ServiceMetrics metrics) {
serviceMetricsRepository.save(metrics);
public void appendAll(List<ServiceMetrics> metrics) {
serviceMetricsRepository.saveAll(metrics);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.univ.tracedin.domain.metric;

import java.io.Serializable;

public record ServiceMetricsCollectedEvent(ServiceMetrics serviceMetrics) implements Serializable {

public static ServiceMetricsCollectedEvent from(ServiceMetrics metrics) {
return new ServiceMetricsCollectedEvent(metrics);
}

public String getKey() {
return serviceMetrics.getProjectKey() + "-" + serviceMetrics.getServiceName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.univ.tracedin.domain.metric;

public interface ServiceMetricsCollectedMessagePublisher {

void publish(ServiceMetricsCollectedEvent serviceMetricsCollectedEvent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import lombok.RequiredArgsConstructor;

import com.univ.tracedin.domain.project.ServiceNode;
import com.univ.tracedin.domain.project.Node;

@Component
@RequiredArgsConstructor
public class ServiceMetricsReader {

private final ServiceMetricsRepository serviceMetricsRepository;

public List<HttpRequestCount> readHttpRequestCount(ServiceNode serviceNode) {
return serviceMetricsRepository.getHttpRequestCount(serviceNode);
public List<HttpRequestCount> readHttpRequestCount(Node node) {
return serviceMetricsRepository.getHttpRequestCount(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.List;

import com.univ.tracedin.domain.project.ServiceNode;
import com.univ.tracedin.domain.project.Node;

public interface ServiceMetricsRepository {

void save(ServiceMetrics metrics);
List<HttpRequestCount> getHttpRequestCount(Node node);

List<HttpRequestCount> getHttpRequestCount(ServiceNode serviceNode);
void saveAll(List<ServiceMetrics> metrics);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

import lombok.RequiredArgsConstructor;

import com.univ.tracedin.domain.project.ServiceNode;
import com.univ.tracedin.domain.project.Node;

@Service
@RequiredArgsConstructor
public class ServiceMetricsService {

private final ServiceMetricsAppender serviceMetricAppender;
private final ServiceMetricsReader serviceMetricReader;
private final ServiceMetricsCollectedMessagePublisher serviceMetricsCollectedMessagePublisher;

public void appendMetrics(ServiceMetrics metrics) {
serviceMetricAppender.append(metrics);
serviceMetricsCollectedMessagePublisher.publish(ServiceMetricsCollectedEvent.from(metrics));
}

public List<HttpRequestCount> getHttpRequestCount(ServiceNode serviceNode) {
return serviceMetricReader.readHttpRequestCount(serviceNode);
public List<HttpRequestCount> getHttpRequestCount(Node node) {
return serviceMetricReader.readHttpRequestCount(node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public interface HitMapReader {

List<EndTimeBucket> read(String projectKey);
List<EndTimeBucket> read(String projectKey, String serviceName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class NetworkTopology {

private final List<ServiceNode> serviceNodes;
private final List<Node> nodes;
private final List<Edge> edges;

public static NetworkTopology of(List<ServiceNode> serviceNodes, List<Edge> edges) {
return new NetworkTopology(serviceNodes, edges);
public static NetworkTopology of(List<Node> nodes, List<Edge> edges) {
return new NetworkTopology(nodes, edges);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.univ.tracedin.domain.project;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class Node {

private String projectKey;
private String name;
private NodeType nodeType;

public static Node of(String projectKey, String serviceName, NodeType nodeType) {
return new Node(projectKey, serviceName, nodeType);
}
}
Loading

0 comments on commit 172b5e2

Please sign in to comment.