Skip to content

Commit

Permalink
Merge branch 'core_data_migration'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Package.swift
#	Sources/EmbraceCore/Public/Metadata/MetadataHandler.swift
#	Sources/EmbraceCore/Public/Metadata/MetadataRecordTmp.swift
#	Sources/EmbraceCoreDataInternal/CoreDataWrapper.swift
#	Sources/EmbraceStorageInternal/Records/EmbraceStorage+Metadata.swift
#	Sources/EmbraceUploadInternal/Cache/EmbraceUploadCache.swift
#	Sources/EmbraceUploadInternal/Cache/UploadDataRecord.swift
#	Tests/EmbraceCoreDataInternalTests/CoreDataWrapperTests.swift
#	Tests/EmbraceCoreTests/Public/Metadata/MetadataHandlerTests.swift
#	Tests/EmbraceStorageInternalTests/MetadataRecordTests.swift
#	Tests/EmbraceUploadInternalTests/EmbraceUploadCacheTests+ClearDataDate.swift
#	Tests/EmbraceUploadInternalTests/EmbraceUploadCacheTests.swift
  • Loading branch information
NachoEmbrace committed Feb 6, 2025
2 parents 987d07b + 3a9b43a commit 0eeb747
Show file tree
Hide file tree
Showing 144 changed files with 2,863 additions and 5,812 deletions.
6 changes: 0 additions & 6 deletions EmbraceIO.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ Pod::Spec.new do |spec|
storage.vendored_frameworks = "xcframeworks/EmbraceStorageInternal.xcframework"
storage.dependency "EmbraceIO/EmbraceCommonInternal"
storage.dependency "EmbraceIO/EmbraceSemantics"
storage.dependency "EmbraceIO/GRDB"
end

spec.subspec 'EmbraceUploadInternal' do |upload|
upload.vendored_frameworks = "xcframeworks/EmbraceUploadInternal.xcframework"
upload.dependency "EmbraceIO/EmbraceCommonInternal"
upload.dependency "EmbraceIO/EmbraceOTelInternal"
upload.dependency "EmbraceIO/GRDB"
end

spec.subspec 'EmbraceCrashlyticsSupport' do |cs|
Expand Down Expand Up @@ -110,10 +108,6 @@ Pod::Spec.new do |spec|
otelSdk.dependency "EmbraceIO/OpenTelemetryApi"
end

spec.subspec 'GRDB' do |grdb|
grdb.vendored_frameworks = "xcframeworks/GRDB.xcframework"
end

