Skip to content

Commit

Permalink
[#7375] Add Channelz metric for grpc debug
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 7, 2020
1 parent 5032090 commit 709fe74
Show file tree
Hide file tree
Showing 24 changed files with 566 additions and 29 deletions.
10 changes: 8 additions & 2 deletions agent/src/main/resources/pinpoint-root.config
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ profiler.transport.grpc.agent.sender.write.buffer.highwatermark=32M
profiler.transport.grpc.agent.sender.write.buffer.lowwatermark=16M
## AUTO, NIO, EPOLL
profiler.transport.grpc.agent.sender.channel-type=AUTO
profiler.transport.grpc.agent.sender.maxtraceevent=0

# Metadata
profiler.transport.grpc.metadata.collector.ip=${profiler.transport.grpc.collector.ip}
Expand All @@ -53,7 +54,8 @@ profiler.transport.grpc.metadata.sender.flow-control.window.size=1M
profiler.transport.grpc.metadata.sender.write.buffer.highwatermark=32M
profiler.transport.grpc.metadata.sender.write.buffer.lowwatermark=16M
## AUTO, NIO, EPOLL
profiler.transport.grpc.agent.sender.channel-type=AUTO
profiler.transport.grpc.metadata.sender.channel-type=AUTO
profiler.transport.grpc.metadata.sender.maxtraceevent=0

# Stat
profiler.transport.grpc.stat.collector.ip=${profiler.transport.grpc.collector.ip}
Expand All @@ -70,7 +72,8 @@ profiler.transport.grpc.stat.sender.flow-control.window.size=1M
profiler.transport.grpc.stat.sender.write.buffer.highwatermark=32M
profiler.transport.grpc.stat.sender.write.buffer.lowwatermark=16M
## AUTO, NIO, EPOLL
profiler.transport.grpc.agent.sender.channel-type=AUTO
profiler.transport.grpc.stat.sender.channel-type=AUTO
profiler.transport.grpc.stat.sender.maxtraceevent=0

# Span
profiler.transport.grpc.span.collector.ip=${profiler.transport.grpc.collector.ip}
Expand All @@ -90,6 +93,9 @@ profiler.transport.grpc.span.sender.discardpolicy.logger.discard.ratelimit=1
profiler.transport.grpc.span.sender.discardpolicy.maxpendingthreshold=1024
profiler.transport.grpc.span.sender.discardpolicy.discard-count-for-reconnect=1000
profiler.transport.grpc.span.sender.discardpolicy.not-ready-timeout-millis=300000
## AUTO, NIO, EPOLL
profiler.transport.grpc.span.sender.channel-type=AUTO
profiler.transport.grpc.span.sender.maxtraceevent=8

# This configuration enable some function of netty
# Functions are available without the this configuration when using jdk8 and below,
Expand Down
16 changes: 16 additions & 0 deletions agent/src/main/resources/profiles/local/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>

<RollingFile name="statRollingFile" filename="${logging_dir}/pinpoint-stat.log"
filepattern="${logging_dir}/pinpoint-%i.log">
<PatternLayout>
<Pattern>${file_message_pattern}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="${backupsize}" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>

<Loggers>
<Logger name="com.navercorp.pinpoint.metric" level="INFO" additivity="false">
<AppenderRef ref="statRollingFile"/>
<!-- <AppenderRef ref="console"/>-->
<!-- <AppenderRef ref="rollingFile"/>-->
</Logger>
<Logger name="com.navercorp.pinpoint" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
Expand Down
17 changes: 17 additions & 0 deletions agent/src/main/resources/profiles/release/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,26 @@
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>

<RollingFile name="statRollingFile" filename="${logging_dir}/pinpoint-stat.log"
filepattern="${logging_dir}/pinpoint-%i.log">
<PatternLayout>
<Pattern>${file_message_pattern}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="${backupsize}" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>

<Loggers>
<Logger name="com.navercorp.pinpoint.metric" level="INFO" additivity="false">
<AppenderRef ref="statRollingFile"/>
<!-- <AppenderRef ref="console"/>-->
<!-- <AppenderRef ref="rollingFile"/>-->
</Logger>

<Logger name="com.navercorp.pinpoint" level="INFO" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingFile"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.navercorp.pinpoint.grpc;

import io.grpc.Channel;
import io.grpc.InternalWithLogId;
import io.grpc.ManagedChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -54,4 +56,29 @@ public static boolean shutdownManagedChannel(String name, ManagedChannel managed
return false;
}
}

private static final String LOG_ID_STR = "logId=";

