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

[#9563] Dynamic loading of GrpcMetricHandler #9564

Merged
merged 1 commit into from
Dec 30, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -46,104 +33,31 @@
@Service
public class GrpcAgentStatHandlerV2 implements SimpleHandler<GeneratedMessageV3> {
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<GrpcMetricHandler> 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<GeneratedMessageV3> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Loading