|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.android |
| 7 | + |
| 8 | +import android.app.Application |
| 9 | +import android.os.Build |
| 10 | +import io.opentelemetry.android.common.RumConstants.RUM_SDK_VERSION |
| 11 | +import io.opentelemetry.sdk.resources.Resource |
| 12 | +import io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME |
| 13 | +import io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION |
| 14 | +import io.opentelemetry.semconv.incubating.DeviceIncubatingAttributes.DEVICE_MANUFACTURER |
| 15 | +import io.opentelemetry.semconv.incubating.DeviceIncubatingAttributes.DEVICE_MODEL_IDENTIFIER |
| 16 | +import io.opentelemetry.semconv.incubating.DeviceIncubatingAttributes.DEVICE_MODEL_NAME |
| 17 | +import io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_DESCRIPTION |
| 18 | +import io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_NAME |
| 19 | +import io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_TYPE |
| 20 | +import io.opentelemetry.semconv.incubating.OsIncubatingAttributes.OS_VERSION |
| 21 | + |
| 22 | +private const val DEFAULT_APP_NAME = "unknown_service:android" |
| 23 | + |
| 24 | +object AndroidResource { |
| 25 | + @JvmStatic |
| 26 | + fun createDefault(application: Application): Resource { |
| 27 | + val appName = readAppName(application) |
| 28 | + val resourceBuilder = |
| 29 | + Resource.getDefault().toBuilder().put(SERVICE_NAME, appName) |
| 30 | + val appVersion = readAppVersion(application) |
| 31 | + appVersion?.let { resourceBuilder.put(SERVICE_VERSION, it) } |
| 32 | + |
| 33 | + return resourceBuilder |
| 34 | + .put(RUM_SDK_VERSION, BuildConfig.OTEL_ANDROID_VERSION) |
| 35 | + .put(DEVICE_MODEL_NAME, Build.MODEL) |
| 36 | + .put(DEVICE_MODEL_IDENTIFIER, Build.MODEL) |
| 37 | + .put(DEVICE_MANUFACTURER, Build.MANUFACTURER) |
| 38 | + .put(OS_NAME, "Android") |
| 39 | + .put(OS_TYPE, "linux") |
| 40 | + .put(OS_VERSION, Build.VERSION.RELEASE) |
| 41 | + .put(OS_DESCRIPTION, oSDescription) |
| 42 | + .build() |
| 43 | + } |
| 44 | + |
| 45 | + private fun readAppName(application: Application): String = |
| 46 | + try { |
| 47 | + val stringId = |
| 48 | + application.applicationContext.applicationInfo.labelRes |
| 49 | + application.applicationContext.getString(stringId) |
| 50 | + } catch (_: Exception) { |
| 51 | + DEFAULT_APP_NAME |
| 52 | + } |
| 53 | + |
| 54 | + private fun readAppVersion(application: Application): String? { |
| 55 | + val ctx = application.applicationContext |
| 56 | + return try { |
| 57 | + val packageInfo = ctx.packageManager.getPackageInfo(ctx.packageName, 0) |
| 58 | + packageInfo.versionName |
| 59 | + } catch (_: Exception) { |
| 60 | + null |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private val oSDescription: String |
| 65 | + get() { |
| 66 | + val osDescriptionBuilder = StringBuilder() |
| 67 | + return osDescriptionBuilder |
| 68 | + .append("Android Version ") |
| 69 | + .append(Build.VERSION.RELEASE) |
| 70 | + .append(" (Build ") |
| 71 | + .append(Build.ID) |
| 72 | + .append(" API level ") |
| 73 | + .append(Build.VERSION.SDK_INT) |
| 74 | + .append(")") |
| 75 | + .toString() |
| 76 | + } |
| 77 | +} |
0 commit comments