public static long getLogId(Channel channel) {
if (channel == null) {
return -1;
}
if (channel instanceof InternalWithLogId) {
InternalWithLogId logId = (InternalWithLogId) channel;
return logId.getLogId().getId();
}

final String channelString = channel.toString();
final int start = channelString.indexOf(LOG_ID_STR);
if (start == -1) {
return -1;
}
final int end = channelString.indexOf(',', start + LOG_ID_STR.length());
if (end == -1) {
return -1;
}
final String logId = channelString.substring(start + LOG_ID_STR.length(), end);
return Long.parseLong(logId);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ClientOption {
public static final int DEFAULT_WRITE_BUFFER_HIGH_WATER_MARK = 32 * 1024 * 1024;
public static final int DEFAULT_WRITE_BUFFER_LOW_WATER_MARK = 16 * 1024 * 1024;
public static final String DEFAULT_CHANNEL_TYPE = ChannelTypeEnum.AUTO.name();
public static final int DEFAULT_MAX_TRACE_EVENT = 0;

private final long keepAliveTime;
private final long keepAliveTimeout;
Expand All @@ -54,10 +55,11 @@ public class ClientOption {
private final int writeBufferHighWaterMark;
private final int writeBufferLowWaterMark;
private final ChannelTypeEnum channelTypeEnum;
private final int maxTraceEvent;

private ClientOption(long keepAliveTime, long keepAliveTimeout, int maxHeaderListSize, int maxInboundMessageSize,
int flowControlWindow, int connectTimeout, int writeBufferHighWaterMark, int writeBufferLowWaterMark,
ChannelTypeEnum channelTypeEnum) {
ChannelTypeEnum channelTypeEnum, int maxTraceEvent) {
this.keepAliveTime = keepAliveTime;
this.keepAliveTimeout = keepAliveTimeout;
this.flowControlWindow = flowControlWindow;
Expand All @@ -68,6 +70,7 @@ private ClientOption(long keepAliveTime, long keepAliveTimeout, int maxHeaderLis
this.writeBufferLowWaterMark = writeBufferLowWaterMark;

this.channelTypeEnum = Assert.requireNonNull(channelTypeEnum, "channelTypeEnum");
this.maxTraceEvent = maxTraceEvent;
}

public int getFlowControlWindow() {
Expand Down Expand Up @@ -113,6 +116,9 @@ public int getWriteBufferLowWaterMark() {
public ChannelTypeEnum getChannelTypeEnum() {
return channelTypeEnum;
}
public int getMaxTraceEvent() {
return maxTraceEvent;
}

@Override
public String toString() {
Expand All @@ -128,6 +134,7 @@ public String toString() {
sb.append(", writeBufferHighWaterMark=").append(writeBufferHighWaterMark);
sb.append(", writeBufferLowWaterMark=").append(writeBufferLowWaterMark);
sb.append(", channelTypeEnum=").append(channelTypeEnum);
sb.append(", maxTraceEvent=").append(maxTraceEvent);
sb.append('}');

return sb.toString();
Expand All @@ -145,11 +152,12 @@ public static class Builder {
private int writeBufferHighWaterMark = DEFAULT_WRITE_BUFFER_HIGH_WATER_MARK;
private int writeBufferLowWaterMark = DEFAULT_WRITE_BUFFER_LOW_WATER_MARK;
private ChannelTypeEnum channelTypeEnum = ChannelTypeEnum.valueOf(DEFAULT_CHANNEL_TYPE);
private int maxTraceEvent;

public ClientOption build() {
final ClientOption clientOption = new ClientOption(keepAliveTime, keepAliveTimeout, maxHeaderListSize, maxInboundMessageSize,
flowControlWindow, connectTimeout,
writeBufferHighWaterMark, writeBufferLowWaterMark, channelTypeEnum);
writeBufferHighWaterMark, writeBufferLowWaterMark, channelTypeEnum, maxTraceEvent);
return clientOption;
}

Expand Down Expand Up @@ -200,6 +208,11 @@ public void setChannelTypeEnum(String channelTypeEnum) {
this.channelTypeEnum = ChannelTypeEnum.valueOf(channelTypeEnum);
}

public void setMaxTraceEvent(int maxTraceEvent) {
Assert.isTrue(maxTraceEvent >= 0, "maxTraceEvent must be positive");
this.maxTraceEvent = maxTraceEvent;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Builder{");
Expand All @@ -212,6 +225,7 @@ public String toString() {
sb.append(", writeBufferHighWaterMark=").append(writeBufferHighWaterMark);
sb.append(", writeBufferLowWaterMark=").append(writeBufferLowWaterMark);
sb.append(", channelTypeEnum=").append(channelTypeEnum);
sb.append(", maxTraceEvent=").append(maxTraceEvent);
sb.append('}');
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.navercorp.pinpoint.common.profiler.concurrent.PinpointThreadFactory;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.grpc.ChannelTypeEnum;
import com.navercorp.pinpoint.grpc.ExecutorUtils;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannel;
Expand Down Expand Up @@ -144,15 +143,20 @@ public ManagedChannel build(String channelName, String host, int port) {
}
setupClientOption(channelBuilder);

channelBuilder.maxTraceEvents(clientOption.getMaxTraceEvent());

final ManagedChannel channel = channelBuilder.build();

return channel;
}

private void setupInternal(NettyChannelBuilder channelBuilder) {
InternalNettyChannelBuilder.setStatsEnabled(channelBuilder, false);
InternalNettyChannelBuilder.setTracingEnabled(channelBuilder, false);

InternalNettyChannelBuilder.setStatsEnabled(channelBuilder, false);
InternalNettyChannelBuilder.setStatsRecordStartedRpcs(channelBuilder, false);
InternalNettyChannelBuilder.setStatsRecordFinishedRpcs(channelBuilder, false);
InternalNettyChannelBuilder.setStatsRecordRealTimeMetrics(channelBuilder, false);
}

private void addHeader(NettyChannelBuilder channelBuilder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.navercorp.pinpoint.grpc;

import io.grpc.Channel;
import org.junit.Assert;
import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ManagedChannelUtilsTest {
@Test
public void testGetLogId() {
Channel channel = mock(Channel.class);
when(channel.toString()).thenReturn("ManagedChannelOrphanWrapper{delegate=ManagedChannelImpl{logId=1, target=127.0.0.1:9993}}");

Assert.assertEquals(1, ManagedChannelUtils.getLogId(channel));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.navercorp.pinpoint.plugin.grpc;

import io.grpc.ManagedChannelBuilder;

import java.lang.reflect.Method;

public class BuilderUtils {

private interface Call<T> {
void call(T target);
}

private static Call<ManagedChannelBuilder<?>> usePlaintext = getUsePlainText();

public static void usePlainText(ManagedChannelBuilder<?> builder) {
usePlaintext.call(builder);
}

private static Call getUsePlainText() {
final Class<ManagedChannelBuilder> builderClass = ManagedChannelBuilder.class;

final Method oldUsePlaintext = getMethod(builderClass, "usePlaintext");
if (oldUsePlaintext != null) {
return new Call<ManagedChannelBuilder<?>>() {
@Override
public void call(ManagedChannelBuilder<?> target) {
try {
oldUsePlaintext.invoke(target);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
};
}

final Method newUsePlaintext = getMethod(builderClass, "usePlaintext", boolean.class);
if (newUsePlaintext != null) {
return new Call<ManagedChannelBuilder<?>>() {
@Override
public void call(ManagedChannelBuilder<?> target) {
try {
newUsePlaintext.invoke(target, true);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
};
}

throw new UnsupportedOperationException("usePlaintext method not found");
}

private static Method getMethod(Class<ManagedChannelBuilder> builderClass, String method, Class<?>... parameterTypes) {
try {
return builderClass.getMethod(method, parameterTypes);
} catch (NoSuchMethodException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.navercorp.pinpoint.plugin.grpc;

import io.grpc.ManagedChannelBuilder;
import org.junit.Test;

import static org.mockito.Mockito.mock;

public class BuilderUtilsTest {
@Test
public void usePlainText() {
ManagedChannelBuilder mock = mock(ManagedChannelBuilder.class);
BuilderUtils.usePlainText(mock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.stub.MetadataUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
Expand All @@ -46,15 +48,17 @@ public class HelloWorldSimpleClient implements HelloWorldClient {
*/
@SuppressWarnings("deprecated")
public HelloWorldSimpleClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
// .usePlaintext() // no API in old version
.usePlaintext(true)
.intercept(MetadataUtils.newCaptureMetadataInterceptor(new AtomicReference<Metadata>(), new AtomicReference<Metadata>()))
.build());
this(newChannel(host, port));
}

private static ManagedChannel newChannel(String host, int port) {
ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(host, port);
BuilderUtils.usePlainText(builder);
builder.intercept(MetadataUtils.newCaptureMetadataInterceptor(new AtomicReference<Metadata>(), new AtomicReference<Metadata>()));
return builder.build();
}


/**
* Construct client for accessing HelloWorld server using the existing channel.
*/
Expand Down
Loading

0 comments on commit 709fe74

Please sign in to comment.