Search in sources :

Example 1 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class TraceHelperTest method performSuccess.

@Test
public void performSuccess() {
    BsonDocument bsonDocument = new BsonDocument();
    bsonDocument.put("collection", new BsonString(collection));
    CommandSucceededEvent succeededEvent = new CommandSucceededEvent(this.requestId, this.connectionDescription, this.cmdName, bsonDocument, 10);
    Context context = EaseAgent.getContext();
    TraceHelper.commandStarted(context, this.config, this.startedEvent);
    TraceHelper.commandSucceeded(context, succeededEvent);
    this.assertTrace(true, null);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Test(org.junit.Test)

Example 2 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class MongoBaseTest method before.

@Before
public void before() {
    EaseAgent.initializeContextSupplier.get().clear();
    config = new AutoRefreshPluginConfigImpl();
    IPluginConfig iPluginConfig = mock(IPluginConfig.class);
    when(iPluginConfig.enabled()).thenReturn(true);
    when(iPluginConfig.namespace()).thenReturn("mongodb");
    when(iPluginConfig.domain()).thenReturn("observability");
    config.onChange(null, iPluginConfig);
    Context context = EaseAgent.getContext();
    ContextUtils.setBeginTime(context);
    MockEaseAgent.cleanLastSpan();
    clusterId = new ClusterId("local-cluster");
    serverAddress = new ServerAddress("127.0.0.1", 2020);
    serverId = new ServerId(clusterId, serverAddress);
    this.connectionDescription = new ConnectionDescription(serverId);
    Map<String, Object> map = new HashMap<>();
    map.put("collection", collection);
    BsonDocument bsonDocument = BsonDocument.parse(JsonUtil.toJson(map));
    this.startedEvent = new CommandStartedEvent(this.requestId, this.connectionDescription, this.dbName, this.cmdName, bsonDocument);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ClusterId(com.mongodb.connection.ClusterId) HashMap(java.util.HashMap) ServerAddress(com.mongodb.ServerAddress) ConnectionDescription(com.mongodb.connection.ConnectionDescription) ServerId(com.mongodb.connection.ServerId) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) AutoRefreshPluginConfigImpl(com.megaease.easeagent.plugin.api.config.AutoRefreshPluginConfigImpl) IPluginConfig(com.megaease.easeagent.plugin.api.config.IPluginConfig) Before(org.junit.Before)

Example 3 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class MongoReactiveMetricTest method performSuccess.

@Test
public void performSuccess() {
    Context context = EaseAgent.getContext();
    MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { null, null, null, mock(SingleResultCallback.class) }).build();
    interceptor.before(methodInfo, context);
    BsonDocument bsonDocument = new BsonDocument();
    bsonDocument.put("collection", new BsonString(collection));
    CommandSucceededEvent succeededEvent = new CommandSucceededEvent(this.requestId, this.connectionDescription, this.cmdName, bsonDocument, 10);
    this.listener.commandStarted(startedEvent);
    this.listener.commandSucceeded(succeededEvent);
    MongoInternalConnectionSendAndReceiveAsync4MetricInterceptor.SingleResultCallbackProxy<?> proxy = (MongoInternalConnectionSendAndReceiveAsync4MetricInterceptor.SingleResultCallbackProxy<?>) methodInfo.getArgs()[3];
    proxy.onResult(null, null);
    this.assertMetric(initMetricInterceptor.getMongoMetric().getNameFactory(), initMetricInterceptor.getMongoMetric().getMetricRegistry(), true);
    context.exit(interceptor.getEnterKey(methodInfo, context));
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoInternalConnectionSendAndReceiveAsync4MetricInterceptor(com.megaease.easeagent.plugin.mongodb.interceptor.MongoInternalConnectionSendAndReceiveAsync4MetricInterceptor) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Test(org.junit.Test)

Example 4 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class KafkaProducerDoSendInterceptorTest method testRedirectedTag.

@Test
public void testRedirectedTag() {
    KafkaAbstractConfigConstructInterceptor kafkaAbstractConfigConstructInterceptor = new KafkaAbstractConfigConstructInterceptor();
    KafkaTestUtils.mockRedirect(() -> {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, TestConst.URIS);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        MethodInfo methodInfo = MethodInfo.builder().args(new Object[] { props }).build();
        kafkaAbstractConfigConstructInterceptor.doBefore(methodInfo, EaseAgent.getContext());
        MockKafkaProducer kafkaProducer = new MockKafkaProducer(props);
        kafkaProducer.setEaseAgent$$DynamicField$$Data(props.getProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
        KafkaProducerDoSendInterceptor interceptor = new KafkaProducerDoSendInterceptor();
        Context context = EaseAgent.getContext();
        ProducerRecord record = new ProducerRecord<>(topic, key, value);
        methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
        interceptor.doBefore(methodInfo, context);
        assertTrue(context.currentTracing().hasCurrentSpan());
        finishSpan();
        assertFalse(context.currentTracing().hasCurrentSpan());
        ReportSpan mockSpan = MockEaseAgent.getLastSpan();
        assertEquals(TestConst.REDIRECT_URIS, mockSpan.tag("label.remote"));
    });
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockKafkaProducer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MethodInfo(com.megaease.easeagent.plugin.interceptor.MethodInfo) Properties(java.util.Properties) KafkaAbstractConfigConstructInterceptor(com.megaease.easeagent.plugin.kafka.interceptor.redirect.KafkaAbstractConfigConstructInterceptor) Test(org.junit.Test)

Example 5 with Context

use of com.megaease.easeagent.plugin.api.Context in project easeagent by megaease.

the class KafkaProducerDoSendInterceptorTest method doBefore.

@Test
public void doBefore() {
    KafkaProducerDoSendInterceptor interceptor = new KafkaProducerDoSendInterceptor();
    MockKafkaProducer kafkaProducer = MockKafkaProducer.buildOne();
    Context context = EaseAgent.getContext();
    ProducerRecord record = new ProducerRecord<>(topic, key, value);
    MethodInfo methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    Span span = finishSpan();
    assertFalse(context.currentTracing().hasCurrentSpan());
    ReportSpan mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    checkBaseInfo(mockSpan);
    assertEquals(key, mockSpan.tag(KafkaTags.KAFKA_KEY_TAG));
    record = new ProducerRecord<>(topic, value);
    methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
    context = EaseAgent.getContext();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    span = finishSpan();
    assertFalse(context.currentTracing().hasCurrentSpan());
    mockSpan = MockEaseAgent.getLastSpan();
    SpanTestUtils.sameId(span, mockSpan);
    checkBaseInfo(mockSpan);
    assertNull(mockSpan.tag(KafkaTags.KAFKA_KEY_TAG));
    record = new ProducerRecord<>(topic, value);
    methodInfo = MethodInfo.builder().invoker(kafkaProducer).args(new Object[] { record, null }).build();
    context = EaseAgent.getContext();
    interceptor.doBefore(methodInfo, context);
    assertTrue(context.currentTracing().hasCurrentSpan());
    finishSpan();
    assertFalse(context.currentTracing().hasCurrentSpan());
    assertNotNull(methodInfo.getArgs()[1]);
    assertTrue(methodInfo.getArgs()[1] instanceof TraceCallback);
}
Also used : Context(com.megaease.easeagent.plugin.api.Context) ReportSpan(com.megaease.easeagent.plugin.report.tracing.ReportSpan) MockKafkaProducer(com.megaease.easeagent.plugin.kafka.interceptor.MockKafkaProducer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) 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