1212import io .jenkins .plugins .opentelemetry .api .OpenTelemetryLifecycleListener ;
1313import io .jenkins .plugins .opentelemetry .semconv .ExtendedJenkinsAttributes ;
1414import io .jenkins .plugins .opentelemetry .semconv .JenkinsMetrics ;
15+ import io .opentelemetry .api .GlobalOpenTelemetry ;
1516import io .opentelemetry .api .common .Attributes ;
1617import io .opentelemetry .api .common .AttributesBuilder ;
17- import io .opentelemetry .api .incubator .events .EventLogger ;
1818import io .opentelemetry .api .logs .Severity ;
1919import io .opentelemetry .api .metrics .LongCounter ;
2020import io .opentelemetry .api .metrics .Meter ;
2121import io .opentelemetry .semconv .ClientAttributes ;
2222import io .opentelemetry .semconv .incubating .EnduserIncubatingAttributes ;
23+ import io .opentelemetry .semconv .incubating .EventIncubatingAttributes ;
24+ import io .opentelemetry .semconv .incubating .UserIncubatingAttributes ;
2325import jenkins .YesNoMaybe ;
2426import jenkins .security .SecurityListener ;
2527import org .springframework .security .core .Authentication ;
@@ -48,15 +50,16 @@ public class AuditingSecurityListener extends SecurityListener implements OpenTe
4850 private LongCounter loginFailureCounter ;
4951 private LongCounter loginCounter ;
5052
51- private EventLogger eventLogger ;
53+ private io . opentelemetry . api . logs . Logger otelLogger ;
5254
5355 private JenkinsControllerOpenTelemetry jenkinsControllerOpenTelemetry ;
5456
5557 @ PostConstruct
5658 public void postConstruct () {
5759 LOGGER .log (Level .FINE , () -> "Start monitoring Jenkins controller authentication events..." );
5860
59- this .eventLogger = jenkinsControllerOpenTelemetry .getDefaultEventLogger ();
61+ otelLogger = GlobalOpenTelemetry .get ().getLogsBridge ().get (ExtendedJenkinsAttributes .INSTRUMENTATION_NAME );
62+
6063 Meter meter = jenkinsControllerOpenTelemetry .getDefaultMeter ();
6164
6265 loginSuccessCounter =
@@ -96,28 +99,29 @@ protected void loggedIn(@NonNull String username) {
9699 AttributesBuilder attributesBuilder = Attributes .builder ();
97100 Optional <User > user = Optional .ofNullable (User .current ());
98101 attributesBuilder
102+ .put (EventIncubatingAttributes .EVENT_NAME , ExtendedJenkinsAttributes .EVENT_NAME_USER_LOGIN )
99103 .put (ExtendedJenkinsAttributes .EVENT_CATEGORY , ExtendedJenkinsAttributes .EventCategoryValues .AUTHENTICATION )
100104 .put (ExtendedJenkinsAttributes .EVENT_OUTCOME , ExtendedJenkinsAttributes .EventOutcomeValues .SUCCESS )
101105 .put (EnduserIncubatingAttributes .ENDUSER_ID , user .map (User ::getId ).orElse (username ))
106+ .put (UserIncubatingAttributes .USER_ID , user .map (User ::getId ).orElse (username ))
102107 ;
103108
104109 // Stapler.getCurrentRequest() returns null, it's not yet initialized
105110 SecurityContext securityContext = SecurityContextHolder .getContext ();
106111 if (securityContext != null ) {
107112 Authentication authentication = securityContext .getAuthentication ();
108113 Object details = authentication .getDetails ();
109- if (details instanceof WebAuthenticationDetails ) {
110- WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails ) details ;
114+ if (details instanceof WebAuthenticationDetails webAuthenticationDetails ) {
111115 attributesBuilder
112116 .put (ClientAttributes .CLIENT_ADDRESS , webAuthenticationDetails .getRemoteAddress ());
113117 message += " from " + webAuthenticationDetails .getRemoteAddress ();
114118 }
115119 }
116- attributesBuilder .put ("message" , message );
117120
118- eventLogger . builder ( "user_login" )
119- .setAttributes (attributesBuilder .build ())
121+ otelLogger . logRecordBuilder ( )
122+ .setAllAttributes (attributesBuilder .build ())
120123 .setSeverity (Severity .INFO )
124+ .setBody (message )
121125 .emit ();
122126 }
123127
@@ -130,18 +134,19 @@ protected void failedToLogIn(@NonNull String username) {
130134 String message = "Failed login of user '" + username + "'" ;
131135 AttributesBuilder attributesBuilder = Attributes .builder ();
132136 attributesBuilder
137+ .put (EventIncubatingAttributes .EVENT_NAME , ExtendedJenkinsAttributes .EVENT_NAME_USER_LOGIN )
133138 .put (ExtendedJenkinsAttributes .EVENT_CATEGORY , ExtendedJenkinsAttributes .EventCategoryValues .AUTHENTICATION )
134139 .put (ExtendedJenkinsAttributes .EVENT_OUTCOME , ExtendedJenkinsAttributes .EventOutcomeValues .FAILURE )
135140 .put (EnduserIncubatingAttributes .ENDUSER_ID , username )
141+ .put (UserIncubatingAttributes .USER_ID , username )
136142 ;
137143
138144 // TODO find a solution to retrieve the remoteIpAddress
139145
140- attributesBuilder .put ("message" , message );
141-
142- eventLogger .builder ("user_login" )
143- .setAttributes (attributesBuilder .build ())
146+ otelLogger .logRecordBuilder ()
147+ .setAllAttributes (attributesBuilder .build ())
144148 .setSeverity (Severity .WARN )
149+ .setBody (message )
145150 .emit ();
146151 }
147152
0 commit comments