Open Kilda Java Documentation
LoggerContextInitializer.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm.topology.utils;
17 
20 
21 import org.apache.storm.tuple.Tuple;
22 import org.aspectj.lang.ProceedingJoinPoint;
23 import org.aspectj.lang.annotation.Around;
24 import org.aspectj.lang.annotation.Aspect;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.MDC;
28 
32 @Aspect
34  private static final Logger LOGGER = LoggerFactory.getLogger(LoggerContextInitializer.class);
35 
36  @Around("execution(* org.apache.storm.topology.IRichBolt+.execute(org.apache.storm.tuple.Tuple)) && args(input)"
37  + "|| execution(* org.apache.storm.topology.IStatefulBolt+.execute(org.apache.storm.tuple.Tuple)) && args(input)")
38  public Object aroundAdvice(ProceedingJoinPoint joinPoint, Tuple input) throws Throwable {
39  String correlationId = CorrelationContext.extractFrom(input)
41  .orElseGet(() -> {
42  LOGGER.debug("CorrelationId was not sent or can't be extracted for tuple {}", input);
43  return DEFAULT_CORRELATION_ID;
44  });
45 
46  // putClosable is not available in SLF4J 1.6, so let's save and restore the previous state.
47  String previousCorrelationId = MDC.get(CORRELATION_ID);
48  MDC.put(CORRELATION_ID, correlationId);
49 
50  try {
51  return joinPoint.proceed(joinPoint.getArgs());
52  } finally {
53  if (previousCorrelationId != null) {
54  MDC.put(CORRELATION_ID, previousCorrelationId);
55  } else {
56  MDC.remove(CORRELATION_ID);
57  }
58  }
59  }
60 }
Object aroundAdvice(ProceedingJoinPoint joinPoint, Tuple input)
static Optional< CorrelationContext > extractFrom(Tuple input)
static final String DEFAULT_CORRELATION_ID
Definition: Utils.java:69
static final String CORRELATION_ID
Definition: Utils.java:43