Skip to content

Commit

Permalink
[pinpoint-apm#7375] Add Channelz monitoring API for Collector
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 9, 2020
1 parent 08bbd49 commit 9f221fc
Show file tree
Hide file tree
Showing 24 changed files with 1,297 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.navercorp.pinpoint.collector.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.channelz.ChannelzRegistry;
import com.navercorp.pinpoint.grpc.channelz.ChannelzUtils;
import io.grpc.InternalChannelz;
import io.grpc.InternalInstrumented;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/channelz")
public class ChannelzController {

private final ChannelzRegistry channelzRegistry;
private final InternalChannelz channelz = InternalChannelz.instance();
private final ObjectMapper mapper;

@Autowired
public ChannelzController(ChannelzRegistry channelzRegistry, ObjectMapper objectMapper) {
this.channelzRegistry = Assert.requireNonNull(channelzRegistry, "channelzRegistry");
this.mapper = Assert.requireNonNull(objectMapper, "objectMapper");
}

@RequestMapping("/getSocket")
@ResponseBody
public String getSocket(long logId) throws JsonProcessingException {
InternalChannelz.SocketStats stats = getSocket0(logId);

return mapper.writeValueAsString(stats);
}

@RequestMapping("/html/getSocket")
@ResponseBody
public String getSocketToHtml(long logId) {
InternalChannelz.SocketStats stats = getSocket0(logId);

return new HTMLBuilder().build(stats);
}

private InternalChannelz.SocketStats getSocket0(long logId) {
InternalInstrumented<InternalChannelz.SocketStats> socket = channelz.getSocket(logId);
if (socket == null) {
return null;
}
return ChannelzUtils.getResult("Socket", socket);
}

@RequestMapping("/findSocket")
@ResponseBody
public String findSocket(String remoteAddress, int localPort) throws JsonProcessingException {
String target = remoteAddress + ":" + localPort;

InternalChannelz.SocketStats stats = findSocket(target);
if (stats == null) {
return notFound("remoteAddress:" + remoteAddress +" localPort:" + localPort);
}

return mapper.writeValueAsString(stats);
}

@RequestMapping("/html/findSocket")
@ResponseBody
public String findSocketStatToHtml(String remoteAddress, int localPort) {
String target = remoteAddress + ":" + localPort;

InternalChannelz.SocketStats stats = findSocket(target);
if (stats == null) {
return notFound("remoteAddress:" + remoteAddress +" localPort:" + localPort);
}

return new HTMLBuilder().build(stats);
}

private InternalChannelz.SocketStats findSocket(String targetAddress) {
Long logId = channelzRegistry.getSocketLogId(targetAddress);
InternalInstrumented<InternalChannelz.SocketStats> socket = channelz.getSocket(logId);
if (socket == null) {
return null;
}
return ChannelzUtils.getResult("Socket", socket);
}

@RequestMapping("/html/getServer")
@ResponseBody
public String getServerStatToHtml(String serverName) {
List<InternalChannelz.ServerStats> stats = getServer(serverName);
if (stats == null) {
return notFound("serverName=" + serverName);
}
StringBuilder buffer = new StringBuilder();
for (InternalChannelz.ServerStats stat : stats) {
String html = new HTMLBuilder().build(stat);
buffer.append(html);

buffer.append("<br>");
}
return buffer.toString();
}

@RequestMapping("/html/getSpanReceiver")
@ResponseBody
public String getSpanReceiverl() {
return getServerStatToHtml("grpcSpanReceiver");
}


private List<InternalChannelz.ServerStats> getServer(String serverName) {
Long logId = channelzRegistry.getServerLogId(serverName);

InternalChannelz.ServerList serverList = channelz.getServers(logId, 10000);

List<InternalChannelz.ServerStats> serverStats = ChannelzUtils.getResults("ServerStats", serverList.servers);
return serverStats;
}



private String notFound(String target) {
return target + " not Found";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.navercorp.pinpoint.collector.controller;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* source : http://scrumbucket.org/converting-a-pojo-into-html/
*/
public class HTMLBuilder {


public static class HTMLStyle extends ToStringStyle {

public static final String[] toStringClass = {"java.lang", "java.net"};

public HTMLStyle() {
setFieldSeparator("</td></tr>" + System.lineSeparator() + "<tr><td>");

setContentStart("<table border=\"1\" style=\"border-collapse:collapse\">" + System.lineSeparator() +
"<thead><tr><th>Field</th><th>Data</th></tr></thead>" +
"<tbody><tr><td>");

setFieldNameValueSeparator("</td><td>");

setContentEnd("</td></tr>" + System.lineSeparator() + "</tbody></table>");

setArrayContentDetail(true);
setUseShortClassName(true);
setUseClassName(false);
setUseIdentityHashCode(false);

}

@Override
public void appendDetail(StringBuffer buffer, String fieldName, Object value) {
for (String prefix : toStringClass) {
if (value.getClass().getName().startsWith(prefix)) {
super.appendDetail(buffer, fieldName, value);
return;
}
}
buffer.append(ReflectionToStringBuilder.toString(value, this));
}

@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
super.append(buffer, fieldName, value, fullDetail);
}

@Override
protected void appendDetail(StringBuffer buffer, String fieldName, Map<?, ?> map) {
if (fieldName.equals("others")) {

List<? extends Map.Entry<?, ?>> list = sort(map);
appendContentStart(buffer);
for (Map.Entry<?, ?> entry : list) {
String key = (String) entry.getKey();
Object value = entry.getValue().toString();

append(buffer, key, value, true);
}
removeLastFieldSeparator(buffer);
appendContentEnd(buffer);
return;
}
super.appendDetail(buffer, fieldName, map);
}

private List<? extends Map.Entry<?, ?>> sort(Map<?, ?> map) {
Stream<? extends Map.Entry<?, ?>> stream = map.entrySet().stream();
return stream.sorted(Comparator.comparing(anotherString -> (String) anotherString.getKey()))
.collect(Collectors.toList());
}
}

public String build(Object object) {
ReflectionToStringBuilder builder = new ReflectionToStringBuilder(object, new HTMLStyle());
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.common.server.util.AddressFilter;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.grpc.channelz.ChannelzRegistry;
import com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter;
import com.navercorp.pinpoint.grpc.server.ServerFactory;
import com.navercorp.pinpoint.grpc.server.ServerOption;
Expand Down Expand Up @@ -69,6 +70,7 @@ public class GrpcReceiver implements InitializingBean, DisposableBean, BeanNameA
private ServerOption serverOption;

private Server server;
private ChannelzRegistry channelzRegistry;


@Override
Expand Down Expand Up @@ -107,6 +109,9 @@ public void afterPropertiesSet() throws Exception {
this.serverFactory.addInterceptor(serverInterceptor);
}
}
if (channelzRegistry != null) {
this.serverFactory.setChannelzRegistry(channelzRegistry);
}

// Add service
addService();
Expand Down Expand Up @@ -198,13 +203,20 @@ public void setServerOption(ServerOption serverOption) {
this.serverOption = serverOption;
}

private static final Class<?>[] BINDABLESERVICE_TYPE = {BindableService.class, ServerServiceDefinition.class};

private static boolean supportType(Object service) {
for (Class<?> bindableService : BINDABLESERVICE_TYPE) {
if (bindableService.isInstance(service)) {
return true;
}
}
return false;
}

public void setBindableServiceList(List<Object> serviceList) {
for (Object service : serviceList) {
if (service instanceof BindableService) {
//
} else if (service instanceof ServerServiceDefinition) {
//
} else {
if (!supportType(service)) {
throw new IllegalStateException("unsupported type " + service);
}
}
Expand All @@ -220,4 +232,8 @@ public void setServerInterceptorList(List<ServerInterceptor> serverInterceptorLi
this.serverInterceptorList = serverInterceptorList;
}

public void setChannelzRegistry(ChannelzRegistry channelzRegistry) {
this.channelzRegistry = Assert.requireNonNull(channelzRegistry, "channelzRegistry");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
<ref bean="headerPropagationInterceptor"/>
</util:list>

<util:list id="serverTransportFilterList" value-type="io.grpc.ServerTransportFilter">
<bean class="com.navercorp.pinpoint.grpc.channelz.ChannelzServerTransportFilter">
<constructor-arg ref="channelzRegistry"/>
</bean>
</util:list>

<bean id="headerRader" class="com.navercorp.pinpoint.grpc.server.AgentHeaderReader"/>

<bean id="headerPropagationInterceptor" class="com.navercorp.pinpoint.grpc.server.HeaderPropagationInterceptor">
Expand Down Expand Up @@ -201,6 +207,8 @@
<property name="addressFilter" ref="addressFilter"/>
<property name="bindableServiceList" ref="spanServiceList"/>
<property name="serverInterceptorList" ref="interceptorList"/>
<property name="transportFilterList" ref="serverTransportFilterList"/>
<property name="channelzRegistry" ref="channelzRegistry"/>
<property name="executor" ref="grpcSpanServerExecutor"/>
<property name="enable" value="#{grpcSpanReceiverConfig.isGrpcEnable()}"/>
<property name="serverOption" value="#{grpcSpanReceiverConfig.getGrpcServerOption()}"/>
Expand Down
3 changes: 3 additions & 0 deletions collector/src/main/resources/applicationContext-collector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<bean id="collectorMetric" class="com.navercorp.pinpoint.collector.monitor.CollectorMetric">
</bean>

<bean id="channelzRegistry" class="com.navercorp.pinpoint.grpc.channelz.ChannelzRegistry">
</bean>

<!-- Manager-related Beans -->
<bean id="jmxCollectorManager" class="com.navercorp.pinpoint.collector.manage.jmx.JMXCollectorManager">
</bean>
Expand Down
18 changes: 13 additions & 5 deletions collector/src/main/resources/profiles/local/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
</Appenders>

<Loggers>
<Logger name="com.navercorp.pinpoint" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
<Logger name="com.navercorp.pinpoint" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Logger>

<Logger name="com.navercorp.pinpoint.collector.handler" level="INFO" additivity="false">
Expand All @@ -75,6 +75,14 @@
<AppenderRef ref="console"/>
<Appenderref ref="rollingFile"/>
</Logger>
<Logger name="io.grpc" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
<Appenderref ref="rollingFile"/>
</Logger>
<Logger name="io.netty" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
<Appenderref ref="rollingFile"/>
</Logger>

<Logger name="org.apache.hadoop.hbase" level="INFO" additivity="false">
<AppenderRef ref="console"/>
Expand All @@ -87,9 +95,9 @@
<!-- <Appender-ref ref="console"/>-->
</Logger>

<Root level="info">
<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Root>
</Loggers>
</Configuration>
</Configuration>
10 changes: 9 additions & 1 deletion collector/src/main/resources/profiles/release/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
<AppenderRef ref="console"/>
<Appenderref ref="rollingFile"/>
</Logger>
<Logger name="io.grpc" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<Appenderref ref="rollingFile"/>
</Logger>
<Logger name="io.netty" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<Appenderref ref="rollingFile"/>
</Logger>

<Logger name="org.apache.hadoop.hbase" level="INFO" additivity="false">
<AppenderRef ref="console"/>
Expand All @@ -87,7 +95,7 @@
<!--<appender-ref ref="console"/>-->
</Logger>

<Root level="info">
<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
</Root>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ private void assertHandleMessage(GrpcCommandService commandService, TransportMet
}

private TransportMetadata createTransportMetaData(InetSocketAddress remoteAddress, long transportId) {
return new DefaultTransportMetadata(this.getClass().getSimpleName(), remoteAddress, transportId, System.currentTimeMillis());
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("127.0.0.1", 1111);
return new DefaultTransportMetadata(this.getClass().getSimpleName(), remoteAddress, localAddress, transportId, System.currentTimeMillis(), -1L);
}

private void attachContext(TransportMetadata transportMetadata) {
Expand Down
Loading

0 comments on commit 9f221fc

Please sign in to comment.