spec.subspec 'KSCrash' do |kscrash|
kscrash.dependency "EmbraceIO/KSCrashCore"
kscrash.dependency "EmbraceIO/KSCrashRecording"
Expand Down
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{
"pins" : [
{
"identity" : "grdb.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/groue/GRDB.swift.git",
"state" : {
"revision" : "dd6b98ce04eda39aa22f066cd421c24d7236ea8a",
"version" : "6.29.1"
}
},
{
"identity" : "grpc-swift",
"kind" : "remoteSourceControl",
Expand Down
23 changes: 9 additions & 14 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import PackageDescription

let packageSettings = PackageSettings(
productTypes: [
"GRDB": .framework,
"KSCrash": .framework,
"OpenTelemetrySdk": .framework,
"OpenTelemetryApi": .framework
Expand Down Expand Up @@ -37,10 +36,6 @@ let package = Package(
.package(
url: "https://github.com/open-telemetry/opentelemetry-swift",
exact: "1.13.0"
),
.package(
url: "https://github.com/groue/GRDB.swift",
.upToNextMinor(from: "6.29.1")
)
],
targets: [
Expand All @@ -62,8 +57,7 @@ let package = Package(
"EmbraceIO",
"EmbraceCore",
"EmbraceCrash",
"TestSupport",
.product(name: "GRDB", package: "GRDB.swift")
"TestSupport"
]
),

Expand Down Expand Up @@ -91,8 +85,7 @@ let package = Package(
dependencies: [
"EmbraceCore",
"TestSupport",
"TestSupportObjc",
.product(name: "GRDB", package: "GRDB.swift")
"TestSupportObjc"
],
resources: [
.copy("Mocks/")
Expand All @@ -101,7 +94,10 @@ let package = Package(

// common --------------------------------------------------------------------
.target(
name: "EmbraceCommonInternal"
name: "EmbraceCommonInternal",
dependencies: [
.product(name: "OpenTelemetrySdk", package: "opentelemetry-swift")
]
),
.testTarget(
name: "EmbraceCommonInternalTests",
Expand Down Expand Up @@ -190,8 +186,8 @@ let package = Package(
name: "EmbraceStorageInternal",
dependencies: [
"EmbraceCommonInternal",
"EmbraceSemantics",
.product(name: "GRDB", package: "GRDB.swift")
"EmbraceCoreDataInternal",
"EmbraceSemantics"
]
),
.testTarget(
Expand All @@ -208,8 +204,7 @@ let package = Package(
dependencies: [
"EmbraceCommonInternal",
"EmbraceOTelInternal",
"EmbraceCoreDataInternal",
.product(name: "GRDB", package: "GRDB.swift")
"EmbraceCoreDataInternal"
]
),
.testTarget(
Expand Down
6 changes: 2 additions & 4 deletions Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ let project = Project(
dependencies: [
.target(name: "EmbraceCommonInternal"),
.target(name: "EmbraceSemantics"),
.external(name: "OpenTelemetryApi"),
.external(name: "GRDB")
.external(name: "OpenTelemetryApi")
],
settings: .settings(base: [
"SKIP_INSTALL": "NO",
Expand All @@ -168,8 +167,7 @@ let project = Project(
sources: ["Sources/EmbraceUploadInternal/**"],
dependencies: [
.target(name: "EmbraceCommonInternal"),
.target(name: "EmbraceOTelInternal"),
.external(name: "GRDB")
.target(name: "EmbraceOTelInternal")
],
settings: .settings(base: [
"SKIP_INSTALL": "NO",
Expand Down
28 changes: 28 additions & 0 deletions Sources/EmbraceCommonInternal/Storage/Model/EmbraceLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation
import OpenTelemetryApi

public protocol EmbraceLog {
var idRaw: String { get set }
var processIdRaw: String { get set }
var severityRaw: Int { get set }
var body: String { get set }
var timestamp: Date { get set }

func allAttributes() -> [EmbraceLogAttribute]
func attribute(forKey key: String) -> EmbraceLogAttribute?
func setAttributeValue(value: AttributeValue, forKey key: String)
}

public extension EmbraceLog {
var processId: ProcessIdentifier? {
return ProcessIdentifier(hex: processIdRaw)
}

var severity: LogSeverity {
return LogSeverity(rawValue: severityRaw) ?? .info
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation
import OpenTelemetryApi

public enum EmbraceLogAttributeType: Int {
case string, int, double, bool
}

public protocol EmbraceLogAttribute {
var key: String { get set }
var valueRaw: String { get set }
var typeRaw: Int { get set }
}

public extension EmbraceLogAttribute {

var value: AttributeValue {
get {
let type = EmbraceLogAttributeType(rawValue: typeRaw) ?? .string

switch type {
case .int: return AttributeValue(Int(valueRaw) ?? 0)
case .double: return AttributeValue(Double(valueRaw) ?? 0)
case .bool: return AttributeValue(Bool(valueRaw) ?? false)
default: return AttributeValue(valueRaw)
}
}

set {
valueRaw = newValue.description
typeRaw = typeForValue(newValue).rawValue
}
}

func typeForValue(_ value: AttributeValue) -> EmbraceLogAttributeType {
switch value {
case .int: return .int
case .double: return .double
case .bool: return .bool
default: return .string
}
}
}
49 changes: 49 additions & 0 deletions Sources/EmbraceCommonInternal/Storage/Model/EmbraceMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation

public enum MetadataRecordType: String, Codable {
/// Resource that is attached to session and logs data
case resource

/// Embrace-generated resource that is deemed required and cannot be removed by the user of the SDK
case requiredResource

/// Custom property attached to session and logs data and that can be manipulated by the user of the SDK
case customProperty

/// Persona tag attached to session and logs data and that can be manipulated by the user of the SDK
case personaTag
}

public enum MetadataRecordLifespan: String, Codable {
/// Value tied to a specific session
case session

/// Value tied to multiple sessions within a single process
case process

/// Value tied to all sessions until explicitly removed
case permanent
}

public protocol EmbraceMetadata {
var key: String { get set }
var value: String { get set }
var typeRaw: String { get set }
var lifespanRaw: String { get set }
var lifespanId: String { get set }
var collectedAt: Date { get set }
}

public extension EmbraceMetadata {
var type: MetadataRecordType? {
return MetadataRecordType(rawValue: typeRaw)
}

var lifespan: MetadataRecordLifespan? {
return MetadataRecordLifespan(rawValue: lifespanRaw)
}
}
30 changes: 30 additions & 0 deletions Sources/EmbraceCommonInternal/Storage/Model/EmbraceSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation

public protocol EmbraceSession {
var idRaw: String { get set }
var processIdRaw: String { get set }
var state: String { get set }
var traceId: String { get set }
var spanId: String { get set }
var startTime: Date { get set }
var endTime: Date? { get set }
var lastHeartbeatTime: Date { get set }
var crashReportId: String? { get set }
var coldStart: Bool { get set }
var cleanExit: Bool { get set }
var appTerminated: Bool { get set }
}

public extension EmbraceSession {
var id: SessionIdentifier? {
return SessionIdentifier(string: idRaw)
}

var processId: ProcessIdentifier? {
return ProcessIdentifier(hex: processIdRaw)
}
}
26 changes: 26 additions & 0 deletions Sources/EmbraceCommonInternal/Storage/Model/EmbraceSpan.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright © 2025 Embrace Mobile, Inc. All rights reserved.
//

import Foundation

public protocol EmbraceSpan {
var id: String { get set }
var name: String { get set }
var traceId: String { get set }
var typeRaw: String { get set }
var data: Data { get set }
var startTime: Date { get set }
var endTime: Date? { get set }
var processIdRaw: String { get set }
}

public extension EmbraceSpan {
var type: SpanType? {
return SpanType(rawValue: typeRaw)
}

var processId: ProcessIdentifier? {
return ProcessIdentifier(hex: processIdRaw)
}
}
4 changes: 2 additions & 2 deletions Sources/EmbraceCore/Capture/CaptureServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ final class CaptureServices {
}

@objc func onSessionStart(notification: Notification) {
if let session = notification.object as? SessionRecord {
crashReporter?.currentSessionId = session.id.toString
if let session = notification.object as? EmbraceSession {
crashReporter?.currentSessionId = session.idRaw
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class NetworkPayloadCaptureHandler {
active = true
rulesTriggeredMap.removeAll()

currentSessionId = (notification.object as? SessionRecord)?.id
currentSessionId = (notification.object as? EmbraceSession)?.id
}

@objc func onSessionEnd() {
Expand Down
20 changes: 7 additions & 13 deletions Sources/EmbraceCore/Capture/ResourceCaptureService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ class ResourceCaptureService: CaptureService {

extension EmbraceStorage: ResourceCaptureServiceHandler {
func addResource(key: String, value: AttributeValue) {
do {
_ = try addMetadata(
MetadataRecord(
key: key,
value: value,
type: .requiredResource,
lifespan: .process,
lifespanId: ProcessIdentifier.current.hex
)
)
} catch {
Embrace.logger.error("Failed to capture resource: \(error.localizedDescription)")
}
_ = addMetadata(
key: key,
value: value.description,
type: .requiredResource,
lifespan: .process,
lifespanId: ProcessIdentifier.current.hex
)
}
}
2 changes: 1 addition & 1 deletion Sources/EmbraceCore/Embrace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ To start the SDK you first need to configure it using an `Embrace.Options` insta
return nil
}

return sessionController.currentSession?.id.toString
return sessionController.currentSession?.idRaw
}

/// Returns the current device identifier.
Expand Down
4 changes: 2 additions & 2 deletions Sources/EmbraceCore/Internal/Embrace+Setup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ extension Embrace {
partitionId: partitionId,
appGroupId: options.appGroupId
) {
let storageOptions = EmbraceStorage.Options(baseUrl: storageUrl, fileName: "db.sqlite")
let storageMechanism: StorageMechanism = .onDisk(name: "EmbraceStorage", baseURL: storageUrl)
let storageOptions = EmbraceStorage.Options(storageMechanism: storageMechanism)
let storage = try EmbraceStorage(options: storageOptions, logger: Embrace.logger)
try storage.performMigration()
return storage
} else {
throw EmbraceSetupError.failedStorageCreation(partitionId: partitionId, appGroupId: options.appGroupId)
Expand Down
Loading

0 comments on commit 0eeb747

Please sign in to comment.