Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 358 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pretty print JSON output of Spring Boot Actuator endpoints

#1
**Spring Boot Actuator** provides several **endpoints** to **monitor** an **application** as:

/metrics
/beans
/health
...

Checking the endpoints with:

curl

[To see links please register here]


results in:

{"counter.status.200.env":1,"counter.status.200.health":1,"counter.status.200.info":2,"counter.status.200.metrics":2,"gauge.response.env":5.0,"gauge.response.health":22.0,"gauge.response.info":1.0,"gauge.response.metrics":1.0,"mem":1030144,"mem.free":56118,"processors":8,"uptime":5108095,"instance.uptime":5102906,"heap.committed":1030144,"heap.init":262144,"heap.used":974031,"heap":3728384,"threads.peak":81,"threads.daemon":21,"threads":77,"classes":8854,"classes.loaded":8860,"classes.unloaded":6,"gc.ps_scavenge.count":119,"gc.ps_scavenge.time":7223,"gc.ps_marksweep.count":12,"gc.ps_marksweep.time":17573}


This is **fine for machine consumption** but **hard to read** by **humans**.

I'd like to **format** (i.e. pretty print) the **JSON** output of the **Spring Boot Actuator** endpoints to make them easier to read by operations personel.


Something like:

{
"counter.status.200.env":1,
"counter.status.200.health":1,
"counter.status.200.info":2,
"counter.status.200.metrics":2,
"gauge.response.env":5.0,
"gauge.response.health":22.0,
"gauge.response.info":1.0,
...
}
I tried setting

http.mappers.json-pretty-print=true

but this setting didn't affect the Actuator output.

Is there a **configuration** to **enable pretty print** of the **Spring Boot Actuator JSON** output?

**UPDATE:**

The [official sample][1] works for me.


It's important to follow the comments from @DaveSyer: the property to set is

http.mappers.jsonPrettyPrint=true

Investigation is still under way.

In the meantime I use the the json pretty print **command line** as **workaround**:

Install jsonpp (e.g. for OS X):

brew install jsonpp

Then pipe the curl output trough jsonpp which formats the json file on the fly:

curl

[To see links please register here]

| jsonpp

Results in:

{
"counter.status.200.env": 1,
"counter.status.200.health": 1,
"counter.status.200.info": 2,
"counter.status.200.metrics": 2,
...
}


[1]:

[To see links please register here]

Reply

#2
The "http.mappers" property works for me but I think you might need it camel cased ("jsonPrettyPrint").
Reply

#3
Do the following:

@Configuration
public class JacksonConfig {

@Autowired
private ObjectMapper objectMapper; //reuse the pre-configured mapper


@PostConstruct
public void setup() {
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
//whatever else you need
}


}

This works because Spring Boot uses an `ObjectMapper` bean to perform all the JSON related operations.

Note however that this configuration will pretty print all JSON outputs, not just the actuator related stuff.

**UPDATE**

The answer from @DaveSyer is obviously better! I hadn't found the `HttpMapperProperties` object which is used to configure Jackson. [This][1] is it's Javadoc


[1]:

[To see links please register here]

Reply

#4
I use Python's commonly installed `json.tool` module:

curl --silent

[To see links please register here]

| python -mjson.tool
Reply

#5
As per

[To see links please register here]

, the official way to enable pretty print with Jackson in Spring Boot (1.2.2 at least) is to set the following property:

# Pretty-print JSON responses
spring.jackson.serialization.indent_output=true
Reply

#6
With spring-boot 1.2.6, you need to use:

spring.jackson.serialization.INDENT_OUTPUT=true

From my log when using the old http.mappers.*:

http.mappers.json-pretty-print is deprecated. If you are using Jackson, spring.jackson.serialization.INDENT_OUTPUT=true should be used instead.
Reply

#7
Unfortunately, the application property

> spring.jackson.serialization.INDENT_OUTPUT

did not work for me (spring boot versions 1.2.6 to 1.4.0.RELEASE). Instead, in my extension of *WebMvcConfigurerAdapter*, I've overridden *configureMessageConverters()* and added my own Jackson2ObjectMapperBuilder:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
...
private MappingJackson2HttpMessageConverter jacksonMessageConverter() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS)
.featuresToEnable(SerializationFeature.INDENT_OUTPUT).modulesToInstall(hibernate4Module());
// can use this instead of featuresToEnable(...)
builder.indentOutput(true);
return new MappingJackson2HttpMessageConverter(builder.build());
}


@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}

...

}

That seem to do the trick for me on Spring boot 1.4.0.RELEASE and my actuator output is now pretty printed (along with every other json output)
Reply

#8
Here is my Emacs function to retrieve Spring Actuator Json from endpoints:

(defvar my/spring-actuator-server-history nil)
(defvar my/spring-actuator-last-server "http://localhost:8080")
(defvar my/spring-actuator-path-history nil)
(defvar my/spring-actuator-path-completion
'("actuator" "auditevents" "autoconfig" "beans" "configprops" "dump" "env" "flyway" "health" "heapdump"
"info" "jolokia" "liquibase" "logfile" "loggers" "mappings" "metrics" "shutdown" "trace")))

(defun my/spring-actuator (server path)
(interactive (list (read-string "Server: " my/spring-actuator-last-server 'my/spring-actuator-server-history)
(completing-read "Path: " my/spring-actuator-path-completion nil nil "" 'my/spring-actuator-path-history)))
(setq my/spring-actuator-last-server server)
(let ( (bufname (format "actuator: %s" path)) )
(when (get-buffer bufname)
(kill-buffer bufname))
(switch-to-buffer (url-retrieve-synchronously (format "%s/%s" server path)))
(rename-buffer bufname)
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(forward-char)
(delete-region (point-min) (point))
(json-pretty-print-buffer)
(json-mode) ))

If you don't like dependency on external `json-mode` library replace it with `js-mode`.
Reply

#9
I use `jq` for pretty printing JSON as well as filtering it. It's basically `sed` for JSON. On the mac, it can be installed with homebrew. (

[To see links please register here]

)

curl

[To see links please register here]

| jq
Reply

#10
For Spring Boot 1.5.1 I have in my YML file:

spring:
jackson:
serialization:
INDENT_OUTPUT: true

@BertrandRenuart answer was the closest, but by IDE did not see indent_output as correct.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through