@@ -30,7 +30,9 @@ public struct SessionEvent {
3030/// - All session events are converted to OpenTelemetry log records with appropriate attributes
3131/// - Session end events include duration and end time attributes
3232public class SessionEventInstrumentation {
33- private let logger : Logger
33+ private static var logger : Logger {
34+ return OpenTelemetry . instance. loggerProvider. get ( instrumentationScopeName: SessionEventInstrumentation . instrumentationKey)
35+ }
3436
3537 /// Queue for storing session events that were created before instrumentation was initialized.
3638 /// This allows capturing session events that occur during application startup before
@@ -43,42 +45,35 @@ public class SessionEventInstrumentation {
4345
4446 /// Notification name for new session events.
4547 /// Used to broadcast session creation and expiration events after instrumentation is applied.
46- static let sessionEventNotification = Notification . Name ( SessionConstants . sessionEventNotification)
48+ @available ( * , deprecated, message: " Use SessionEventNotification instead " )
49+ static let sessionEventNotification = SessionEventNotification
4750
4851 static let instrumentationKey = " io.opentelemetry.sessions "
4952
53+ @available ( * , deprecated, message: " Use SessionEventInstrumentation.install() instead " )
54+ public init ( ) {
55+ SessionEventInstrumentation . install ( )
56+ }
57+
5058 /// Flag to track if the instrumentation has been applied.
5159 /// Controls whether new sessions are queued or immediately processed via notifications.
5260 static var isApplied = false
53-
54- public init ( ) {
55- logger = OpenTelemetry . instance. loggerProvider. get ( instrumentationScopeName: SessionEventInstrumentation . instrumentationKey)
56- guard !SessionEventInstrumentation. isApplied else {
61+ public static func install( ) {
62+ guard !isApplied else {
5763 return
5864 }
5965
60- SessionEventInstrumentation . isApplied = true
66+ isApplied = true
6167 // Process any queued sessions
6268 processQueuedSessions ( )
63-
64- // Start observing for new session notifications
65- NotificationCenter . default. addObserver (
66- forName: SessionEventInstrumentation . sessionEventNotification,
67- object: nil ,
68- queue: nil
69- ) { notification in
70- if let sessionEvent = notification. object as? SessionEvent {
71- self . createSessionEvent ( session: sessionEvent. session, eventType: sessionEvent. eventType)
72- }
73- }
7469 }
7570
7671 /// Process any sessions that were queued before instrumentation was applied.
7772 ///
7873 /// This method is called during the `apply()` process to handle any sessions that
7974 /// were created before the instrumentation was initialized. It creates log records
8075 /// for all queued sessions and then clears the queue.
81- private func processQueuedSessions( ) {
76+ private static func processQueuedSessions( ) {
8277 let sessionEvents = SessionEventInstrumentation . queue
8378
8479 if sessionEvents. isEmpty {
@@ -97,7 +92,7 @@ public class SessionEventInstrumentation {
9792 /// - Parameters:
9893 /// - session: The session to create an event for
9994 /// - eventType: The type of event to create (start or end)
100- private func createSessionEvent( session: Session , eventType: SessionEventType ) {
95+ private static func createSessionEvent( session: Session , eventType: SessionEventType ) {
10196 switch eventType {
10297 case . start:
10398 createSessionStartEvent ( session: session)
@@ -111,21 +106,21 @@ public class SessionEventInstrumentation {
111106 /// Creates an OpenTelemetry log record with session attributes including ID, start time,
112107 /// and previous session ID (if available).
113108 /// - Parameter session: The session that has started
114- private func createSessionStartEvent( session: Session ) {
109+ private static func createSessionStartEvent( session: Session ) {
115110 var attributes : [ String : AttributeValue ] = [
116- SessionConstants . id: AttributeValue . string ( session. id) ,
117- SessionConstants . startTime: AttributeValue . double ( Double ( session. startTime. timeIntervalSince1970. toNanoseconds) )
111+ SemanticConventions . Session. id. rawValue: AttributeValue . string ( session. id)
118112 ]
119113
120114 if let previousId = session. previousId {
121- attributes [ SessionConstants . previousId] = AttributeValue . string ( previousId)
115+ attributes [ SemanticConventions . Session . previousId. rawValue ] = AttributeValue . string ( previousId)
122116 }
123117
124118 /// Create `session.start` log record according to otel semantic convention
125119 /// https://opentelemetry.io/docs/specs/semconv/general/session/
126120 logger. logRecordBuilder ( )
127- . setBody ( AttributeValue . string ( SessionConstants . sessionStartEvent) )
121+ . setEventName ( SessionConstants . sessionStartEvent)
128122 . setAttributes ( attributes)
123+ . setTimestamp ( session. startTime)
129124 . emit ( )
130125 }
131126
@@ -134,44 +129,38 @@ public class SessionEventInstrumentation {
134129 /// Creates an OpenTelemetry log record with session attributes including ID, start time,
135130 /// end time, duration, and previous session ID (if available).
136131 /// - Parameter session: The expired session
137- private func createSessionEndEvent( session: Session ) {
138- guard let endTime = session. endTime,
139- let duration = session. duration else {
132+ private static func createSessionEndEvent( session: Session ) {
133+ guard let endTime = session. endTime else {
140134 return
141135 }
142136
143137 var attributes : [ String : AttributeValue ] = [
144- SessionConstants . id: AttributeValue . string ( session. id) ,
145- SessionConstants . startTime: AttributeValue . double ( Double ( session. startTime. timeIntervalSince1970. toNanoseconds) ) ,
146- SessionConstants . endTime: AttributeValue . double ( Double ( endTime. timeIntervalSince1970. toNanoseconds) ) ,
147- SessionConstants . duration: AttributeValue . double ( Double ( duration. toNanoseconds) )
138+ SemanticConventions . Session. id. rawValue: AttributeValue . string ( session. id)
148139 ]
149140
150141 if let previousId = session. previousId {
151- attributes [ SessionConstants . previousId] = AttributeValue . string ( previousId)
142+ attributes [ SemanticConventions . Session . previousId. rawValue ] = AttributeValue . string ( previousId)
152143 }
153144
154145 /// Create `session.end`` log record according to otel semantic convention
155146 /// https://opentelemetry.io/docs/specs/semconv/general/session/
156147 logger. logRecordBuilder ( )
157- . setBody ( AttributeValue . string ( SessionConstants . sessionEndEvent) )
148+ . setEventName ( SessionConstants . sessionEndEvent)
158149 . setAttributes ( attributes)
150+ . setTimestamp ( endTime)
159151 . emit ( )
160152 }
161153
162154 /// Add a session to the queue or send notification if instrumentation is already applied.
163155 ///
164156 /// This static method is the main entry point for handling new sessions. It either:
165- /// - Adds the session to the static queue if instrumentation hasn't been applied yet (max 32 items)
157+ /// - Adds the session to the static queue if instrumentation hasn't been applied yet (max 10 items)
166158 /// - Posts a notification with the session if instrumentation has been applied
167159 ///
168160 /// - Parameter session: The session to process
169161 static func addSession( session: Session , eventType: SessionEventType ) {
170162 if isApplied {
171- NotificationCenter . default. post (
172- name: sessionEventNotification,
173- object: SessionEvent ( session: session, eventType: eventType)
174- )
163+ createSessionEvent ( session: session, eventType: eventType)
175164 } else {
176165 /// SessionManager creates sessions before SessionEventInstrumentation is applied,
177166 /// which the notification observer cannot see. So we need to keep the sessions in a queue.
@@ -181,4 +170,4 @@ public class SessionEventInstrumentation {
181170 queue. append ( SessionEvent ( session: session, eventType: eventType) )
182171 }
183172 }
184- }
173+ }
0 commit comments