Add Spring Boot Profile to Sleuth/Zipkin logs - spring-boot

I'm using these dependencies:
compile 'org.springframework.cloud:spring-cloud-starter-zipkin'
compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
compile 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
Is there a possibility to add the current active profile(s) to each log line? This would make it possible to filter logs based on the profiles in Splunk/ELK/...
So instead of
2017-03-13 13:38:30.465 INFO [app,,,] 19220 --- [ main] com.company.app.Application : Started Application in 20.682 seconds (JVM running for 22.166)
it should log
2017-03-13 13:38:30.465 INFO [app,,,] [dev] 19220 --- [ main] com.company.app.Application : Started Application in 20.682 seconds (JVM running for 22.166)
EDIT:
Based on Marcin's answer, I implemented it as follows:
application.yml
logging:
pattern:
level: "%X{profiles} %5p"
ProfileLogger.java
public class ProfileLogger implements SpanLogger {
private final Environment environment;
private final Logger log;
private final Pattern nameSkipPattern;
#Autowired
public ProfileLogger(String nameSkipPattern, final Environment environment) {
this.nameSkipPattern = Pattern.compile(nameSkipPattern);
this.environment = environment;
this.log = org.slf4j.LoggerFactory.getLogger(this.getClass());
}
private void setProfiles() {
MDC.put("profiles", Arrays.toString(environment.getActiveProfiles()));
}
#Override
public void logStartedSpan(Span parent, Span span) {
setProfiles();
...
}
... // (as https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jSpanLogger.java)
}
LogConfig.java
#Configuration
public class LogConfig {
private final Environment environment;
#Autowired
public LogConfig(final Environment environment) {
this.environment = environment;
}
#Bean
SpanLogger getLogger() {
return new ProfileLogger("", environment);
}
}
This prints logs like the following:
2017-03-13 14:47:02.796 INFO 22481 --- [ main] com.company.app.Application : Started Application in 16.115 seconds (JVM running for 16.792)
2017-03-13 14:47:32.684 [localhost, swagger] TRACE 22481 --- [pool-2-thread-1] c.c.app.config.ProfileLogger : Starting span: [Trace: bfcdd2ce866efbff, Span: bfcdd2ce866efbff, Parent: null, exportable:true]
This is already good but not completely what I'm looking for yet.
I'd like to add the profile from the beginning -> even the "Started Application" should contain the profile - if possible. Secondly, I'd like to move the profiles between INFO and 22481.
One more question came up during implementation: In the linked implementation there is this statement:
if (this.log.isTraceEnabled()) {
this.log.trace(text, span);
}
does that mean you only send traces if log-level is set to TRACE? If so, how could I improve logging to stdout with that approach (given a log-level of debug/info/warn)? I think the log-pattern is overriden by Sleuth/Zipkin upon importing the dependencies and thus, local logging looks the same as tracing. Eventually I'm interested in having the profile displayed in local stdout as well as in Zipkin.
EDIT 2: With the help of Marcin, I have changed the pattern by introducing a resources/logback-spring.xml file containing these lines:
<springProperty scope="context" name="activeSpringProfiles" source="spring.profiles.active"/>
<!-- Example for logging into the build folder of your project -->
<property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${activeSpringProfiles:-}"/>​
<!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) [${activeSpringProfiles:-}] %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
Note that you have to add a bootstrap.yml file too in order to have the application name correctly displayed. Without a bootstrap.yml file, the above log-pattern just prints "bootstrap" as application name.
The bootstrap.yml just contains
spring:
application:
name: app
in my case. Everything else is configured in application-[profile].yml
Now everything works as desired:
2017-03-13 15:58:21.291 INFO [app,,,] [localhost,swagger] 27519 --- [ main] com.company.app.keyserver.Application : Started Application in 17.565 seconds (JVM running for 18.232)

Of course - you have to just provide your own logging format (e.g. via logging.pattern.level - check https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html for more info). Then you have to register your own SpanLogger bean implementation where you will take care of adding the value of a spring profile via MDC (you can take a look at this as an example https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/log/Slf4jSpanLogger.java )
UPDATE:
There's another solution for more complex approach that seems much easier than rewriting Sleuth classes. You can try the logback-spring.xml way like here - https://github.com/spring-cloud-samples/sleuth-documentation-apps/blob/master/service1/src/main/resources/logback-spring.xml#L5-L11 . I'm resolving the application name there so maybe you could do the same with active profiles and won't need to write any code?

Related

Field properties in org.springframework.cloud.netflix.turbine.stream.TurbineStreamAutoConfiguration required a bean of type

I went through links like: Spring Boot + Eureka Server + Hystrix with Turbine: empty turbine.stream, but still did not worked for me. This question is continuation of Unable to connect to Command Metric Stream. in Hystrix Dashboard issue.
My source code: https://github.com/javaHelper/spring-cloud-cordinating-services/tree/master/Protecting-Systems-with-Circuit-Breakers
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-15 10:46:04.141 ERROR 4380 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field properties in org.springframework.cloud.netflix.turbine.stream.TurbineStreamAutoConfiguration required a bean of type 'org.springframework.cloud.netflix.turbine.stream.TurbineStreamProperties' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.cloud.netflix.turbine.stream.TurbineStreamProperties' in your configuration.
Simply trying to start the
turbine::
TurbineApplication.java
#SpringBootApplication
#EnableTurbine
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
application.properties
server.port=3000
spring.application.name=turbine-aggregator
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
turbine.app-config=weather-app,datetime-app
turbine.cluster-name-expression=new String("default")
remove the turbine stream dependencies from the pom.xml
That should resolve the problem.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine-stream</artifactId>
</dependency>
Maybe you need to configure Spring to enable configuration properties for turbine?
#Configuration
#EnableConfigurationProperties(TurbineStreamProperties.class)
public class TurbineConfig {
}

Is it possible to enforce message order on ActiveMQ topics using Spring Boot and JmsTemplate?

In playing around with Spring Boot, ActiveMQ, and JmsTemplate, I noticed that it appears that message order is not always preserved. In reading on ActiveMQ, "Message Groups" are offered as a potential solution to preserving message order when sending to a topic. Is there a way to do this with JmsTemplate?
Add Note: I'm starting to think that JmsTemplate is nice for "getting launched", but has too many issues.
Sample code and console output posted below...
#RestController
public class EmptyControllerSB {
#Autowired
MsgSender msgSender;
#RequestMapping(method = RequestMethod.GET, value = { "/v1/msgqueue" })
public String getAccount() {
msgSender.sendJmsMessageA();
msgSender.sendJmsMessageB();
return "Do nothing...successfully!";
}
}
#Component
public class MsgSender {
#Autowired
JmsTemplate jmsTemplate;
void sendJmsMessageA() {
jmsTemplate.convertAndSend(new ActiveMQTopic("VirtualTopic.TEST-TOPIC"), "message A");
}
void sendJmsMessageB() {
jmsTemplate.convertAndSend(new ActiveMQTopic("VirtualTopic.TEST-TOPIC"), "message B");
}
}
#Component
public class MsgReceiver {
private final String consumerOne = "Consumer.myConsumer1.VirtualTopic.TEST-TOPIC";
private final String consumerTwo = "Consumer.myConsumer2.VirtualTopic.TEST-TOPIC";
#JmsListener(destination = consumerOne )
public void receiveMessage1(String strMessage) {
System.out.println("Received on #1a -> " + strMessage);
}
#JmsListener(destination = consumerOne )
public void receiveMessage2(String strMessage) {
System.out.println("Received on #1b -> " + strMessage);
}
#JmsListener(destination = consumerTwo )
public void receiveMessage3(String strMessage) {
System.out.println("Received on #2 -> " + strMessage);
}
}
Here's the console output (note the order of output in first sequence)...
\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\gnupg\bin;C:\Users\LesR\AppData\Local\Microsoft\WindowsApps;c:\Gradle\gradle-5.0\bin;;C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin;;.]
2019-04-03 09:23:08.408 INFO 13936 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-03 09:23:08.408 INFO 13936 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 672 ms
2019-04-03 09:23:08.705 INFO 13936 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-03 09:23:08.845 INFO 13936 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-03 09:23:08.877 INFO 13936 --- [ main] mil.navy.msgqueue.MsgqueueApplication : Started MsgqueueApplication in 1.391 seconds (JVM running for 1.857)
2019-04-03 09:23:14.949 INFO 13936 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-03 09:23:14.949 INFO 13936 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-03 09:23:14.952 INFO 13936 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
Received on #2 -> message A
Received on #1a -> message B
Received on #1b -> message A
Received on #2 -> message B
<HIT DO-NOTHING ENDPOINT AGAIN>
Received on #1b -> message A
Received on #2 -> message A
Received on #1a -> message B
Received on #2 -> message B
BLUF - Add "?consumer.exclusive=true" to the declaration of the destination for the JmsListener annotation.
It seems that the solution is not that complex, especially if one abandons ActiveMQ's "message groups" in favor or "exclusive consumers". The drawback to the "message groups" is that the sender has to have prior knowledge of the potential partitioning of message consumers. If the producer has this knowledge, then "message groups" are a nice solution, as the solution is somewhat independent of the consumer.
But, a similar solution can be implemented from the consumer side, by having the consumer declare "exclusive consumer" on the queue. While I did not see anything in the JmsTemplate implementation that directly supports this, it seems that Spring's JmsTemplate implementation passes the queue name to ActiveMQ, and then ActiveMQ "does the right thing" and enforces the exclusive consumer behavior.
So...
Change the following...
private final String consumerOne = "Consumer.myConsumer1.VirtualTopic.TEST-TOPIC";
to...
private final String consumerOne = "Consumer.myConsumer1.VirtualTopic.TEST-TOPIC";?consumer.exclusive=true
Once I did this, only one of the two declared receive methods were invoked, and message order was maintained in all my test runs.

