|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.navercorp.pinpoint.collector.handler.grpc.metric; |
| 18 | + |
| 19 | +import com.google.protobuf.GeneratedMessageV3; |
| 20 | +import com.navercorp.pinpoint.collector.handler.grpc.GrpcMetricHandler; |
| 21 | +import com.navercorp.pinpoint.collector.mapper.grpc.stat.GrpcAgentProfilerMetricMapper; |
| 22 | +import com.navercorp.pinpoint.collector.service.AgentStatService; |
| 23 | +import com.navercorp.pinpoint.common.server.bo.stat.ProfilerMetricBo; |
| 24 | +import com.navercorp.pinpoint.grpc.MessageFormatUtils; |
| 25 | +import com.navercorp.pinpoint.grpc.trace.PProfilerMetric; |
| 26 | +import org.apache.logging.log4j.LogManager; |
| 27 | +import org.apache.logging.log4j.Logger; |
| 28 | +import org.springframework.stereotype.Service; |
| 29 | + |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +@Service |
| 33 | +public class AgentProfilerMetricHandler implements GrpcMetricHandler { |
| 34 | + private final Logger logger = LogManager.getLogger(this.getClass()); |
| 35 | + private final GrpcAgentProfilerMetricMapper agentProfilerMetricMapper; |
| 36 | + private final AgentStatService[] agentStatServiceList; |
| 37 | + |
| 38 | + public AgentProfilerMetricHandler(GrpcAgentProfilerMetricMapper agentProfilerMetricMapper, |
| 39 | + AgentStatService[] agentStatServiceList) { |
| 40 | + this.agentProfilerMetricMapper = Objects.requireNonNull(agentProfilerMetricMapper, "agentProfilerMetricMapper"); |
| 41 | + this.agentStatServiceList = Objects.requireNonNull(agentStatServiceList, "agentStatServiceList"); |
| 42 | + |
| 43 | + for (AgentStatService service : this.agentStatServiceList) { |
| 44 | + logger.info("{}:{}", AgentStatService.class.getSimpleName(), service.getClass().getSimpleName()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean accept(GeneratedMessageV3 message) { |
| 50 | + return message instanceof PProfilerMetric; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void handle(GeneratedMessageV3 message) { |
| 55 | + if (logger.isDebugEnabled()) { |
| 56 | + logger.debug("Handle PProfilerMetric={}", MessageFormatUtils.debugLog(message)); |
| 57 | + } |
| 58 | + |
| 59 | + final PProfilerMetric profilerMetric = (PProfilerMetric) message; |
| 60 | + final ProfilerMetricBo profilerMetricBo = this.agentProfilerMetricMapper.map(profilerMetric); |
| 61 | + if (profilerMetricBo == null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + handleProfilerMetric(profilerMetricBo); |
| 66 | + } |
| 67 | + |
| 68 | + private void handleProfilerMetric(ProfilerMetricBo profilerMetricBo) { |
| 69 | + for (AgentStatService agentStatService : agentStatServiceList) { |
| 70 | + try { |
| 71 | + agentStatService.save(profilerMetricBo); |
| 72 | + } catch (Exception e) { |
| 73 | + logger.warn("Failed to handle service={}, AgentStatBo={}", agentStatService, profilerMetricBo, e); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments