diff --git a/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentStatHandlerV2.java b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentStatHandlerV2.java index dbdd90996ba7..dc865f3d34be 100644 --- a/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentStatHandlerV2.java +++ b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentStatHandlerV2.java @@ -17,27 +17,14 @@ package com.navercorp.pinpoint.collector.handler.grpc; import com.google.protobuf.GeneratedMessageV3; -import com.navercorp.pinpoint.collector.config.CollectorConfiguration; import com.navercorp.pinpoint.collector.handler.SimpleHandler; -import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentStatBatchMapper; -import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentStatMapper; -import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentUriStatMapper; -import com.navercorp.pinpoint.collector.service.AgentStatService; -import com.navercorp.pinpoint.collector.service.AgentUriStatService; -import com.navercorp.pinpoint.common.server.bo.stat.AgentStatBo; -import com.navercorp.pinpoint.common.server.bo.stat.AgentUriStatBo; -import com.navercorp.pinpoint.grpc.Header; -import com.navercorp.pinpoint.grpc.MessageFormatUtils; -import com.navercorp.pinpoint.grpc.server.ServerContext; -import com.navercorp.pinpoint.grpc.trace.PAgentStat; -import com.navercorp.pinpoint.grpc.trace.PAgentStatBatch; -import com.navercorp.pinpoint.grpc.trace.PAgentUriStat; import com.navercorp.pinpoint.io.request.ServerRequest; import io.grpc.Status; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Objects; /** @@ -46,104 +33,31 @@ @Service public class GrpcAgentStatHandlerV2 implements SimpleHandler { private final Logger logger = LogManager.getLogger(this.getClass().getName()); - private final boolean isDebug = logger.isDebugEnabled(); + private final GrpcMetricHandler[] metricHandlers; - private final CollectorConfiguration collectorConfiguration; - private final GrpcAgentStatMapper agentStatMapper; + public GrpcAgentStatHandlerV2(List metricHandlers) { + Objects.requireNonNull(metricHandlers, "metricHandlers"); + this.metricHandlers = metricHandlers.toArray(new GrpcMetricHandler[]{}); - private final GrpcAgentStatBatchMapper agentStatBatchMapper; - - private final GrpcAgentUriStatMapper agentUriStatMapper; - - private final AgentStatService[] agentStatServiceList; - - private final AgentUriStatService agentUriStatService; - - public GrpcAgentStatHandlerV2(GrpcAgentStatMapper agentStatMapper, - GrpcAgentStatBatchMapper agentStatBatchMapper, - GrpcAgentUriStatMapper agentUriStatMapper, - AgentStatService[] agentStatServiceList, - AgentUriStatService agentUriStatService, - CollectorConfiguration collectorConfiguration) { - this.agentStatMapper = Objects.requireNonNull(agentStatMapper, "agentStatMapper"); - this.agentStatBatchMapper = Objects.requireNonNull(agentStatBatchMapper, "agentStatBatchMapper"); - this.agentUriStatMapper = Objects.requireNonNull(agentUriStatMapper, "agentUriStatMapper"); - - this.agentStatServiceList = Objects.requireNonNull(agentStatServiceList, "agentStatServiceList"); - for (AgentStatService agentStatService : this.agentStatServiceList) { - logger.info("AgentStatService:{}", agentStatService.getClass().getSimpleName()); + for (GrpcMetricHandler handler : this.metricHandlers) { + logger.info("{}:{}", GrpcMetricHandler.class.getSimpleName(), handler); } - - this.agentUriStatService = Objects.requireNonNull(agentUriStatService, "agentUriStatService"); - this.collectorConfiguration = Objects.requireNonNull(collectorConfiguration, "collectorConfiguration"); } @Override public void handleSimple(ServerRequest serverRequest) { final GeneratedMessageV3 data = serverRequest.getData(); - if (data instanceof PAgentStat) { - handleAgentStat((PAgentStat) data); - } else if (data instanceof PAgentStatBatch) { - handleAgentStatBatch((PAgentStatBatch) data); - } else if (data instanceof PAgentUriStat) { - if (collectorConfiguration.isUriStatEnable()) { - handleAgentUriStat((PAgentUriStat) data); - } else { - logger.debug("Failed to handle PAgentUriStat. Cause : uriStatEnable is false."); - } - } else { - logger.warn("Invalid request type. serverRequest={}", serverRequest); - throw Status.INTERNAL.withDescription("Bad Request(invalid request type)").asRuntimeException(); - } - } - - private void handleAgentStat(PAgentStat agentStat) { - if (isDebug) { - logger.debug("Handle PAgentStat={}", MessageFormatUtils.debugLog(agentStat)); - } - - final AgentStatBo agentStatBo = this.agentStatMapper.map(agentStat); - if (agentStatBo == null) { - return; - } - - for (AgentStatService agentStatService : agentStatServiceList) { - try { - agentStatService.save(agentStatBo); - } catch (Exception e) { - logger.warn("Failed to handle service={}, AgentStat={}", agentStatService, MessageFormatUtils.debugLog(agentStat), e); - } - } - } - - private void handleAgentStatBatch(PAgentStatBatch agentStatBatch) { - if (isDebug) { - logger.debug("Handle PAgentStatBatch={}", MessageFormatUtils.debugLog(agentStatBatch)); - } - - Header header = ServerContext.getAgentInfo(); - final AgentStatBo agentStatBo = this.agentStatBatchMapper.map(agentStatBatch, header); - if (agentStatBo == null) { - return; - } - for (AgentStatService agentStatService : agentStatServiceList) { - try { - agentStatService.save(agentStatBo); - } catch (Exception e) { - logger.warn("Failed to handle service={}, AgentStatBatch={}", agentStatService, MessageFormatUtils.debugLog(agentStatBatch), e); + for (GrpcMetricHandler messageHandler : metricHandlers) { + if (messageHandler.accept(data)) { + messageHandler.handle(data); + return; } } - } - - private void handleAgentUriStat(PAgentUriStat agentUriStat) { - if (isDebug) { - logger.debug("Handle PAgentUriStat={}", MessageFormatUtils.debugLog(agentUriStat)); - } - final AgentUriStatBo agentUriStatBo = agentUriStatMapper.map(agentUriStat); - agentUriStatService.save(agentUriStatBo); + logger.warn("Invalid request type. serverRequest={}", serverRequest); + throw Status.INTERNAL.withDescription("Bad Request(invalid request type)").asRuntimeException(); } } \ No newline at end of file diff --git a/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcMetricHandler.java b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcMetricHandler.java new file mode 100644 index 000000000000..9861813f86b2 --- /dev/null +++ b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcMetricHandler.java @@ -0,0 +1,11 @@ +package com.navercorp.pinpoint.collector.handler.grpc; + +import com.google.protobuf.GeneratedMessageV3; +import org.springframework.stereotype.Component; + +@Component +public interface GrpcMetricHandler { + boolean accept(GeneratedMessageV3 message); + + void handle(GeneratedMessageV3 message); +} diff --git a/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentMetricBatchHandler.java b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentMetricBatchHandler.java new file mode 100644 index 000000000000..a6efd4e7b79f --- /dev/null +++ b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentMetricBatchHandler.java @@ -0,0 +1,52 @@ +package com.navercorp.pinpoint.collector.handler.grpc.metric; + +import com.google.protobuf.GeneratedMessageV3; +import com.navercorp.pinpoint.collector.handler.grpc.GrpcMetricHandler; +import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentStatBatchMapper; +import com.navercorp.pinpoint.common.server.bo.stat.AgentStatBo; +import com.navercorp.pinpoint.grpc.Header; +import com.navercorp.pinpoint.grpc.MessageFormatUtils; +import com.navercorp.pinpoint.grpc.server.ServerContext; +import com.navercorp.pinpoint.grpc.trace.PAgentStatBatch; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +@Component +public class AgentMetricBatchHandler implements GrpcMetricHandler { + private final Logger logger = LogManager.getLogger(this.getClass()); + + private final GrpcAgentStatBatchMapper agentStatBatchMapper; + + private final AgentMetricHandler agentMetricHandler; + + + public AgentMetricBatchHandler(GrpcAgentStatBatchMapper agentStatBatchMapper, + AgentMetricHandler agentMetricHandler) { + this.agentStatBatchMapper = Objects.requireNonNull(agentStatBatchMapper, "agentStatBatchMapper"); + this.agentMetricHandler = Objects.requireNonNull(agentMetricHandler, "agentStatHandler"); + } + + @Override + public boolean accept(GeneratedMessageV3 message) { + return message instanceof PAgentStatBatch; + } + + @Override + public void handle(GeneratedMessageV3 message) { + if (logger.isDebugEnabled()) { + logger.debug("Handle PAgentStatBatch={}", MessageFormatUtils.debugLog(message)); + } + final PAgentStatBatch agentStatBatch = (PAgentStatBatch) message; + + final Header header = ServerContext.getAgentInfo(); + final AgentStatBo agentStatBo = this.agentStatBatchMapper.map(agentStatBatch, header); + if (agentStatBo == null) { + return; + } + + this.agentMetricHandler.handleAgentStat(agentStatBo); + } +} diff --git a/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentMetricHandler.java b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentMetricHandler.java new file mode 100644 index 000000000000..df24f28671e8 --- /dev/null +++ b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentMetricHandler.java @@ -0,0 +1,63 @@ +package com.navercorp.pinpoint.collector.handler.grpc.metric; + +import com.google.protobuf.GeneratedMessageV3; +import com.navercorp.pinpoint.collector.handler.grpc.GrpcMetricHandler; +import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentStatMapper; +import com.navercorp.pinpoint.collector.service.AgentStatService; +import com.navercorp.pinpoint.common.server.bo.stat.AgentStatBo; +import com.navercorp.pinpoint.grpc.MessageFormatUtils; +import com.navercorp.pinpoint.grpc.trace.PAgentStat; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +@Component +public class AgentMetricHandler implements GrpcMetricHandler { + private final Logger logger = LogManager.getLogger(this.getClass()); + + private final GrpcAgentStatMapper agentStatMapper; + + private final AgentStatService[] agentStatServiceList; + + public AgentMetricHandler(GrpcAgentStatMapper agentStatMapper, + AgentStatService[] agentStatServiceList) { + this.agentStatMapper = Objects.requireNonNull(agentStatMapper, "agentStatMapper"); + this.agentStatServiceList = Objects.requireNonNull(agentStatServiceList, "agentStatServiceList"); + + for (AgentStatService service : this.agentStatServiceList) { + logger.info("{}:{}", AgentStatService.class.getSimpleName(), service.getClass().getSimpleName()); + } + } + + @Override + public boolean accept(GeneratedMessageV3 message) { + return message instanceof PAgentStat; + } + + @Override + public void handle(GeneratedMessageV3 message) { + if (logger.isDebugEnabled()) { + logger.debug("Handle PAgentStat={}", MessageFormatUtils.debugLog(message)); + } + final PAgentStat agentStat = (PAgentStat) message; + + final AgentStatBo agentStatBo = this.agentStatMapper.map(agentStat); + if (agentStatBo == null) { + return; + } + + handleAgentStat(agentStatBo); + } + + public void handleAgentStat(AgentStatBo agentStatBo) { + for (AgentStatService agentStatService : agentStatServiceList) { + try { + agentStatService.save(agentStatBo); + } catch (Exception e) { + logger.warn("Failed to handle service={}, AgentStatBo={}", agentStatService, agentStatBo, e); + } + } + } +} diff --git a/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentUriMetricHandler.java b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentUriMetricHandler.java new file mode 100644 index 000000000000..a51cb4957212 --- /dev/null +++ b/collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/metric/AgentUriMetricHandler.java @@ -0,0 +1,62 @@ +package com.navercorp.pinpoint.collector.handler.grpc.metric; + +import com.google.protobuf.GeneratedMessageV3; +import com.navercorp.pinpoint.collector.config.CollectorConfiguration; +import com.navercorp.pinpoint.collector.handler.grpc.GrpcMetricHandler; +import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentUriStatMapper; +import com.navercorp.pinpoint.collector.service.AgentUriStatService; +import com.navercorp.pinpoint.common.server.bo.stat.AgentUriStatBo; +import com.navercorp.pinpoint.grpc.MessageFormatUtils; +import com.navercorp.pinpoint.grpc.trace.PAgentUriStat; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +@Component +public class AgentUriMetricHandler implements GrpcMetricHandler { + + private final Logger logger = LogManager.getLogger(this.getClass()); + + private final GrpcAgentUriStatMapper agentUriStatMapper; + private final AgentUriStatService agentUriStatService; + + private final boolean uriStatEnable; + + public AgentUriMetricHandler(CollectorConfiguration collectorConfiguration, + GrpcAgentUriStatMapper agentUriStatMapper, + AgentUriStatService agentUriStatService) { + Objects.requireNonNull(collectorConfiguration, "collectorConfiguration"); + this.uriStatEnable = collectorConfiguration.isUriStatEnable(); + + this.agentUriStatMapper = Objects.requireNonNull(agentUriStatMapper, "agentUriStatMapper"); + this.agentUriStatService = Objects.requireNonNull(agentUriStatService, "agentUriStatService"); + } + + @Override + public boolean accept(GeneratedMessageV3 message) { + return message instanceof PAgentUriStat; + + } + + @Override + public void handle(GeneratedMessageV3 message) { + if (logger.isDebugEnabled()) { + logger.debug("Handle PAgentUriStat={}", MessageFormatUtils.debugLog(message)); + } + if (!uriStatEnable) { + return; + } + final PAgentUriStat agentUriStat = (PAgentUriStat) message; + final AgentUriStatBo agentUriStatBo = agentUriStatMapper.map(agentUriStat); + agentUriStatService.save(agentUriStatBo); + } + + @Override + public String toString() { + return "AgentUriStatHandler{" + + "uriStatEnable=" + uriStatEnable + + '}'; + } +} diff --git a/collector/src/test/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentUriStatHandlerV2Test.java b/collector/src/test/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentUriMetricHandlerV2Test.java similarity index 70% rename from collector/src/test/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentUriStatHandlerV2Test.java rename to collector/src/test/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentUriMetricHandlerV2Test.java index 01a3ab039b5e..368e28c88ce3 100644 --- a/collector/src/test/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentUriStatHandlerV2Test.java +++ b/collector/src/test/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentUriMetricHandlerV2Test.java @@ -18,6 +18,9 @@ import com.google.protobuf.GeneratedMessageV3; import com.navercorp.pinpoint.collector.config.CollectorConfiguration; +import com.navercorp.pinpoint.collector.handler.grpc.metric.AgentMetricBatchHandler; +import com.navercorp.pinpoint.collector.handler.grpc.metric.AgentMetricHandler; +import com.navercorp.pinpoint.collector.handler.grpc.metric.AgentUriMetricHandler; import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentStatBatchMapper; import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentStatMapper; import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentUriStatMapper; @@ -37,28 +40,29 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ThreadLocalRandom; -import static org.mockito.Mockito.never; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author Taejin Koo */ -public class GrpcAgentUriStatHandlerV2Test { +public class GrpcAgentUriMetricHandlerV2Test { private Context prevContext; @BeforeEach - public void setUp() throws Exception { + public void setUp() { Context root = Context.ROOT; prevContext = root.attach(); } @AfterEach - public void tearDown() throws Exception { + public void tearDown() { Context root = Context.ROOT; if (prevContext != null) { root.detach(prevContext); @@ -68,9 +72,8 @@ public void tearDown() throws Exception { @Test public void throwExceptionTest() { Assertions.assertThrows(StatusRuntimeException.class, () -> { - AgentUriStatService mockAgentUriStatService = Mockito.mock(AgentUriStatService.class); - - ServerRequest mockServerRequest = Mockito.mock(ServerRequest.class); + AgentUriStatService mockAgentUriStatService = mock(AgentUriStatService.class); + ServerRequest mockServerRequest = mock(ServerRequest.class); GrpcAgentStatHandlerV2 handler = createMockHandler(mockAgentUriStatService, false); @@ -80,32 +83,28 @@ public void throwExceptionTest() { @Test public void skipTest() { - AgentUriStatService mockAgentUriStatService = Mockito.mock(AgentUriStatService.class); - - ServerRequest mockServerRequest = Mockito.mock(ServerRequest.class); - Mockito.when(mockServerRequest.getData()).thenReturn(PAgentUriStat.getDefaultInstance()); + AgentUriStatService mockAgentUriStatService = mock(AgentUriStatService.class); + ServerRequest mockServerRequest = mock(ServerRequest.class); + when(mockServerRequest.getData()).thenReturn(PAgentUriStat.getDefaultInstance()); GrpcAgentStatHandlerV2 handler = createMockHandler(mockAgentUriStatService, false); handler.handleSimple(mockServerRequest); - - Mockito.verify(mockAgentUriStatService, never()).save(Mockito.any()); } @Test public void handleTest() { - AgentUriStatService mockAgentUriStatService = Mockito.mock(AgentUriStatService.class); + AgentUriStatService mockAgentUriStatService = mock(AgentUriStatService.class); attachContext(new Header("name", "agentId", "agentName", "applicationName", ServiceType.UNKNOWN.getCode(), System.currentTimeMillis(), Header.SOCKET_ID_NOT_EXIST, new ArrayList<>())); PAgentUriStat pAgentUriStat = createPAgentUriStat(); - ServerRequest mockServerRequest = Mockito.mock(ServerRequest.class); - Mockito.when(mockServerRequest.getData()).thenReturn(pAgentUriStat); + ServerRequest mockServerRequest = mock(ServerRequest.class); + when(mockServerRequest.getData()).thenReturn(pAgentUriStat); GrpcAgentStatHandlerV2 handler = createMockHandler(mockAgentUriStatService, true); handler.handleSimple(mockServerRequest); - Mockito.verify(mockAgentUriStatService).save(Mockito.any()); } private PAgentUriStat createPAgentUriStat() { @@ -136,16 +135,23 @@ private void attachContext(Header header) { private GrpcAgentStatHandlerV2 createMockHandler(AgentUriStatService agentUriStatService, boolean enableUriStat) { - GrpcAgentStatMapper mockAgentStatMapper = Mockito.mock(GrpcAgentStatMapper.class); + GrpcAgentStatMapper mockAgentStatMapper = mock(GrpcAgentStatMapper.class); GrpcAgentStatBatchMapper agentStatBatchMapper = new GrpcAgentStatBatchMapper(mockAgentStatMapper); AgentStatService[] agentStatServices = new AgentStatService[0]; - CollectorConfiguration collectorConfiguration = Mockito.mock(CollectorConfiguration.class); - Mockito.when(collectorConfiguration.isUriStatEnable()).thenReturn(enableUriStat); + AgentMetricHandler statHandler = new AgentMetricHandler(mockAgentStatMapper, agentStatServices); + AgentMetricBatchHandler statBatchHandler = new AgentMetricBatchHandler(agentStatBatchMapper, statHandler); + + + CollectorConfiguration collectorConfiguration = mock(CollectorConfiguration.class); + when(collectorConfiguration.isUriStatEnable()).thenReturn(enableUriStat); + GrpcAgentUriStatMapper grpcAgentUriStatMapper = new GrpcAgentUriStatMapper(); + AgentUriMetricHandler uriHandelr = new AgentUriMetricHandler(collectorConfiguration, grpcAgentUriStatMapper, agentUriStatService); + + List handlers = List.of(statHandler, statBatchHandler, uriHandelr); - return new GrpcAgentStatHandlerV2(mockAgentStatMapper, agentStatBatchMapper, new GrpcAgentUriStatMapper(), - agentStatServices, agentUriStatService, collectorConfiguration); + return new GrpcAgentStatHandlerV2(handlers); } }