Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use secret Sentry DSN value #4210

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }}
ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }}
ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }}
ELEMENT_ANDROID_SENTRY_DSN: ${{ secrets.ELEMENT_ANDROID_SENTRY_DSN }}
run: ./gradlew :app:assembleGplayDebug app:assembleFDroidDebug -PallWarningsAsErrors=true $CI_GRADLE_ARG_PROPERTIES
- name: Upload debug APKs
if: ${{ matrix.variant == 'debug' }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build_enterprise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }}
ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }}
ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }}
ELEMENT_ANDROID_SENTRY_DSN: ${{ secrets.ELEMENT_ANDROID_SENTRY_DSN }}
run: ./gradlew :app:assembleGplayDebug -PallWarningsAsErrors=true $CI_GRADLE_ARG_PROPERTIES
- name: Upload debug Enterprise APKs
if: ${{ matrix.variant == 'debug' }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }}
ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }}
ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }}
ELEMENT_ANDROID_SENTRY_DSN: ${{ secrets.ELEMENT_ANDROID_SENTRY_DSN }}
ELEMENT_ANDROID_NIGHTLY_KEYID: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_KEYID }}
ELEMENT_ANDROID_NIGHTLY_KEYPASSWORD: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_KEYPASSWORD }}
ELEMENT_ANDROID_NIGHTLY_STOREPASSWORD: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_STOREPASSWORD }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/nightly_enterprise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }}
ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }}
ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }}
ELEMENT_ANDROID_SENTRY_DSN: ${{ secrets.ELEMENT_ANDROID_SENTRY_DSN }}
ELEMENT_ANDROID_NIGHTLY_KEYID: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_KEYID }}
ELEMENT_ANDROID_NIGHTLY_KEYPASSWORD: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_KEYPASSWORD }}
ELEMENT_ANDROID_NIGHTLY_STOREPASSWORD: ${{ secrets.ELEMENT_ANDROID_NIGHTLY_STOREPASSWORD }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
ELEMENT_ANDROID_MAPTILER_API_KEY: ${{ secrets.MAPTILER_KEY }}
ELEMENT_ANDROID_MAPTILER_LIGHT_MAP_ID: ${{ secrets.MAPTILER_LIGHT_MAP_ID }}
ELEMENT_ANDROID_MAPTILER_DARK_MAP_ID: ${{ secrets.MAPTILER_DARK_MAP_ID }}
ELEMENT_ANDROID_SENTRY_DSN: ${{ secrets.ELEMENT_ANDROID_SENTRY_DSN }}
run: ./gradlew bundleGplayRelease $CI_GRADLE_ARG_PROPERTIES
- name: Upload bundle as artifact
uses: actions/upload-artifact@v4
Expand Down
12 changes: 9 additions & 3 deletions docs/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

<!--- TOC -->

* [TODO](#todo)
* [Sentry](#sentry)

<!--- END -->

## TODO
## Sentry

There is no analytics in the project yet.
To make Sentry analytics and bug reporting work, you need to provide a Sentry DSN in the `local.properties` file, or set the `ELEMENT_ANDROID_SENTRY_DSN` environment variable.

The format used to add the DSN to your `local.properties` file is the following:

```properties
services.analyticsproviders.sentry.dsn=https://your-sentry-dsn/project-id
```
9 changes: 1 addition & 8 deletions features/location/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@
* Please see LICENSE files in the repository root for full details.
*/

import java.util.Properties
import extension.readLocalProperty

plugins {
id("io.element.android-compose-library")
id("kotlin-parcelize")
}

fun readLocalProperty(name: String): String? = Properties().apply {
try {
load(rootProject.file("local.properties").reader())
} catch (ignored: java.io.IOException) {
}
}.getProperty(name)

android {
namespace = "io.element.android.features.location.api"

Expand Down
8 changes: 8 additions & 0 deletions plugins/src/main/kotlin/extension/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.gradle.api.provider.ValueSourceParameters
import org.gradle.process.ExecOperations
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.util.Properties
import javax.inject.Inject

abstract class GitRevisionValueSource : ValueSource<String, ValueSourceParameters.None> {
Expand Down Expand Up @@ -47,3 +48,10 @@ private fun ExecOperations.runCommand(cmd: String): String {
}
return String(outputStream.toByteArray()).trim()
}

fun Project.readLocalProperty(name: String): String? = Properties().apply {
try {
load(rootProject.file("local.properties").reader())
} catch (ignored: IOException) {
}
}.getProperty(name)
16 changes: 16 additions & 0 deletions services/analyticsproviders/sentry/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import extension.readLocalProperty
import extension.setupAnvil

/*
Expand All @@ -12,6 +13,21 @@ plugins {

android {
namespace = "io.element.android.services.analyticsproviders.sentry"

buildFeatures {
buildConfig = true
}

defaultConfig {
buildConfigField(
type = "String",
name = "SENTRY_DSN",
value = (System.getenv("ELEMENT_ANDROID_SENTRY_DSN")
?: readLocalProperty("services.analyticsproviders.sentry.dsn")
?: ""
).let { "\"$it\"" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the doc:

image

So it's maybe safer to set default value to null, or do not call options.dsn = SentryConfig.DNS if SentryConfig.DNS is empty (maybe the latter is simpler, and we may Timber a warning in this case).

)
}
}

setupAnvil()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.element.android.services.analyticsproviders.api.AnalyticsProvider
import io.element.android.services.analyticsproviders.sentry.log.analyticsTag
import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.android.core.SentryAndroid
import timber.log.Timber
Expand All @@ -35,13 +36,20 @@
override fun init() {
Timber.tag(analyticsTag.value).d("Initializing Sentry")
if (Sentry.isEnabled()) return

val dsn = if (SentryConfig.DSN.isNotBlank()) {
SentryConfig.DSN

Check warning on line 41 in services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt

View check run for this annotation

Codecov / codecov/patch

services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt#L41

Added line #L41 was not covered by tests
} else {
Timber.w("No Sentry DSN provided, Sentry will not be initialized")

Check warning on line 43 in services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt

View check run for this annotation

Codecov / codecov/patch

services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt#L43

Added line #L43 was not covered by tests
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of something like

val dsn = SentryConfig.DSN.takeIf { it.isNotBlank() }
if (dsn == null)
    Timber.w("No Sentry DSN provided, Sentry will not report anything")
}

but this is probably equivalent.


SentryAndroid.init(context) { options ->
options.dsn = SentryConfig.DNS
options.dsn = dsn

Check warning on line 48 in services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt

View check run for this annotation

Codecov / codecov/patch

services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt#L48

Added line #L48 was not covered by tests
options.beforeSend = SentryOptions.BeforeSendCallback { event, _ -> event }
options.tracesSampleRate = 1.0
options.isEnableUserInteractionTracing = true
options.environment = buildMeta.buildType.toSentryEnv()
options.diagnosticLevel
}
}

Expand All @@ -51,9 +59,11 @@
}

override fun capture(event: VectorAnalyticsEvent) {
Sentry.captureMessage("Event: ${event.getName()}", SentryLevel.INFO)

Check warning on line 62 in services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt

View check run for this annotation

Codecov / codecov/patch

services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt#L62

Added line #L62 was not covered by tests
}

override fun screen(screen: VectorAnalyticsScreen) {
Sentry.captureMessage("Screen: ${screen.getName()}", SentryLevel.INFO)

Check warning on line 66 in services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt

View check run for this annotation

Codecov / codecov/patch

services/analyticsproviders/sentry/src/main/kotlin/io/element/android/services/analyticsproviders/sentry/SentryAnalyticsProvider.kt#L66

Added line #L66 was not covered by tests
}

override fun updateUserProperties(userProperties: UserProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package io.element.android.services.analyticsproviders.sentry

object SentryConfig {
const val NAME = "Sentry"
const val DNS = "https://[email protected]/59"
const val DSN = BuildConfig.SENTRY_DSN
const val ENV_DEBUG = "DEBUG"
const val ENV_RELEASE = "RELEASE"
}