HikariCP restart with Spring Cloud Config

I have recently configured my application to use Spring Cloud Config with Github as a configuration repository.
Spring Boot - 2.1.1.RELEASE
Spring Cloud Dependencies - Greenwich.RC2
My application is using pretty much everything out of the box. I have just configured the database in application.yml and I have HikariCP autoconfigurations doing the magic in the background.
I am refreshing my applications using this job that calls refresh() method on the RefreshEndpoint.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
#EnableScheduling
#Component
public class ConfigRefreshJob {
private static final Logger LOG = LoggerFactory.getLogger(ConfigRefreshJob.class);
private static final int ONE_MINUTE = 60 * 1000;
private final RefreshEndpoint refreshEndpoint;
#Autowired
public ConfigRefreshJob(final RefreshEndpoint refreshEndpoint) {
this.refreshEndpoint = refreshEndpoint;
}
#Scheduled(fixedDelay = ONE_MINUTE)
public void refreshConfigs() {
LOG.info("Refreshing Configurations - {}", refreshEndpoint.refresh());
}
}
Everything seems be working good, but I see following logs every time I refresh the configurations. These logs say HikariCP pool is shutdown and started everytime I refresh.
2019-01-16 18:54:55.817 INFO 14 --- [taskScheduler-9] o.s.b.SpringApplication : Started application in 0.155 seconds (JVM running for 144.646)
2019-01-16 18:54:55.828 INFO 14 --- [taskScheduler-9] c.z.h.HikariDataSource : HikariPool-1555 - Shutdown initiated...
2019-01-16 18:54:55.828 INFO 14 --- [taskScheduler-9] c.z.h.HikariDataSource : HikariPool-1555 - Shutdown completed.
2019-01-16 18:54:55.828 INFO 14 --- [taskScheduler-9] c.d.ConfigRefreshJob : Refreshing Configurations - []
2019-01-16 18:55:03.094 INFO 14 --- [ XNIO-1 task-5] c.z.h.HikariDataSource : HikariPool-1556 - Starting...
2019-01-16 18:55:03.123 INFO 14 --- [ XNIO-1 task-5] c.z.h.HikariDataSource : HikariPool-1556 - Start completed.
If I look at the times of these logs, it takes around 8 seconds for the HikariCP to be configured again.
I haven't found any issues in my application as of now since the load on the application is not that much right now, but here are couple of questions that I have.
Does this restart of HikariCP cause issues with the load to the application is increased?
If the restarting can cause issues, is there a way to not refresh the HikariCP?
HikariCP is made refreshable by default because a change made to it that seals the configuration once the pool is started.
So disable this, set spring.cloud.refresh.refreshable to an empty set.
Here is the example to configure in yaml
spring:
cloud:
refresh:
refreshable:
- com.example.app.config.ConfigProperties
where ConfigProperties is the class annotated with #RefreshScope.
this worked for me (spring-boot-2.2.7.RELEASE, spring-cloud-Hoxton.SR4)
spring.cloud.refresh.extra-refreshable=com.zaxxer.hikari.HikariDataSource

