12
12
import io .jenkins .plugins .opentelemetry .api .OpenTelemetryLifecycleListener ;
13
13
import io .jenkins .plugins .opentelemetry .semconv .ExtendedJenkinsAttributes ;
14
14
import io .jenkins .plugins .opentelemetry .semconv .JenkinsMetrics ;
15
+ import io .opentelemetry .api .GlobalOpenTelemetry ;
15
16
import io .opentelemetry .api .common .Attributes ;
16
17
import io .opentelemetry .api .common .AttributesBuilder ;
17
- import io .opentelemetry .api .incubator .events .EventLogger ;
18
18
import io .opentelemetry .api .logs .Severity ;
19
19
import io .opentelemetry .api .metrics .LongCounter ;
20
20
import io .opentelemetry .api .metrics .Meter ;
21
21
import io .opentelemetry .semconv .ClientAttributes ;
22
22
import io .opentelemetry .semconv .incubating .EnduserIncubatingAttributes ;
23
+ import io .opentelemetry .semconv .incubating .EventIncubatingAttributes ;
24
+ import io .opentelemetry .semconv .incubating .UserIncubatingAttributes ;
23
25
import jenkins .YesNoMaybe ;
24
26
import jenkins .security .SecurityListener ;
25
27
import org .springframework .security .core .Authentication ;
@@ -48,15 +50,16 @@ public class AuditingSecurityListener extends SecurityListener implements OpenTe
48
50
private LongCounter loginFailureCounter ;
49
51
private LongCounter loginCounter ;
50
52
51
- private EventLogger eventLogger ;
53
+ private io . opentelemetry . api . logs . Logger otelLogger ;
52
54
53
55
private JenkinsControllerOpenTelemetry jenkinsControllerOpenTelemetry ;
54
56
55
57
@ PostConstruct
56
58
public void postConstruct () {
57
59
LOGGER .log (Level .FINE , () -> "Start monitoring Jenkins controller authentication events..." );
58
60
59
- this .eventLogger = jenkinsControllerOpenTelemetry .getDefaultEventLogger ();
61
+ otelLogger = GlobalOpenTelemetry .get ().getLogsBridge ().get (ExtendedJenkinsAttributes .INSTRUMENTATION_NAME );
62
+
60
63
Meter meter = jenkinsControllerOpenTelemetry .getDefaultMeter ();
61
64
62
65
loginSuccessCounter =
@@ -96,28 +99,29 @@ protected void loggedIn(@NonNull String username) {
96
99
AttributesBuilder attributesBuilder = Attributes .builder ();
97
100
Optional <User > user = Optional .ofNullable (User .current ());
98
101
attributesBuilder
102
+ .put (EventIncubatingAttributes .EVENT_NAME , ExtendedJenkinsAttributes .EVENT_NAME_USER_LOGIN )
99
103
.put (ExtendedJenkinsAttributes .EVENT_CATEGORY , ExtendedJenkinsAttributes .EventCategoryValues .AUTHENTICATION )
100
104
.put (ExtendedJenkinsAttributes .EVENT_OUTCOME , ExtendedJenkinsAttributes .EventOutcomeValues .SUCCESS )
101
105
.put (EnduserIncubatingAttributes .ENDUSER_ID , user .map (User ::getId ).orElse (username ))
106
+ .put (UserIncubatingAttributes .USER_ID , user .map (User ::getId ).orElse (username ))
102
107
;
103
108
104
109
// Stapler.getCurrentRequest() returns null, it's not yet initialized
105
110
SecurityContext securityContext = SecurityContextHolder .getContext ();
106
111
if (securityContext != null ) {
107
112
Authentication authentication = securityContext .getAuthentication ();
108
113
Object details = authentication .getDetails ();
109
- if (details instanceof WebAuthenticationDetails ) {
110
- WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails ) details ;
114
+ if (details instanceof WebAuthenticationDetails webAuthenticationDetails ) {
111
115
attributesBuilder
112
116
.put (ClientAttributes .CLIENT_ADDRESS , webAuthenticationDetails .getRemoteAddress ());
113
117
message += " from " + webAuthenticationDetails .getRemoteAddress ();
114
118
}
115
119
}
116
- attributesBuilder .put ("message" , message );
117
120
118
- eventLogger . builder ( "user_login" )
119
- .setAttributes (attributesBuilder .build ())
121
+ otelLogger . logRecordBuilder ( )
122
+ .setAllAttributes (attributesBuilder .build ())
120
123
.setSeverity (Severity .INFO )
124
+ .setBody (message )
121
125
.emit ();
122
126
}
123
127
@@ -130,18 +134,19 @@ protected void failedToLogIn(@NonNull String username) {
130
134
String message = "Failed login of user '" + username + "'" ;
131
135
AttributesBuilder attributesBuilder = Attributes .builder ();
132
136
attributesBuilder
137
+ .put (EventIncubatingAttributes .EVENT_NAME , ExtendedJenkinsAttributes .EVENT_NAME_USER_LOGIN )
133
138
.put (ExtendedJenkinsAttributes .EVENT_CATEGORY , ExtendedJenkinsAttributes .EventCategoryValues .AUTHENTICATION )
134
139
.put (ExtendedJenkinsAttributes .EVENT_OUTCOME , ExtendedJenkinsAttributes .EventOutcomeValues .FAILURE )
135
140
.put (EnduserIncubatingAttributes .ENDUSER_ID , username )
141
+ .put (UserIncubatingAttributes .USER_ID , username )
136
142
;
137
143
138
144
// TODO find a solution to retrieve the remoteIpAddress
139
145
140
- attributesBuilder .put ("message" , message );
141
-
142
- eventLogger .builder ("user_login" )
143
- .setAttributes (attributesBuilder .build ())
146
+ otelLogger .logRecordBuilder ()
147
+ .setAllAttributes (attributesBuilder .build ())
144
148
.setSeverity (Severity .WARN )
149
+ .setBody (message )
145
150
.emit ();
146
151
}
147
152
0 commit comments