Search in sources :

Example 81 with Context

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));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 82 with Context

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());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Meter(com.megaease.easeagent.plugin.api.metric.Meter) RabbitMqPlugin(com.megaease.easeagent.plugin.rabbitmq.RabbitMqPlugin) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Envelope(com.rabbitmq.client.Envelope) RabbitMqConsumerMetric(com.megaease.easeagent.plugin.rabbitmq.RabbitMqConsumerMetric) Test(org.junit.Test)

Example 83 with Context

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());
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) Message(org.springframework.amqp.core.Message) MessageProperties(org.springframework.amqp.core.MessageProperties) Meter(com.megaease.easeagent.plugin.api.metric.Meter) RabbitMqPlugin(com.megaease.easeagent.plugin.rabbitmq.RabbitMqPlugin) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) RabbitMqConsumerMetric(com.megaease.easeagent.plugin.rabbitmq.RabbitMqConsumerMetric) Test(org.junit.Test)

Example 84 with Context

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"));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) TagVerifier(com.megaease.easeagent.mock.plugin.api.utils.TagVerifier) LastJsonReporter(com.megaease.easeagent.mock.report.impl.LastJsonReporter) RedisPlugin(com.megaease.easeagent.plugin.redis.RedisPlugin) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) IPluginConfig(com.megaease.easeagent.plugin.api.config.IPluginConfig) Test(org.junit.Test)

Example 85 with Context

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));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) Span(com.megaease.easeagent.plugin.api.trace.Span) Test(org.junit.Test)

Aggregations

Context (com.megaease.easeagent.plugin.api.Context)122 Test (org.junit.Test)101 MethodInfo (com.megaease.easeagent.plugin.interceptor.MethodInfo)87 ReportSpan (com.megaease.easeagent.plugin.report.tracing.ReportSpan)39 Span (com.megaease.easeagent.plugin.api.trace.Span)32 RequestContext (com.megaease.easeagent.plugin.api.context.RequestContext)13 Scope (com.megaease.easeagent.plugin.api.trace.Scope)13 Message (org.springframework.amqp.core.Message)10 URI (java.net.URI)9 LastJsonReporter (com.megaease.easeagent.mock.report.impl.LastJsonReporter)8 TagVerifier (com.megaease.easeagent.mock.plugin.api.utils.TagVerifier)7 BsonDocument (org.bson.BsonDocument)7 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)7 HttpRequest (com.megaease.easeagent.plugin.tools.trace.HttpRequest)6 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)6 ArrayList (java.util.ArrayList)6 BsonString (org.bson.BsonString)6 Call (okhttp3.Call)5 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)4 MockClientRequest (org.springframework.web.reactive.function.client.MockClientRequest)4