Skip to content

Commit 7d02427

Browse files
author
AR Abdul Azeez
committed
otel logging detekt
1 parent 34bccdb commit 7d02427

21 files changed

Lines changed: 223 additions & 90 deletions

File tree

OneSignalSDK/detekt/detekt-baseline-core.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<ID>ConstructorParameterNaming:UpdateUserOperationExecutor.kt$UpdateUserOperationExecutor$private val _propertiesModelStore: PropertiesModelStore</ID>
139139
<ID>ConstructorParameterNaming:UpdateUserOperationExecutor.kt$UpdateUserOperationExecutor$private val _userBackend: IUserBackendService</ID>
140140
<ID>ConstructorParameterNaming:UserBackendService.kt$UserBackendService$private val _httpClient: IHttpClient</ID>
141+
<ID>ConstructorParameterNaming:UserManager.kt$UserManager$private val _customEventController: ICustomEventController</ID>
141142
<ID>ConstructorParameterNaming:UserManager.kt$UserManager$private val _identityModelStore: IdentityModelStore</ID>
142143
<ID>ConstructorParameterNaming:UserManager.kt$UserManager$private val _languageContext: ILanguageContext</ID>
143144
<ID>ConstructorParameterNaming:UserManager.kt$UserManager$private val _propertiesModelStore: PropertiesModelStore</ID>
@@ -191,6 +192,7 @@
191192
<ID>LongMethod:TrackGooglePurchase.kt$TrackGooglePurchase$private fun queryBoughtItems()</ID>
192193
<ID>LongMethod:TrackGooglePurchase.kt$TrackGooglePurchase$private fun sendPurchases( skusToAdd: ArrayList&lt;String>, newPurchaseTokens: ArrayList&lt;String>, )</ID>
193194
<ID>LongMethod:UpdateUserOperationExecutor.kt$UpdateUserOperationExecutor$override suspend fun execute(operations: List&lt;Operation>): ExecutionResponse</ID>
195+
<ID>LongParameterList:ICustomEventBackendService.kt$ICustomEventBackendService$( appId: String, onesignalId: String, externalId: String?, timestamp: Long, eventName: String, eventProperties: String?, metadata: CustomEventMetadata, )</ID>
194196
<ID>LongParameterList:IDatabase.kt$IDatabase$( table: String, columns: Array&lt;String>? = null, whereClause: String? = null, whereArgs: Array&lt;String>? = null, groupBy: String? = null, having: String? = null, orderBy: String? = null, limit: String? = null, action: (ICursor) -> Unit, )</ID>
195197
<ID>LongParameterList:IOutcomeEventsBackendService.kt$IOutcomeEventsBackendService$( appId: String, userId: String, subscriptionId: String, deviceType: String, direct: Boolean?, event: OutcomeEvent, )</ID>
196198
<ID>LongParameterList:IParamsBackendService.kt$ParamsObject$( var googleProjectNumber: String? = null, var enterprise: Boolean? = null, var useIdentityVerification: Boolean? = null, var notificationChannels: JSONArray? = null, var firebaseAnalytics: Boolean? = null, var restoreTTLFilter: Boolean? = null, var clearGroupOnSummaryClick: Boolean? = null, var receiveReceiptEnabled: Boolean? = null, var disableGMSMissingPrompt: Boolean? = null, var unsubscribeWhenNotificationsDisabled: Boolean? = null, var locationShared: Boolean? = null, var requiresUserPrivacyConsent: Boolean? = null, var opRepoExecutionInterval: Long? = null, var influenceParams: InfluenceParamsObject, var fcmParams: FCMParamsObject, )</ID>
@@ -279,6 +281,7 @@
279281
<ID>RethrowCaughtException:OSDatabase.kt$OSDatabase$throw e</ID>
280282
<ID>ReturnCount:AppIdResolution.kt$fun resolveAppId( inputAppId: String?, configModel: ConfigModel, preferencesService: IPreferencesService, ): AppIdResolution</ID>
281283
<ID>ReturnCount:BackgroundManager.kt$BackgroundManager$override fun cancelRunBackgroundServices(): Boolean</ID>
284+
<ID>ReturnCount:OneSignalImp.kt$OneSignalImp$private fun internalInit( context: Context, appId: String?, ): Boolean</ID>
282285
<ID>ReturnCount:ConfigModel.kt$ConfigModel$override fun createModelForProperty( property: String, jsonObject: JSONObject, ): Model?</ID>
283286
<ID>ReturnCount:HttpClient.kt$HttpClient$private suspend fun makeRequest( url: String, method: String?, jsonBody: JSONObject?, timeout: Int, headers: OptionalHeaders?, ): HttpResponse</ID>
284287
<ID>ReturnCount:IdentityOperationExecutor.kt$IdentityOperationExecutor$override suspend fun execute(operations: List&lt;Operation>): ExecutionResponse</ID>
@@ -370,6 +373,7 @@
370373
<ID>TooManyFunctions:IUserManager.kt$IUserManager</ID>
371374
<ID>TooManyFunctions:InfluenceManager.kt$InfluenceManager : IInfluenceManagerISessionLifecycleHandler</ID>
372375
<ID>TooManyFunctions:JSONObjectExtensions.kt$com.onesignal.common.JSONObjectExtensions.kt</ID>
376+
<ID>TooManyFunctions:JSONUtils.kt$JSONUtils$JSONUtils</ID>
373377
<ID>TooManyFunctions:Logging.kt$Logging$Logging</ID>
374378
<ID>TooManyFunctions:Model.kt$Model : IEventNotifier</ID>
375379
<ID>TooManyFunctions:ModelStore.kt$ModelStore&lt;TModel> : IEventNotifierIModelStoreIModelChangedHandler</ID>

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/backend/IParamsBackendService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ internal class FCMParamsObject(
5858

5959
internal class RemoteLoggingParamsObject(
6060
val logLevel: com.onesignal.debug.LogLevel? = null,
61-
val isEnabled: Boolean = logLevel != null && logLevel != com.onesignal.debug.LogLevel.NONE,
61+
val isEnabled: Boolean = logLevel != null,
6262
)

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/backend/impl/ParamsBackendService.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal class ParamsBackendService(
6161
// Process Remote Logging params
6262
var remoteLoggingParams: RemoteLoggingParamsObject? = null
6363
responseJson.expandJSONObject("logging_config") {
64-
val logLevel = parseLogLevel(it)
64+
val logLevel = LogLevel.fromString(it.safeString("log_level"))
6565
remoteLoggingParams =
6666
RemoteLoggingParamsObject(
6767
logLevel = logLevel,
@@ -134,21 +134,4 @@ internal class ParamsBackendService(
134134
isUnattributedEnabled,
135135
)
136136
}
137-
138-
/**
139-
* Parse LogLevel from JSON. Supports string (enum name)
140-
*/
141-
@Suppress("ReturnCount", "TooGenericExceptionCaught", "SwallowedException")
142-
private fun parseLogLevel(json: JSONObject): LogLevel {
143-
val logLevel = json.safeString("log_level")
144-
if (logLevel != null) {
145-
try {
146-
return LogLevel.valueOf(logLevel.uppercase())
147-
} catch (_: IllegalArgumentException) {
148-
Logging.warn("Invalid log_level string: $logLevel")
149-
}
150-
}
151-
152-
return LogLevel.NONE
153-
}
154137
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/config/ConfigModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal.core.internal.config
22

33
import com.onesignal.common.modeling.Model
4+
import com.onesignal.core.internal.http.OneSignalService.ONESIGNAL_API_BASE_URL
45
import org.json.JSONArray
56
import org.json.JSONObject
67

@@ -36,7 +37,7 @@ class ConfigModel : Model() {
3637
* The API URL String.
3738
*/
3839
var apiUrl: String
39-
get() = getStringProperty(::apiUrl.name) { "https://api.onesignal.com/" }
40+
get() = getStringProperty(::apiUrl.name) { ONESIGNAL_API_BASE_URL }
4041
set(value) {
4142
setStringProperty(::apiUrl.name, value)
4243
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.onesignal.core.internal.http
2+
3+
/** Central API base URL used by all SDK HTTP traffic, including Otel log export. */
4+
object OneSignalService {
5+
// const val ONESIGNAL_API_BASE_URL = "https://api.staging.onesignal.com/"
6+
const val ONESIGNAL_API_BASE_URL = "https://api.onesignal.com/"
7+
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/debug/LogLevel.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,19 @@ enum class LogLevel {
4949
fun fromInt(value: Int): LogLevel {
5050
return values()[value]
5151
}
52+
53+
/**
54+
* Parses a [LogLevel] from its string name (case-insensitive).
55+
* Returns `null` if the string is null or not a valid level name.
56+
*/
57+
@JvmStatic
58+
fun fromString(value: String?): LogLevel? {
59+
if (value == null) return null
60+
return try {
61+
valueOf(value.uppercase())
62+
} catch (_: IllegalArgumentException) {
63+
null
64+
}
65+
}
5266
}
5367
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/debug/internal/crash/OneSignalCrashUploaderWrapper.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.onesignal.debug.internal.crash
22

3+
import com.onesignal.common.threading.OneSignalDispatchers
34
import com.onesignal.core.internal.application.IApplicationService
45
import com.onesignal.core.internal.startup.IStartableService
56
import com.onesignal.debug.internal.logging.otel.android.AndroidOtelLogger
67
import com.onesignal.debug.internal.logging.otel.android.createAndroidOtelPlatformProvider
78
import com.onesignal.otel.OtelFactory
89
import com.onesignal.otel.crash.OtelCrashUploader
9-
import kotlinx.coroutines.runBlocking
1010

1111
/**
1212
* Android-specific wrapper for OtelCrashUploader that implements IStartableService.
@@ -46,15 +46,15 @@ internal class OneSignalCrashUploaderWrapper(
4646
@Suppress("TooGenericExceptionCaught")
4747
override fun start() {
4848
if (!OtelSdkSupport.isSupported) return
49-
try {
50-
runBlocking {
49+
OneSignalDispatchers.launchOnIO {
50+
try {
5151
uploader.start()
52+
} catch (t: Throwable) {
53+
com.onesignal.debug.internal.logging.Logging.warn(
54+
"OneSignal: Crash uploader failed to start: ${t.message}",
55+
t,
56+
)
5257
}
53-
} catch (t: Throwable) {
54-
com.onesignal.debug.internal.logging.Logging.warn(
55-
"OneSignal: Crash uploader failed to start: ${t.message}",
56-
t,
57-
)
5858
}
5959
}
6060
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/debug/internal/crash/OtelAnrDetector.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ internal class OtelAnrDetector(
5858
logger.info("$TAG: ✅ ANR detection started successfully")
5959
}
6060

61+
@Suppress("TooGenericExceptionCaught")
6162
private fun setupRunnables() {
6263
// Runnable that runs on the main thread to indicate it's responsive
6364
mainThreadRunnable = Runnable {
@@ -81,8 +82,8 @@ internal class OtelAnrDetector(
8182
}
8283

8384
private fun checkForAnr() {
84-
// Post a message to the main thread
85-
mainHandler.post(mainThreadRunnable!!)
85+
val runnable = mainThreadRunnable ?: return
86+
mainHandler.post(runnable)
8687

8788
// Wait for the check interval
8889
Thread.sleep(checkIntervalMs)
@@ -145,6 +146,7 @@ internal class OtelAnrDetector(
145146
logger.info("$TAG: ✅ ANR detection stopped")
146147
}
147148

149+
@Suppress("TooGenericExceptionCaught")
148150
private fun reportAnr(unresponsiveDurationMs: Long) {
149151
try {
150152
logger.info("$TAG: Checking if ANR is OneSignal-related (unresponsive for ${unresponsiveDurationMs}ms)")

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/debug/internal/logging/otel/android/OtelIdResolver.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,10 @@ internal class OtelIdResolver(
226226
}
227227
}
228228

229-
@Suppress("TooGenericExceptionCaught", "SwallowedException")
230-
private fun extractLogLevelFromParams(remoteLoggingParams: JSONObject): com.onesignal.debug.LogLevel? {
231-
return if (remoteLoggingParams.has("logLevel")) {
232-
val logLevelString = remoteLoggingParams.getString("logLevel")
233-
try {
234-
com.onesignal.debug.LogLevel.valueOf(logLevelString.uppercase())
235-
} catch (e: Exception) {
236-
null
237-
}
238-
} else {
239-
null
240-
}
241-
}
229+
private fun extractLogLevelFromParams(remoteLoggingParams: JSONObject): com.onesignal.debug.LogLevel? =
230+
com.onesignal.debug.LogLevel.fromString(
231+
if (remoteLoggingParams.has("logLevel")) remoteLoggingParams.getString("logLevel") else null
232+
)
242233

243234
/**
244235
* Resolves install ID from SharedPreferences.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/debug/internal/logging/otel/android/OtelPlatformProvider.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.Context
55
import android.os.Build
66
import com.onesignal.common.OneSignalUtils
77
import com.onesignal.common.OneSignalWrapper
8+
import com.onesignal.core.internal.http.OneSignalService
89
import com.onesignal.debug.internal.logging.Logging
910
import com.onesignal.otel.IOtelPlatformProvider
1011

@@ -107,16 +108,11 @@ internal class OtelPlatformProvider(
107108
override val currentThreadName: String
108109
get() = Thread.currentThread().name
109110

110-
// Crash-specific configuration
111-
// Store crashStoragePath privately since we need a custom getter that logs
112-
private val _crashStoragePath: String = config.crashStoragePath
113-
114-
override val crashStoragePath: String
115-
get() {
116-
// Log the path on first access so developers know where to find crash logs
117-
Logging.info("OneSignal: Crash logs stored at: $_crashStoragePath")
118-
return _crashStoragePath
119-
}
111+
override val crashStoragePath: String by lazy {
112+
val path = config.crashStoragePath
113+
Logging.info("OneSignal: Crash logs stored at: $path")
114+
path
115+
}
120116

121117
override val minFileAgeForReadMillis: Long = 5_000
122118

@@ -141,6 +137,8 @@ internal class OtelPlatformProvider(
141137

142138
override val appIdForHeaders: String
143139
get() = appId ?: ""
140+
141+
override val apiBaseUrl: String = OneSignalService.ONESIGNAL_API_BASE_URL
144142
}
145143

146144
/**

0 commit comments

Comments
 (0)