Skip to content

Commit

Permalink
testing tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jan 17, 2025
1 parent f996835 commit f1170dc
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sealed class Attachment(val id: String) {
}

abstract val attributes: Map<EmbraceAttributeKey, String>
abstract val errorCode: AttachmentErrorCode?

protected fun constructAttributes(
id: String,
Expand All @@ -42,7 +43,7 @@ sealed class Attachment(val id: String) {

private val size: Long = bytes.size.toLong()

private val errorCode: AttachmentErrorCode? = when {
override val errorCode: AttachmentErrorCode? = when {
!counter() -> OVER_MAX_ATTACHMENTS
size > LIMIT_MB -> ATTACHMENT_TOO_LARGE
else -> null
Expand All @@ -64,7 +65,7 @@ sealed class Attachment(val id: String) {
counter: () -> Boolean,
) : Attachment(id) {

private val errorCode: AttachmentErrorCode? = when {
override val errorCode: AttachmentErrorCode? = when {
!counter() -> OVER_MAX_ATTACHMENTS
url.isEmpty() -> UNKNOWN
isNotUuid() -> UNKNOWN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum class SupportedEnvelopeType(

CRASH(Envelope.logEnvelopeType, "p1", Endpoint.LOGS),
SESSION(Envelope.sessionEnvelopeType, "p3", Endpoint.SESSIONS),
ATTACHMENT(null, "p4", Endpoint.ATTACHMENT),
ATTACHMENT(null, "p4", Endpoint.ATTACHMENTS),
LOG(Envelope.logEnvelopeType, "p5", Endpoint.LOGS),
BLOB(Envelope.logEnvelopeType, "p7", Endpoint.LOGS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OkHttpRequestExecutionService(
envelopeType: SupportedEnvelopeType,
payloadType: String,
): ExecutionResult {
val multipart = envelopeType.endpoint == Endpoint.ATTACHMENT
val multipart = envelopeType.endpoint == Endpoint.ATTACHMENTS
val apiRequest = envelopeType.endpoint.getApiRequestFromEndpoint(multipart)
val request = when {
multipart -> prepareMultipartRequest(payloadStream, apiRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ enum class Endpoint(
LOGS("logs", "v2"),
SESSIONS("spans", "v2"),
CONFIG("config", "v2"),
ATTACHMENT("attachment", "v2"),
ATTACHMENTS("attachments", "v2"),
UNKNOWN("unknown", "v1"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal class FakeApiServer(
deliveryTracer.onServerReceivedRequest(endpoint.name)
return when (endpoint) {
Endpoint.LOGS, Endpoint.SESSIONS -> handleEnvelopeRequest(request, endpoint)
Endpoint.ATTACHMENT -> handleAttachmentRequest(request)
Endpoint.ATTACHMENTS -> handleAttachmentRequest(request)

// IMPORTANT NOTE: this response is not used until the SDK next starts!
Endpoint.CONFIG -> handleConfigRequest(request)
Expand Down Expand Up @@ -150,7 +150,7 @@ internal class FakeApiServer(
"logs" -> Endpoint.LOGS
"spans" -> Endpoint.SESSIONS
"config" -> Endpoint.CONFIG
"attachment" -> Endpoint.ATTACHMENT
"attachments" -> Endpoint.ATTACHMENTS
else -> error("Unsupported path $endpoint")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import io.embrace.android.embracesdk.internal.injection.ModuleInitBootstrapper
import io.embrace.android.embracesdk.internal.injection.embraceImplInject
import io.embrace.android.embracesdk.internal.logs.attachments.Attachment
import io.embrace.android.embracesdk.internal.logs.attachments.Attachment.EmbraceHosted
import io.embrace.android.embracesdk.internal.logs.attachments.AttachmentErrorCode.ATTACHMENT_TOO_LARGE
import io.embrace.android.embracesdk.internal.logs.attachments.AttachmentErrorCode.OVER_MAX_ATTACHMENTS
import io.embrace.android.embracesdk.internal.logs.attachments.AttachmentErrorCode.UNKNOWN
import io.embrace.android.embracesdk.internal.payload.PushNotificationBreadcrumb
import io.embrace.android.embracesdk.internal.serialization.truncatedStacktrace
import io.embrace.android.embracesdk.internal.utils.getSafeStackTrace
Expand All @@ -31,6 +34,9 @@ internal class LogsApiDelegate(
private val attachmentService by embraceImplInject(sdkCallChecker) {
bootstrapper.logModule.attachmentService
}
private val logger by embraceImplInject(sdkCallChecker) {
bootstrapper.initModule.logger
}

override fun logInfo(message: String) = logMessage(message, Severity.INFO)
override fun logWarning(message: String) = logMessage(message, Severity.WARNING)
Expand Down Expand Up @@ -91,6 +97,7 @@ internal class LogsApiDelegate(
attachment: ByteArray,
) {
val obj = attachmentService?.createAttachment(attachment) ?: return
logAttachmentErrorIfNeeded(obj)
logMessageImpl(
severity = severity,
message = message,
Expand All @@ -107,6 +114,7 @@ internal class LogsApiDelegate(
attachmentUrl: String,
) {
val obj = attachmentService?.createAttachment(attachmentId, attachmentUrl) ?: return
logAttachmentErrorIfNeeded(obj)
logMessageImpl(
severity = severity,
message = message,
Expand All @@ -115,6 +123,18 @@ internal class LogsApiDelegate(
)
}

private fun logAttachmentErrorIfNeeded(obj: Attachment) {
if (obj.errorCode != null) {
val msg = when (obj.errorCode) {
ATTACHMENT_TOO_LARGE -> "Supplied attachment exceeds 1Mb limit. This attachment will not be uploaded."
OVER_MAX_ATTACHMENTS -> "A maximum of 5 attachments are allowed per session. This attachment will not be uploaded."
UNKNOWN -> "An unknown error occurred while processing the attachment."
null -> null
} ?: return
logger?.logError(msg, RuntimeException(msg))
}
}

override fun logException(
throwable: Throwable,
severity: Severity,
Expand Down
2 changes: 2 additions & 0 deletions examples/ExampleApp/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ dependencies {
implementation(libs.opentelemetry.sdk)

// uncomment to enable debugging through source contained in those modules
// implementation(libs.embrace.android.api)
// implementation(libs.embrace.android.sdk)
// implementation(libs.embrace.android.core)
// implementation(libs.embrace.android.features)
// implementation(libs.embrace.android.payload)
// implementation(libs.embrace.android.delivery)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum class CodeExample(
TRACING("Tracing API"),
ADD_BREADCRUMB("Add Breadcrumb"),
LOG_MESSAGE("Send Log Message"),
LOG_MESSAGE_ATTACHMENT("Log with Attachment"),
RECORD_NETWORK_REQUEST("Network Requests"),
SESSION_PROPERTIES("Session Properties"),
END_SESSION("End Session"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.embrace.android.exampleapp.ui.examples.AddBreadcrumbExample
import io.embrace.android.exampleapp.ui.examples.AnrDetectionExample
import io.embrace.android.exampleapp.ui.examples.EndSessionExample
import io.embrace.android.exampleapp.ui.examples.JvmCrashExample
import io.embrace.android.exampleapp.ui.examples.LogMessageAttachmentsExample
import io.embrace.android.exampleapp.ui.examples.NdkCrashExample
import io.embrace.android.exampleapp.ui.examples.LogMessageExample
import io.embrace.android.exampleapp.ui.examples.NetworkRequestExample
Expand All @@ -20,6 +21,7 @@ fun ExampleContent(example: CodeExample) {
CodeExample.TRACING -> TracingApiExample()
CodeExample.ADD_BREADCRUMB -> AddBreadcrumbExample()
CodeExample.LOG_MESSAGE -> LogMessageExample()
CodeExample.LOG_MESSAGE_ATTACHMENT -> LogMessageAttachmentsExample()
CodeExample.RECORD_NETWORK_REQUEST -> NetworkRequestExample()
CodeExample.SESSION_PROPERTIES -> SessionPropertiesExample()
CodeExample.END_SESSION -> EndSessionExample()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.embrace.android.exampleapp.ui.examples

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.embrace.android.embracesdk.Embrace
import io.embrace.android.embracesdk.Severity
import io.embrace.android.exampleapp.ui.RadioButtonList
import java.util.UUID

@Composable
fun LogMessageAttachmentsExample() {
var textValue by remember { mutableStateOf("My log message") }
var severityValue by remember { mutableStateOf(Severity.INFO) }
var includeProps by remember { mutableStateOf(true) }
var hostFileOn3rdPartyServer by remember { mutableStateOf(false) }

TextField(value = textValue, onValueChange = {
textValue = it
})
RadioButtonList(
items = Severity.entries,
selectedItem = severityValue
) {
severityValue = it
}

Row {
Checkbox(
checked = includeProps,
onCheckedChange = {
includeProps = it
}
)
Text("Include properties", Modifier.padding(top = 8.dp))
}

Row {
Checkbox(
checked = hostFileOn3rdPartyServer,
onCheckedChange = {
hostFileOn3rdPartyServer = it
}
)
Text("Host files on a 3rd party server (not Embrace)", Modifier.padding(top = 8.dp))
}
Spacer(Modifier.padding(8.dp))
Button(onClick = {
val properties = when (includeProps) {
true -> mapOf(
"my_custom_key" to "my_custom_value"
)
else -> null
}

if (hostFileOn3rdPartyServer) {
Embrace.getInstance().logMessage(
message = textValue,
severity = severityValue,
properties = properties,
attachmentId = UUID.randomUUID().toString(),
attachmentUrl = "https://example.com/my-attachment",
)
} else {
Embrace.getInstance().logMessage(
message = textValue,
severity = severityValue,
properties = properties,
attachment = "Hello, world!".toByteArray(),
)
}
}) {
Text("Send Log")
}
}
11 changes: 7 additions & 4 deletions examples/ExampleApp/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-toolin
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
embrace-android-sdk = { group = "io.embrace", name = "embrace-android-sdk", version.ref = "embrace" }
embrace-android-core = { group = "io.embrace", name = "embrace-android-core", version.ref = "embrace" }
embrace-android-features = { group = "io.embrace", name = "embrace-android-features", version.ref = "embrace" }
embrace-android-payload = { group = "io.embrace", name = "embrace-android-payload", version.ref = "embrace" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
opentelemetry-bom = { group = "io.opentelemetry", name = "opentelemetry-bom", version.ref = "otel" }
opentelemetry-api = { group = "io.opentelemetry", name = "opentelemetry-api"}
opentelemetry-sdk = { group = "io.opentelemetry", name = "opentelemetry-sdk"}

embrace-android-api = { group = "io.embrace", name = "embrace-android-api", version.ref = "embrace" }
embrace-android-sdk = { group = "io.embrace", name = "embrace-android-sdk", version.ref = "embrace" }
embrace-android-core = { group = "io.embrace", name = "embrace-android-core", version.ref = "embrace" }
embrace-android-features = { group = "io.embrace", name = "embrace-android-features", version.ref = "embrace" }
embrace-android-payload = { group = "io.embrace", name = "embrace-android-payload", version.ref = "embrace" }
embrace-android-delivery = { group = "io.embrace", name = "embrace-android-delivery", version.ref = "embrace" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Expand Down

0 comments on commit f1170dc

Please sign in to comment.