Skip to content

Commit

Permalink
refactor: simplify otel limits
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Dec 9, 2024
1 parent f3c3d7f commit 1d3cd32
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ interface SessionSpanWriter {
/**
* Add the given key-value pair as an Attribute to the session span
*/
fun addSystemAttribute(attribute: SpanAttributeData)
fun addAttribute(attribute: SpanAttributeData)

/**
* Remove the attribute with the given key
*/
fun removeSystemAttribute(key: String)
fun removeAttribute(key: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class EmbraceSessionProperties(
}
temporary[sanitizedKey] = sanitizedValue
}
writer.addSystemAttribute(
writer.addAttribute(
SpanAttributeData(
sanitizedKey.toSessionPropertyAttributeName(),
sanitizedValue
Expand All @@ -79,7 +79,7 @@ internal class EmbraceSessionProperties(
preferencesService.permanentSessionProperties = permanentProperties()
existed = true
}
writer.removeSystemAttribute(sanitizedKey.toSessionPropertyAttributeName())
writer.removeAttribute(sanitizedKey.toSessionPropertyAttributeName())
return existed
}
}
Expand All @@ -98,7 +98,7 @@ internal class EmbraceSessionProperties(

fun addPermPropsToSessionSpan() {
permanentProperties().entries.forEach {
writer.addSystemAttribute(
writer.addAttribute(
SpanAttributeData(
it.key.toSessionPropertyAttributeName(),
it.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package io.embrace.android.embracesdk.internal.config.instrumented

import io.embrace.android.embracesdk.internal.config.instrumented.schema.OtelLimitsConfig

internal fun OtelLimitsConfig.isNameValid(str: String, internal: Boolean): Boolean =
str.isNotBlank() && ((internal && str.length <= getMaxInternalNameLength()) || str.length <= getMaxNameLength())
internal fun OtelLimitsConfig.isNameValid(str: String): Boolean =
str.isNotBlank() && str.length <= getMaxNameLength()

internal fun OtelLimitsConfig.isAttributeValid(key: String, value: String, internal: Boolean) =
((internal && key.length <= getMaxInternalAttributeKeyLength()) || key.length <= getMaxCustomAttributeKeyLength()) &&
((internal && value.length <= getMaxInternalAttributeValueLength()) || value.length <= getMaxCustomAttributeValueLength())
internal fun OtelLimitsConfig.isAttributeValid(key: String, value: String) =
key.length <= getMaxAttributeKeyLength() && value.length <= getMaxAttributeValueLength()
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ internal class OpenTelemetrySdk(
SpanLimits
.getDefault()
.toBuilder()
.setMaxNumberOfEvents(limits.getMaxTotalEventCount())
.setMaxNumberOfAttributes(limits.getMaxTotalAttributeCount())
.setMaxNumberOfEvents(limits.getMaxEventCount())
.setMaxNumberOfAttributes(limits.getMaxAttributeCount())
.build()
)
.setClock(openTelemetryClock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ internal class SessionOrchestratorImpl(
private fun updatePeriodicCacheAttrs() {
val now = clock.now().millisToNanos()
val attr = SpanAttributeData(embHeartbeatTimeUnixNano.name, now.toString())
sessionSpanWriter.addSystemAttribute(attr)
sessionSpanWriter.addSystemAttribute(SpanAttributeData(embTerminated.name, true.toString()))
sessionSpanWriter.addAttribute(attr)
sessionSpanWriter.addAttribute(SpanAttributeData(embTerminated.name, true.toString()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@ internal class SessionSpanAttrPopulatorImpl(

override fun populateSessionSpanStartAttrs(session: SessionZygote) {
with(sessionSpanWriter) {
addSystemAttribute(SpanAttributeData(embColdStart.name, session.isColdStart.toString()))
addSystemAttribute(SpanAttributeData(embSessionNumber.name, session.number.toString()))
addSystemAttribute(SpanAttributeData(embState.name, session.appState.name.lowercase(Locale.US)))
addSystemAttribute(SpanAttributeData(embCleanExit.name, false.toString()))
addSystemAttribute(SpanAttributeData(embTerminated.name, true.toString()))
addAttribute(SpanAttributeData(embColdStart.name, session.isColdStart.toString()))
addAttribute(SpanAttributeData(embSessionNumber.name, session.number.toString()))
addAttribute(SpanAttributeData(embState.name, session.appState.name.lowercase(Locale.US)))
addAttribute(SpanAttributeData(embCleanExit.name, false.toString()))
addAttribute(SpanAttributeData(embTerminated.name, true.toString()))

session.startType.toString().lowercase(Locale.US).let {
addSystemAttribute(SpanAttributeData(embSessionStartType.name, it))
addAttribute(SpanAttributeData(embSessionStartType.name, it))
}
}
}

override fun populateSessionSpanEndAttrs(endType: LifeEventType?, crashId: String?, coldStart: Boolean) {
with(sessionSpanWriter) {
addSystemAttribute(SpanAttributeData(embCleanExit.name, true.toString()))
addSystemAttribute(SpanAttributeData(embTerminated.name, false.toString()))
addAttribute(SpanAttributeData(embCleanExit.name, true.toString()))
addAttribute(SpanAttributeData(embTerminated.name, false.toString()))
crashId?.let {
addSystemAttribute(SpanAttributeData(embCrashId.name, crashId))
addAttribute(SpanAttributeData(embCrashId.name, crashId))
}
endType?.toString()?.lowercase(Locale.US)?.let {
addSystemAttribute(SpanAttributeData(embSessionEndType.name, it))
addAttribute(SpanAttributeData(embSessionEndType.name, it))
}
if (coldStart) {
startupService.getSdkStartupDuration()?.let { duration ->
addSystemAttribute(SpanAttributeData(embSessionStartupDuration.name, duration.toString()))
addAttribute(SpanAttributeData(embSessionStartupDuration.name, duration.toString()))
}
}

val logCount = logService.getErrorLogsCount()
addSystemAttribute(SpanAttributeData(embErrorLogCount.name, logCount.toString()))
addAttribute(SpanAttributeData(embErrorLogCount.name, logCount.toString()))

metadataService.getDiskUsage()?.deviceDiskFree?.let { free ->
addSystemAttribute(SpanAttributeData(embFreeDiskBytes.name, free.toString()))
addAttribute(SpanAttributeData(embFreeDiskBytes.name, free.toString()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal class CurrentSessionSpanImpl(
}

override fun getSessionId(): String {
return sessionSpan.get()?.getSystemAttribute(SessionIncubatingAttributes.SESSION_ID) ?: ""
return sessionSpan.get()?.getAttribute(SessionIncubatingAttributes.SESSION_ID) ?: ""
}

override fun readySession(): Boolean {
Expand Down Expand Up @@ -124,8 +124,8 @@ internal class CurrentSessionSpanImpl(
} else {
val crashTime = openTelemetryClock.now().nanosToMillis()
spanRepository.failActiveSpans(crashTime)
endingSessionSpan.setSystemAttribute(
appTerminationCause.key.attributeKey,
endingSessionSpan.addAttribute(
appTerminationCause.key.attributeKey.key,
appTerminationCause.value
)
endingSessionSpan.stop(errorCode = ErrorCode.FAILURE, endTimeMs = crashTime)
Expand All @@ -139,7 +139,7 @@ internal class CurrentSessionSpanImpl(

override fun addEvent(schemaType: SchemaType, startTimeMs: Long): Boolean {
val currentSession = sessionSpan.get() ?: return false
return currentSession.addSystemEvent(
return currentSession.addEvent(
schemaType.fixedObjectName.toEmbraceObjectName(),
startTimeMs,
schemaType.attributes() + schemaType.telemetryType.toEmbraceKeyValuePair()
Expand All @@ -148,17 +148,17 @@ internal class CurrentSessionSpanImpl(

override fun removeEvents(type: EmbType) {
val currentSession = sessionSpan.get() ?: return
currentSession.removeSystemEvents(type)
currentSession.removeEvents(type)
}

override fun addSystemAttribute(attribute: SpanAttributeData) {
override fun addAttribute(attribute: SpanAttributeData) {
val currentSession = sessionSpan.get() ?: return
currentSession.addSystemAttribute(attribute.key, attribute.value)
currentSession.addAttribute(attribute.key, attribute.value)
}

override fun removeSystemAttribute(key: String) {
override fun removeAttribute(key: String) {
val currentSession = sessionSpan.get() ?: return
currentSession.removeSystemAttribute(key)
currentSession.removeAttribute(key)
}

/**
Expand All @@ -175,7 +175,7 @@ internal class CurrentSessionSpanImpl(
private = false,
).apply {
start(startTimeMs = startTimeMs)
setSystemAttribute(SessionIncubatingAttributes.SESSION_ID, Uuid.getEmbUuid())
addAttribute(SessionIncubatingAttributes.SESSION_ID.key, Uuid.getEmbUuid())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ internal fun LogRecordBuilder.setAttribute(
*/
fun AttributesBuilder.fromMap(
attributes: Map<String, String>,
internal: Boolean,
limits: OtelLimitsConfig = InstrumentedConfigImpl.otelLimits,
): AttributesBuilder {
attributes.filter {
limits.isAttributeValid(it.key, it.value, internal) || it.key.isValidLongValueAttribute()
limits.isAttributeValid(it.key, it.value) || it.key.isValidLongValueAttribute()
}.forEach {
put(it.key, it.value)
}
Expand Down
Loading

0 comments on commit 1d3cd32

Please sign in to comment.