spring config server not posting to rabbitmq

I've generated a spring boot config server from spring's initialzr.
I've installed rabbitmq with brew. initialzr generated with boot version 2.1.1.RELEASE and cloud version Greenwich.M3.
the simple rest services are connecting to rabbitmq queues. the config server is connection to a gitlab config repo.
But when I commit and push a change to it, the change is not reflected by the service application. The config server gets logs messages when the push is completed. Can anyone say what might be wrong? No messages ever seem to appear in rabbitmq console. I have been able to refresh the properties via actuator/bus-refresh through rabbitmq though.
config-server log messages on commit of change to config-repo's employee-service.yml file:
2018-12-07 11:53:12.185 INFO 84202 --- [nio-8888-exec-1] o.s.c.c.monitor.PropertyPathEndpoint : Refresh for: employee:service
2018-12-07 11:53:12.228 INFO 84202 --- [nio-8888-exec-1] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$b43cc593] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-12-07 11:53:12.253 INFO 84202 --- [nio-8888-exec-1] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2018-12-07 11:53:12.259 INFO 84202 --- [nio-8888-exec-1] o.s.boot.SpringApplication : Started application in 0.072 seconds (JVM running for 3075.606)
2018-12-07 11:53:12.345 INFO 84202 --- [nio-8888-exec-1] o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []
2018-12-07 11:53:12.345 INFO 84202 --- [nio-8888-exec-1] o.s.c.c.monitor.PropertyPathEndpoint : Refresh for: employee-service
2018-12-07 11:53:12.377 INFO 84202 --- [nio-8888-exec-1] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$b43cc593] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-12-07 11:53:12.398 INFO 84202 --- [nio-8888-exec-1] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2018-12-07 11:53:12.402 INFO 84202 --- [nio-8888-exec-1] o.s.boot.SpringApplication : Started application in 0.056 seconds (JVM running for 3075.749)
2018-12-07 11:53:12.489 INFO 84202 --- [nio-8888-exec-1] o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []
config-server has this application.yml:
---
server:
port: ${PORT:8888}
spring:
cloud:
bus:
enabled: true
config:
server:
git:
uri: ${CONFIG_REPO_URI:git#gitlab.<somedomain>:<somegroup>/config-repo.git}
search-paths:
- feature/initial-repo
main:
banner-mode: "off"
and ConfigServerApplication.java:
#SpringBootApplication
#EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
and these gradle dependencies:
dependencies {
implementation('org.springframework.cloud:spring-cloud-config-server')
implementation('org.springframework.cloud:spring-cloud-starter-stream-rabbit')
implementation('org.springframework.cloud:spring-cloud-config-monitor')
testImplementation('org.springframework.boot:spring-boot-starter-test')
implementation('org.springframework.cloud:spring-cloud-stream-test-support')
}
service has this applciation.yml:
---
server:
port: 8092
management:
security:
enabled: "false"
endpoints:
web:
exposure:
include:
- '*'
spring:
main:
banner-mode: "off"
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
this bootstrap.yml:
---
spring:
application:
name: employee-service
cloud:
config:
uri:
- http://localhost:8888
label: feature(_)initial-repo
these gradle dependencies:
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.cloud:spring-cloud-starter-config')
implementation('org.springframework.cloud:spring-cloud-starter-bus-amqp')
implementation('org.springframework.boot:spring-boot-starter-actuator')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
this main class:
#SpringBootApplication
public class EmployeeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeServiceApplication.class, args);
}
}
and this controller class:
#RefreshScope
#RestController
public class WelcomeController {
#Value("${app.service-name}")
private String serviceName;
#Value("${app.shared.attribute}")
private String sharedAttribute;
#GetMapping("/service")
public String getServiceName() {
return "service name [" + this.serviceName + "]";
}
#GetMapping("/shared")
public String getSharedAttribute() {
return " application.yml [" + this.sharedAttribute + "]";
}
}
Try to create and build your project with Maven instead of Gradle.
I experience the same problem. I have two identical apps with the same RabbitMQ dependencies, one is build with Maven and second with Gradle. The Maven-based app publishes things to RabbitMQ as expected. The very same app, built with Gradle doesn't establish connection to RabbitMQ and is not publishing events.
To further clarify things, I run both apps in Eclipse with Spring Tools.

