|
1 | | -/* |
2 | | - * Copyright 2025 Google LLC |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | - |
17 | 1 | package com.google.firebase.perf.session |
18 | 2 |
|
19 | | -import com.google.firebase.perf.session.gauges.GaugeManager |
20 | | -import com.google.firebase.perf.v1.ApplicationProcessState |
| 3 | +import com.google.firebase.perf.config.ConfigResolver |
| 4 | +import com.google.firebase.perf.logging.AndroidLogger |
21 | 5 | import com.google.firebase.sessions.api.SessionSubscriber |
22 | | -import java.util.UUID |
23 | 6 |
|
24 | | -class FirebasePerformanceSessionSubscriber(override val isDataCollectionEnabled: Boolean) : |
| 7 | +class FirebasePerformanceSessionSubscriber(private val dataCollectionEnabled: Boolean) : |
25 | 8 | SessionSubscriber { |
| 9 | + private val perfSessionToAqs: MutableMap<String, SessionSubscriber.SessionDetails?> = |
| 10 | + mutableMapOf() |
| 11 | + |
| 12 | + override val isDataCollectionEnabled: Boolean |
| 13 | + get() = dataCollectionEnabled |
26 | 14 |
|
27 | | - override val sessionSubscriberName: SessionSubscriber.Name = SessionSubscriber.Name.PERFORMANCE |
| 15 | + override val sessionSubscriberName: SessionSubscriber.Name |
| 16 | + get() = SessionSubscriber.Name.PERFORMANCE |
28 | 17 |
|
29 | 18 | override fun onSessionChanged(sessionDetails: SessionSubscriber.SessionDetails) { |
30 | | - val currentPerfSession = SessionManager.getInstance().perfSession() |
31 | | - |
32 | | - // A [PerfSession] was created before a session was started. |
33 | | - if (currentPerfSession.aqsSessionId() == null) { |
34 | | - currentPerfSession.setAQSId(sessionDetails) |
35 | | - GaugeManager.getInstance() |
36 | | - .logGaugeMetadata(currentPerfSession.aqsSessionId(), ApplicationProcessState.FOREGROUND) |
37 | | - return |
| 19 | + AndroidLogger.getInstance().debug("AQS Session Changed: $sessionDetails") |
| 20 | + val currentInternalSessionId = SessionManager.getInstance().perfSession().internalSessionId |
| 21 | + |
| 22 | + // There can be situations where a new [PerfSession] was created, but an AQS wasn't |
| 23 | + // available (during cold start). |
| 24 | + if (perfSessionToAqs[currentInternalSessionId] == null) { |
| 25 | + perfSessionToAqs[currentInternalSessionId] = sessionDetails |
| 26 | + } else { |
| 27 | + val newSession = PerfSession.createNewSession() |
| 28 | + SessionManager.getInstance().updatePerfSession(newSession) |
| 29 | + perfSessionToAqs[newSession.internalSessionId] = sessionDetails |
38 | 30 | } |
| 31 | + } |
| 32 | + |
| 33 | + fun reportPerfSession(perfSessionId: String) { |
| 34 | + perfSessionToAqs[perfSessionId] = null |
| 35 | + } |
39 | 36 |
|
40 | | - val updatedSession = PerfSession.createWithId(UUID.randomUUID().toString()) |
41 | | - updatedSession.setAQSId(sessionDetails) |
42 | | - SessionManager.getInstance().updatePerfSession(updatedSession) |
43 | | - GaugeManager.getInstance() |
44 | | - .logGaugeMetadata(updatedSession.aqsSessionId(), ApplicationProcessState.FOREGROUND) |
| 37 | + fun getAqsMappedToPerfSession(perfSessionId: String): String { |
| 38 | + AndroidLogger.getInstance() |
| 39 | + .debug("AQS for perf session $perfSessionId is ${perfSessionToAqs[perfSessionId]?.sessionId}") |
| 40 | + return perfSessionToAqs[perfSessionId]?.sessionId ?: perfSessionId |
| 41 | + } |
| 42 | + |
| 43 | + fun clearSessionForTest() { |
| 44 | + perfSessionToAqs.clear() |
| 45 | + } |
| 46 | + |
| 47 | + companion object { |
| 48 | + val instance: FirebasePerformanceSessionSubscriber by lazy { |
| 49 | + FirebasePerformanceSessionSubscriber( |
| 50 | + ConfigResolver.getInstance().isPerformanceMonitoringEnabled |
| 51 | + ) |
| 52 | + } |
45 | 53 | } |
46 | 54 | } |
0 commit comments