1818
1919package org .apache .skywalking .apm .plugin .mongodb .v3 .interceptor .v37 ;
2020
21- import static org . hamcrest . CoreMatchers . is ;
22- import static org . junit . Assert . assertThat ;
23- import static org . mockito . Mockito . mock ;
24- import static org . mockito . Mockito . when ;
25- import java . lang . reflect . Method ;
26- import java . util . List ;
21+ import com . mongodb . MongoNamespace ;
22+ import com . mongodb . ReadConcern ;
23+ import com . mongodb . client . internal . OperationExecutor ;
24+ import com . mongodb . operation . AggregateOperation ;
25+ import com . mongodb . operation . FindOperation ;
26+ import com . mongodb . operation . WriteOperation ;
2727import org .apache .skywalking .apm .agent .core .context .trace .AbstractTracingSpan ;
2828import org .apache .skywalking .apm .agent .core .context .trace .LogDataEntity ;
2929import org .apache .skywalking .apm .agent .core .context .trace .SpanLayer ;
5050import org .mockito .Mock ;
5151import org .mockito .junit .MockitoJUnit ;
5252import org .mockito .junit .MockitoRule ;
53- import com .mongodb .MongoNamespace ;
54- import com .mongodb .ReadConcern ;
55- import com .mongodb .client .internal .OperationExecutor ;
56- import com .mongodb .operation .FindOperation ;
57- import com .mongodb .operation .WriteOperation ;
53+
54+ import java .lang .reflect .Method ;
55+ import java .util .Collections ;
56+ import java .util .List ;
57+
58+ import static org .hamcrest .CoreMatchers .is ;
59+ import static org .junit .Assert .assertThat ;
60+ import static org .mockito .Mockito .mock ;
61+ import static org .mockito .Mockito .when ;
5862
5963@ RunWith (TracingSegmentRunner .class )
6064public class MongoDBOperationExecutorInterceptorTest {
@@ -75,6 +79,8 @@ public class MongoDBOperationExecutorInterceptorTest {
7579 private Object [] arguments ;
7680
7781 private Class [] argumentTypes ;
82+ private Decoder decoder ;
83+ private MongoNamespace mongoNamespace ;
7884
7985 @ Before
8086 public void setUp () {
@@ -87,11 +93,10 @@ public void setUp() {
8793
8894 BsonDocument document = new BsonDocument ();
8995 document .append ("name" , new BsonString ("by" ));
90- MongoNamespace mongoNamespace = new MongoNamespace ("test.user" );
91- Decoder decoder = mock (Decoder .class );
96+ mongoNamespace = new MongoNamespace ("test.user" );
97+ decoder = mock (Decoder .class );
9298 FindOperation findOperation = new FindOperation (mongoNamespace , decoder );
9399 findOperation .filter (document );
94-
95100 arguments = new Object [] {findOperation };
96101 argumentTypes = new Class [] {findOperation .getClass ()};
97102 }
@@ -104,11 +109,28 @@ public void testIntercept() throws Throwable {
104109 MatcherAssert .assertThat (segmentStorage .getTraceSegments ().size (), is (1 ));
105110 TraceSegment traceSegment = segmentStorage .getTraceSegments ().get (0 );
106111 List <AbstractTracingSpan > spans = SegmentHelper .getSpans (traceSegment );
107- assertRedisSpan (spans .get (0 ));
112+ assertMongoFindOperationSpan (spans .get (0 ));
108113 }
109114
110115 @ Test
111- public void testInterceptWithException () throws Throwable {
116+ public void testAggregateOperationIntercept () throws Throwable {
117+ MongoNamespace mongoNamespace = new MongoNamespace ("test.user" );
118+ BsonDocument matchStage = new BsonDocument ("$match" , new BsonDocument ("name" , new BsonString ("by" )));
119+ List <BsonDocument > pipeline = Collections .singletonList (matchStage );
120+ AggregateOperation <BsonDocument > aggregateOperation = new AggregateOperation (mongoNamespace , pipeline , decoder );
121+ Object [] arguments = {aggregateOperation };
122+ Class [] argumentTypes = {aggregateOperation .getClass ()};
123+ interceptor .beforeMethod (enhancedInstance , getMethod (), arguments , argumentTypes , null );
124+ interceptor .afterMethod (enhancedInstance , getMethod (), arguments , argumentTypes , null );
125+
126+ MatcherAssert .assertThat (segmentStorage .getTraceSegments ().size (), is (1 ));
127+ TraceSegment traceSegment = segmentStorage .getTraceSegments ().get (0 );
128+ List <AbstractTracingSpan > spans = SegmentHelper .getSpans (traceSegment );
129+ assertMongoAggregateOperationSpan (spans .get (0 ));
130+ }
131+
132+ @ Test
133+ public void testInterceptFindOperationWithException () throws Throwable {
112134 interceptor .beforeMethod (enhancedInstance , getMethod (), arguments , argumentTypes , null );
113135 interceptor .handleMethodException (
114136 enhancedInstance , getMethod (), arguments , argumentTypes , new RuntimeException ());
@@ -117,17 +139,29 @@ public void testInterceptWithException() throws Throwable {
117139 MatcherAssert .assertThat (segmentStorage .getTraceSegments ().size (), is (1 ));
118140 TraceSegment traceSegment = segmentStorage .getTraceSegments ().get (0 );
119141 List <AbstractTracingSpan > spans = SegmentHelper .getSpans (traceSegment );
120- assertRedisSpan (spans .get (0 ));
142+ assertMongoFindOperationSpan (spans .get (0 ));
121143 List <LogDataEntity > logDataEntities = SpanHelper .getLogs (spans .get (0 ));
122144 assertThat (logDataEntities .size (), is (1 ));
123145 SpanAssert .assertException (logDataEntities .get (0 ), RuntimeException .class );
124146 }
125147
126- private void assertRedisSpan (AbstractTracingSpan span ) {
148+ private void assertMongoAggregateOperationSpan (AbstractTracingSpan span ) {
149+ assertThat (span .getOperationName (), is ("MongoDB/AggregateOperation" ));
150+ assertThat (SpanHelper .getComponentId (span ), is (42 ));
151+ List <TagValuePair > tags = SpanHelper .getTags (span );
152+ assertThat (tags .get (1 ).getValue (), is ("test.user" ));
153+ assertThat (tags .get (2 ).getValue (), is ("{\" $match\" : {\" name\" : \" by\" }}," ));
154+ assertThat (tags .get (0 ).getValue (), is ("MongoDB" ));
155+ assertThat (span .isExit (), is (true ));
156+ assertThat (SpanHelper .getLayer (span ), CoreMatchers .is (SpanLayer .DB ));
157+ }
158+
159+ private void assertMongoFindOperationSpan (AbstractTracingSpan span ) {
127160 assertThat (span .getOperationName (), is ("MongoDB/FindOperation" ));
128161 assertThat (SpanHelper .getComponentId (span ), is (42 ));
129162 List <TagValuePair > tags = SpanHelper .getTags (span );
130- assertThat (tags .get (1 ).getValue (), is ("{\" name\" : \" by\" }" ));
163+ assertThat (tags .get (1 ).getValue (), is ("test.user" ));
164+ assertThat (tags .get (2 ).getValue (), is ("{\" name\" : \" by\" }" ));
131165 assertThat (tags .get (0 ).getValue (), is ("MongoDB" ));
132166 assertThat (span .isExit (), is (true ));
133167 assertThat (SpanHelper .getLayer (span ), CoreMatchers .is (SpanLayer .DB ));
0 commit comments