use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.
the class RabbitMqChannelPublishInterceptorTest method before.
@Test
public void before() throws UnknownHostException {
RabbitMqChannelPublishInterceptor interceptor = new RabbitMqChannelPublishInterceptor();
Channel channel = mock(Channel.class);
Connection connection = mock(Connection.class);
when(channel.getConnection()).thenReturn(connection);
String host = "127.0.0.1";
int port = 1111;
InetAddress inetAddress = InetAddress.getByName(host);
when(connection.getAddress()).thenReturn(inetAddress);
when(connection.getPort()).thenReturn(port);
MethodInfo methodInfo = MethodInfo.builder().invoker(channel).args(new Object[] { null, null, null, null, null }).build();
Context context = EaseAgent.getContext();
interceptor.before(methodInfo, context);
assertNotNull(methodInfo.getArgs()[4]);
assertEquals(host + ":" + port, context.get(ContextCons.MQ_URI));
}
use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.
the class RabbitMqConsumerMetricInterceptorTest method after.
@Test
public void after() {
RabbitMqConsumerMetricInterceptor interceptor = new RabbitMqConsumerMetricInterceptor();
InterceptorTestUtils.init(interceptor, new RabbitMqPlugin());
String queue = "testRabbitMqConsumerMetricInterceptorConsumerQueue";
Envelope envelope = new Envelope(0, false, "", queue);
MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { null, envelope }).build();
Context context = EaseAgent.getContext();
context.put(START_KEY, System.currentTimeMillis() - 100);
interceptor.after(methodInfo, context);
RabbitMqConsumerMetric metric = AgentFieldReflectAccessor.getStaticFieldValue(RabbitMqConsumerMetricInterceptor.class, "metric");
Meter meter = metric.meter(queue, MetricSubType.CONSUMER);
Meter meterError = metric.meter(queue, MetricSubType.CONSUMER_ERROR);
assertEquals(1, meter.getCount());
assertEquals(0, meterError.getCount());
methodInfo = MethodInfo.builder().args(new Object[] { null, envelope }).throwable(new RuntimeException("testError")).build();
interceptor.after(methodInfo, context);
assertEquals(2, meter.getCount());
assertEquals(1, meterError.getCount());
}
use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.
the class RabbitMqOnMessageMetricInterceptorTest method after.
@Test
public void after() {
RabbitMqOnMessageMetricInterceptor interceptor = new RabbitMqOnMessageMetricInterceptor();
InterceptorTestUtils.init(interceptor, new RabbitMqPlugin());
MessageProperties messageProperties = new MessageProperties();
String queue = "testConsumerQueue";
messageProperties.setConsumerQueue(queue);
String testMqUri = "testMqUri";
messageProperties.setHeader(ContextCons.MQ_URI, testMqUri);
Message message = new Message("testBody".getBytes(), messageProperties);
MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { message }).build();
Context context = EaseAgent.getContext();
context.put(START_KEY, System.currentTimeMillis() - 100);
interceptor.after(methodInfo, context);
RabbitMqConsumerMetric metric = AgentFieldReflectAccessor.getStaticFieldValue(RabbitMqOnMessageMetricInterceptor.class, "metric");
Meter meter = metric.meter(queue, MetricSubType.CONSUMER);
Meter meterError = metric.meter(queue, MetricSubType.CONSUMER_ERROR);
assertEquals(1, meter.getCount());
assertEquals(0, meterError.getCount());
methodInfo = MethodInfo.builder().args(new Object[] { Arrays.asList(message, message) }).build();
interceptor.after(methodInfo, context);
assertEquals(3, meter.getCount());
assertEquals(0, meterError.getCount());
methodInfo = MethodInfo.builder().args(new Object[] { message }).throwable(new RuntimeException("testError")).build();
interceptor.after(methodInfo, context);
assertEquals(4, meter.getCount());
assertEquals(1, meterError.getCount());
methodInfo = MethodInfo.builder().args(new Object[] { Arrays.asList(message, message) }).throwable(new RuntimeException("testError")).build();
interceptor.after(methodInfo, context);
assertEquals(6, meter.getCount());
assertEquals(3, meterError.getCount());
}
use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.
the class CommonRedisMetricInterceptorTest method doAfter.
@Test
public void doAfter() throws InterruptedException {
MockCommonRedisMetricInterceptor commonRedisMetricInterceptor = new MockCommonRedisMetricInterceptor();
Context context = EaseAgent.getContext();
RedisPlugin redisPlugin = new RedisPlugin();
IPluginConfig iPluginConfig = EaseAgent.getConfig(redisPlugin.getDomain(), redisPlugin.getNamespace(), commonRedisMetricInterceptor.getType());
commonRedisMetricInterceptor.init(iPluginConfig, "", "", "");
MethodInfo methodInfo = MethodInfo.builder().invoker("tttt").method("get").build();
TagVerifier tagVerifier = new TagVerifier().add("category", "application").add("type", "cache-redis").add("signature", "test_redis_metric");
LastJsonReporter lastJsonReporter = MockEaseAgent.lastMetricJsonReporter(tagVerifier::verifyAnd);
commonRedisMetricInterceptor.doBefore(methodInfo, context);
commonRedisMetricInterceptor.doAfter(methodInfo, context);
Map<String, Object> metric = getMetric(lastJsonReporter);
assertEquals(1, (int) metric.get("cnt"));
assertEquals(0, (int) metric.get("errcnt"));
methodInfo = MethodInfo.builder().invoker("tttt").method("get").throwable(new RuntimeException("test error")).build();
commonRedisMetricInterceptor.doBefore(methodInfo, context);
commonRedisMetricInterceptor.doAfter(methodInfo, context);
lastJsonReporter.clean();
metric = getMetric(lastJsonReporter);
assertEquals(2, (int) metric.get("cnt"));
assertEquals(1, (int) metric.get("errcnt"));
}
use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.
the class JedisTracingInterceptorTest method doTraceBefore.
@Test
public void doTraceBefore() {
JedisTracingInterceptor jedisTracingInterceptor = new JedisTracingInterceptor();
Context context = EaseAgent.getContext();
MethodInfo methodInfo = MethodInfo.builder().invoker("tttt").method("get").build();
jedisTracingInterceptor.doTraceBefore(methodInfo, context);
Span span = context.remove(CommonRedisTracingInterceptorTest.SPAN_KEY);
span.finish();
ReportSpan mockSpan = Objects.requireNonNull(MockEaseAgent.getLastSpan());
assertEquals("string.get", mockSpan.name());
assertEquals(Span.Kind.CLIENT.name(), mockSpan.kind());
assertEquals("redis", mockSpan.remoteServiceName());
assertEquals(Type.REDIS.getRemoteType(), mockSpan.tag(MiddlewareConstants.TYPE_TAG_NAME));
}
Aggregations