You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2021-10-03-spt-development-audit-spring.markdown
+5-5
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ In this post I introduce the features of [spt-development-audit-spring](https://
14
14
{: class="lead"}
15
15
16
16
## Demo project
17
-
{: class="fs-3" }
17
+
{: class="fs-4" }
18
18
19
19
The demo project provides a simple 'books' REST API backed by and in-memory H2 database with basic authentication configured with Spring Security. Once running, the REST API can then be exercised with cURL as follows:
20
20
@@ -23,12 +23,12 @@ The demo project provides a simple 'books' REST API backed by and in-memory H2 d
23
23
See the [README](https://github.com/spt-development/spt-development-demo/blob/main/README.md) for more detail.
24
24
25
25
## Auditing
26
-
{: class="fs-3" }
26
+
{: class="fs-4" }
27
27
28
28
There are many reasons why you might want to add auditing to your application. The main reason we wanted to add auditing to [SPT Contractors](https://spt.contractors/) was to be able to trace through how records reached a particular state; having this information has been invaluable on past projects for replaying when things go wrong and answering user queries when records are in a state they don't expect. We wanted an auditing solution that was simple to implement without polluting my business logic with calls to persist the audit records. Additionally, we wanted another service to be responsible for persisting the audit records which could potentially be extended to perform analysis or transformations at a later date. Out of these requirements `spt-development-audit-spring` was born.
29
29
30
30
## spt-development-audit-spring
31
-
{: class="fs-3" }
31
+
{: class="fs-4" }
32
32
33
33
The demo project uses the `spt-development-audit-spring-boot-starter` to pull in the required dependencies and automatically configure the auditor. The demo project also has a dependency on the [spt-development-cid-web-spring-boot](https://github.com/spt-development/spt-development-cid-web-spring-boot) starter in order to pull in and configure `spt-development-cid-web` and its dependencies so that the correlation ID on the audit event records is set correctly (see [earlier post]({% post_url 2021-05-30-spt-development-cid-part-2 %}) for more details). The demo project also uses the [spt-development-logging-spring-boot](https://github.com/spt-development/spt-development-logging-spring-boot) starter (also discussed in my earlier post) and the [spt-development-cid-jms-spring-boot](https://github.com/spt-development/spt-development-cid-jms-spring-boot) starter that I will talk about in greater depth, later in this post.
34
34
@@ -57,14 +57,14 @@ Multiple parameters can be annotated with `@Audited.Detail` but if you do, the `
57
57
The `spt-development-audit-spring-boot-starter` configures the `Slf4jAuditEventWriter` bean by default, which is an instance of the `AuditEventWriter` interface. In a production service you probably want your adit event records persisted to some kind of database, in which case you will need to create a bean that is an instance of `AuditEventWriter` or if your service methods are transactional, an instance of `TransactionAwareAuditEventWriter` so that the audit events are only written if the transaction is successfully committed. For SPT Contractors, we developed a separate service responsible for persisting the audit event records read from a JMS queue and would recommend this approach in general. Creating a `JmsTemplate` bean and setting the `spt.audit.jms.destination` property results in the `spt-development-audit-spring-boot-starter` instantiating a `JmsAuditEventWriter` bean which writes the audit event records to the configured JMS queue, rather than to the logs with `Slf4jAuditEventWriter`. The demo project uses an in-memory Artemis JMS queue to demonstrate this.
58
58
59
59
## Actuator audit events
60
-
{: class="fs-3" }
60
+
{: class="fs-4" }
61
61
62
62
It is very straight forward to integrate `spt-development-auditor` with [Actuator Auditing](https://docs.spring.io/spring-boot/docs/2.5.5/reference/html/actuator.html#actuator.auditing) which allows you to treat business audit events and actuator audit events (for example generated by Spring Security) in a consistent manner. Simply create a class that extends `AbstractAuditListener` inject a `AuditEventWriter` bean and in the `AbstractAuditListener::onAuditEvent()` method write the audit events with the audit event writer. The demo project uses this approach to audit successful and unsuccessful login attempts.
63
63
64
64
{% gist bf66f89f8c3e49d23bb4850255c76814 %}
65
65
66
66
## spt-development-cid-jms-spring
67
-
{: class="fs-3" }
67
+
{: class="fs-4" }
68
68
69
69
`spt-development-cid-jms-spring` integrates `spt-development-cid` into a Spring JMS (listener) project, initialising the correlation ID from the correlation ID of the received JMS message. The demo project shows the easiest way to integrate `spt-development-cid-jms-spring` into a Spring Boot application, with `spt-development-cid-jms-spring-boot-starter`. If your listener methods have a single `Message` parameter, there is nothing further to do. If your listener methods accept a `@Payload` annotated parameter, they will need an additional parameter annotated with the `@Header` annotation and the name set to `jms_correlationId` as shown in the `AuditListener::onMessage` method of the demo project.
Copy file name to clipboardExpand all lines: _posts/2022-10-26-spt-development-release.markdown
+3-3
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ Additionally, the following libraries have also been updated:
35
35
We have also upgraded the [demo project](https://github.com/spt-development/spt-development-demo) to v2.0.10 of all the libraries above.
36
36
37
37
### Mapped Diagnostic Context
38
-
{: class="fs-4" }
38
+
{: class="fs-5" }
39
39
40
40
Simply adding v2.0.10 spt-development-cid-web-spring-boot-starter or spt-development-cid-jms-spring-boot-starter (depending on your requirements)
41
41
as a dependency to your project, will be enough to have the correlation ID added to the [Mapped Diagnostic Context](https://logback.qos.ch/manual/mdc.html)
@@ -59,7 +59,7 @@ shows the changes made to the demo project to remove the explicit inclusion of c
59
59
spt-development-logging-spring-boot, the number of changes will be reduced.
60
60
61
61
#### Disable MDC
62
-
{: class="fs-5" }
62
+
{: class="fs-6" }
63
63
64
64
Although it is the default functionality and recommended approach, if you don't want to use MDC, maybe because you already have `CorrelationId.get()` calls
65
65
in log statements scattered throughout your code, then it it is possible to disable this new functionality by setting the `spt.cid.mdc.disabled` property to `true`.
@@ -68,7 +68,7 @@ is purely for demonstration and testing purposes however and not expected to be
68
68
no reason why it couldn't be.
69
69
70
70
### Why the U-turn
71
-
{: class="fs-4" }
71
+
{: class="fs-5" }
72
72
73
73
It was a conscious decision [early on]({% post_url 2021-05-30-spt-development-cid-part-2 %}) *not* to use the MDC. However, having had feedback from various
74
74
users, it became eveident that this was a feature that was lacking in the library and having to explicitly add `CorrelationId.get()` explicitly to log
All of the code from this post can be found in the
27
27
[spt-development-micrometer-tracing-demo](https://github.com/spt-development/spt-development-micrometer-tracing-demo) project, which was forked
@@ -43,7 +43,7 @@ Spring Boot samples only 10% of requests to prevent overwhelming the trace backe
43
43
property; this is not necessary for this demo where we are only logging to the console.
44
44
45
45
### Gotcha!
46
-
{: class="fs-4" }
46
+
{: class="fs-5" }
47
47
48
48
One thing that I struggled to find mentioned in the documentation was that tracing is *switched off* by default in tests. This is
49
49
[intended](https://github.com/spring-projects/spring-boot/issues/31308){:target="_blank"} and therefore when I ran the tests initially, the trace
@@ -53,7 +53,7 @@ annotation to the integration tests, which can be used to enable/disable metrics
53
53
{% gist b785e325630c7dcb1eeaefab50df29b0 %}
54
54
55
55
## Auditing
56
-
{: class="fs-3" }
56
+
{: class="fs-4" }
57
57
58
58
Adding the `spt-development-audit-spring-boot-starter` dependency to the project pulls in the required dependencies and automatically configures the
59
59
auditor. Since we are going to be using the Micrometer trace ID in place of the correlation ID, we can exclude the `spt-development-cid`
@@ -70,15 +70,15 @@ With this in place and methods annotated as described in [this post]({% post_url
70
70
fully audited and audit events can be correlated using the trace ID.
71
71
72
72
### Actuator audit events
73
-
{: class="fs-4" }
73
+
{: class="fs-5" }
74
74
75
75
If you are capturing actuator events as described in the previous [auditing post]({% post_url 2021-10-03-spt-development-audit-spring %}) you
76
76
need to inject the `Tracer` bean into your audit event listener and again use the current trace ID in place of the correlation ID.
77
77
78
78
{% gist c7af3532347d0d14fb35646d27d88eae %}
79
79
80
80
### JMS Propagation
81
-
{: class="fs-4" }
81
+
{: class="fs-5" }
82
82
83
83
The demo project makes use of `JmsAuditEventWriter` simply by configuring the `spt.audit.jms.destination` property. The Micrometer trace context
84
84
is not currently propagated in JMS messages by default; there is currently an
@@ -90,7 +90,7 @@ the correlation ID returned by the `CorrelationIdProvider`.
90
90
{% gist e442d6398602902177ff622bcfbf786e %}
91
91
92
92
## Logging
93
-
{: class="fs-3" }
93
+
{: class="fs-4" }
94
94
95
95
As previously written about in more detail [here]({% post_url 2021-05-30-spt-development-cid-part-2 %}) simply add the `spt-development-logging-spring-boot-starter`
96
96
as a dependency to get production quality logging added to your application without any further code changes. With the logging format changes above, you will then
Copy file name to clipboardExpand all lines: _posts/2024-07-14-spring-boot-3.3.1-starters-available-now.markdown
+1
Original file line number
Diff line number
Diff line change
@@ -41,6 +41,7 @@ We have also upgraded the [demo project](https://github.com/spt-development/spt-
41
41
[Micrometer demo project](https://github.com/spt-development/spt-development-micrometer-tracing-demo) to Spring Boot 3.3.1 and v3.3.1 of all the libraries above.
42
42
43
43
### spt-development-logging-spring - Arbitrary bean logging support
44
+
{: class="fs-5" }
44
45
45
46
Release 3.2.0 of `spt-development-logging-spring` introduces support for logging public methods of arbitrary beans. This logging is enabled by adding the
46
47
`EnableBeanLogging` annotation to your project. The `includeBasePackageClasses` must contain at least one class and enables logging of Spring beans from the same package as any of the classes specified.
0 commit comments