99
1010package org .elasticsearch .gradle .testclusters ;
1111
12+ import com .fasterxml .jackson .databind .JsonNode ;
1213import com .fasterxml .jackson .databind .ObjectMapper ;
1314import com .fasterxml .jackson .databind .node .ObjectNode ;
15+ import com .fasterxml .jackson .databind .util .LRUMap ;
16+ import com .fasterxml .jackson .databind .util .LookupCache ;
1417import com .sun .net .httpserver .HttpExchange ;
1518import com .sun .net .httpserver .HttpHandler ;
1619import com .sun .net .httpserver .HttpServer ;
2831import java .io .InputStreamReader ;
2932import java .io .OutputStream ;
3033import java .net .InetSocketAddress ;
34+ import java .util .ArrayList ;
3135import java .util .Arrays ;
36+ import java .util .List ;
3237import java .util .regex .Pattern ;
3338import java .util .stream .Collectors ;
3439
4853public class MockApmServer {
4954 private static final Logger logger = Logging .getLogger (MockApmServer .class );
5055 private static final org .slf4j .Logger log = LoggerFactory .getLogger (MockApmServer .class );
56+ private static final LookupCache <String , String > transactionCache = new LRUMap (16 , 16 );
57+
5158 private final Pattern metricFilter ;
5259 private final Pattern transactionFilter ;
5360 private final Pattern transactionExcludesFilter ;
@@ -136,22 +143,28 @@ private void logFiltered(InputStream body) throws IOException {
136143 ObjectMapper mapper = new ObjectMapper ();
137144 try (BufferedReader reader = new BufferedReader (new InputStreamReader (body ))) {
138145 String line ;
139- String tier = null ;
140- String node = null ;
146+ String nodeMetadata = null ;
147+
148+ List <JsonNode > spans = new ArrayList <>();
141149
142150 while ((line = reader .readLine ()) != null ) {
143151 var jsonNode = mapper .readTree (line );
144152
145153 if (jsonNode .has ("metadata" )) {
146- node = jsonNode .path ("metadata" ).path ("service" ).path ("node" ).path ("configured_name" ).asText (null );
147- tier = jsonNode .path ("metadata" ).path ("labels" ).path ("node_tier" ).asText (null );
154+ nodeMetadata = jsonNode .path ("metadata" ).path ("service" ).path ("node" ).path ("configured_name" ).asText (null );
155+ var tier = jsonNode .path ("metadata" ).path ("labels" ).path ("node_tier" ).asText (null );
156+ nodeMetadata += tier != null ? "/" + tier : "" ;
157+
148158 } else if (transactionFilter != null && jsonNode .has ("transaction" )) {
149159 var transaction = jsonNode .get ("transaction" );
150160 var name = transaction .get ("name" ).asText ();
151161 if (transactionFilter .matcher (name ).matches ()
152162 && (transactionExcludesFilter == null || transactionExcludesFilter .matcher (name ).matches () == false )) {
153- logger .lifecycle ("Transaction [{}/{}]: {}" , node , tier , transaction );
163+ transactionCache .put (transaction .get ("id" ).asText (), name );
164+ logger .lifecycle ("Transaction {} [{}]: {}" , name , nodeMetadata , transaction );
154165 }
166+ } else if (jsonNode .has ("span" )) {
167+ spans .add (jsonNode .get ("span" )); // make sure to record all transactions first
155168 } else if (metricFilter != null && jsonNode .has ("metricset" )) {
156169 var metricset = jsonNode .get ("metricset" );
157170 var samples = (ObjectNode ) metricset .get ("samples" );
@@ -161,10 +174,20 @@ private void logFiltered(InputStream body) throws IOException {
161174 }
162175 }
163176 if (samples .isEmpty () == false ) {
164- logger .lifecycle ("Metricset [{}/{}] " , node , tier , metricset );
177+ logger .lifecycle ("Metricset [{}]: {} " , nodeMetadata , metricset );
165178 }
166179 }
167180 }
181+
182+ // emit only spans for previously matched transactions using the transaction cache
183+ for (var span : spans ) {
184+ var name = span .get ("name" ).asText ();
185+ var transactionId = span .get ("transaction_id" ).asText ();
186+ var transactionName = transactionCache .get (transactionId );
187+ if (transactionName != null ) {
188+ logger .lifecycle ("Span {} of {} [{}]: {}" , name , transactionName , nodeMetadata , span );
189+ }
190+ }
168191 }
169192 }
170193 }
0 commit comments