Skip to content

Commit

Permalink
Fix to add custom properties to session span
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielDemarco committed Jul 29, 2024
1 parent 944abf9 commit 426c54f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class SpansPayloadBuilder {
spanData = try JSONDecoder().decode(SpanData.self, from: rawData)
}

return SessionSpanUtils.payload(from: sessionRecord, spanData: spanData, sessionNumber: sessionNumber)
let properties = try storage.fetchCustomPropertiesForSessionId(sessionRecord.id)

return SessionSpanUtils.payload(from: sessionRecord, spanData: spanData, properties: properties, sessionNumber: sessionNumber)

} catch {
Embrace.logger.warning("Error fetching span for session \(sessionRecord.id):\n\(error.localizedDescription)")
Expand Down
21 changes: 19 additions & 2 deletions Sources/EmbraceCore/Session/SessionSpanUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ struct SessionSpanUtils {
static func payload(
from session: SessionRecord,
spanData: SpanData? = nil,
properties: [MetadataRecord],
sessionNumber: Int
) -> SpanPayload {
return SpanPayload(from: session, spanData: spanData, sessionNumber: sessionNumber)
return SpanPayload(from: session, spanData: spanData, properties: properties, sessionNumber: sessionNumber)
}
}

fileprivate extension SpanPayload {
init(from session: SessionRecord, spanData: SpanData? = nil, sessionNumber: Int) {
init(
from session: SessionRecord,
spanData: SpanData? = nil,
properties: [MetadataRecord],
sessionNumber: Int
) {
self.traceId = session.traceId
self.spanId = session.spanId
self.parentSpanId = nil
Expand Down Expand Up @@ -111,6 +117,17 @@ fileprivate extension SpanPayload {
))
}

var mutableProperties = properties
mutableProperties.removeAll(where: { $0.key.contains("emb.user") })
attributeArray.append(
contentsOf: mutableProperties.map {
Attribute(
key: String(format: "emb.properties.%@", $0.key),
value: $0.value.description
)
}
)

self.attributes = attributeArray

if let spanData = spanData {
Expand Down

0 comments on commit 426c54f

Please sign in to comment.