How to get Environment properties from application.properties into logback.groovy in Spring Boot project?

Trying to inject properties defined in application.properties/application.yml into logback.groovy script in Spring Boot project.
I cannot Inject Environment or ApplicationContext into groovy scripts.
Are there any workarounds?
I am not looking for solutions like System.getProperty('spring.profiles.active')
src/main/resources/logback.groovy
import org.springframework.core.env.Environment
#Inject private Environment env; //this is not working. how to get env here?
println "spring.profiles.active : ${env.getProperty('spring.profiles.active')}"
appender("STDOUT", ConsoleAppender) {
encoder(PatternLayoutEncoder) {
pattern = "%green(%d{HH:mm:ss.SSS}) [%thread] %highlight(%-5level) %cyan(%logger{36}) - %msg%n"
}
}
if(System.getProperty("spring.profiles.active")?.equalsIgnoreCase("prod")) {
root INFO, ["STDOUT", "FILE"]
} else {
root INFO, ["STDOUT"]
}
src/main/resources/application.yml
---
spring:
profiles:
active: development
logback.groovy needs to be evaluated very early because otherwise the code for loading the spring configuration, instantiating beans, etc. could not log anything. That's why #Inject can't work.
I see 2 options:
You could easily load application.properties in logback.groovy, but it's far from trivial to take all the configuration override mechanisms that spring provides into account
Create a spring bean that changes the logback configuration programmaticaly
when initialized
A different approach is to externalize the logback configuration on production with -Dlogback.configurationFile=/path/to/config.groovy. Putting the config file in a well known location (like /etc/my-app) makes it easy to change log levels in production without re-deploying (or even re-starting).
Sorry to resurrect this thread but while since this is thread is what I found while looking for a solution, I wanted to share a workaround.
I have used a Custom converter and conversion rule to pull out properties in classpath resource (i.e. application.properties):
In logback.groovy:
conversionRule('springApplicationName', CustomSpringApplicationNameConverter)
def patternExpression = '%green([%d{YYYY-MM-dd HH:mm:ss.SSS}]) [%t] %highlight(%-5level) %magenta([%springApplicationName]) - %cyan(%logger{36}) -- %msg%n'
and then 'patternExpression' used in desired appender
and my custom converter class (in groovy):
class CustomSpringApplicationNameConverter extends ClassicConverter {
#Override
String convert(ILoggingEvent event) {
ClassPathResource classPathResource = new ClassPathResource('application.properties')
Properties applicationProperties = new Properties()
applicationProperties.load(classPathResource.inputStream)
String springApplicationName = applicationProperties.getProperty('spring.application.name')
if (!springApplicationName) {
System.err.println('Could not find entry for \'spring.application.name\' in \'application.properties\'')
}
springApplicationName
}
}

Resources