-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes logic in EmbraceStorage+Options such that limit is applied to S…
…panType (#232) Was originally SpanType per Trace, which will realistically never hit Also introduces a default max limit if none is configured in the EmbraceStorage.Options Adds tests to cover limit pruning logic
- Loading branch information
Showing
4 changed files
with
160 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
Tests/EmbraceStorageTests/Records/SpanRecord/EmbraceStorage+SpanTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// | ||
// Copyright © 2023 Embrace Mobile, Inc. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import EmbraceStorage | ||
import OpenTelemetryApi | ||
|
||
final class EmbraceStorage_SpanTests: XCTestCase { | ||
|
||
var storage: EmbraceStorage! | ||
|
||
override func setUpWithError() throws { | ||
storage = try EmbraceStorage.createInMemoryDb() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
try storage.teardown() | ||
storage = nil | ||
} | ||
|
||
func test_upsertSpan_appliesConfiguredLimitForType() throws { | ||
storage.options.spanLimits[.performance] = 3 | ||
|
||
for i in 0..<3 { | ||
// given inserted record | ||
let span = SpanRecord( | ||
id: SpanId.random().hexString, | ||
name: "example \(i)", | ||
traceId: TraceId.random().hexString, | ||
type: .performance, | ||
data: Data(), | ||
startTime: Date() | ||
) | ||
|
||
try storage.upsertSpan(span) | ||
} | ||
|
||
try storage.upsertSpan( | ||
SpanRecord( | ||
id: SpanId.random().hexString, | ||
name: "newest", | ||
traceId: TraceId.random().hexString, | ||
type: .performance, | ||
data: Data(), | ||
startTime: Date() | ||
) | ||
) | ||
|
||
let allRecords = try storage.dbQueue.read { db in | ||
try SpanRecord.order(SpanRecord.Schema.startTime).fetchAll(db) | ||
} | ||
XCTAssertEqual(allRecords.count, 3) | ||
XCTAssertEqual(allRecords.map(\.name), ["example 1", "example 2", "newest"]) | ||
} | ||
|
||
func test_upsertSpan_limitIsUniqueToSpecificType() throws { | ||
storage.options.spanLimits[.performance] = 3 | ||
storage.options.spanLimits[.networkHTTP] = 1 | ||
|
||
// insert 3 .performance spans | ||
for i in 0..<3 { | ||
let span = SpanRecord( | ||
id: SpanId.random().hexString, | ||
name: "performance \(i)", | ||
traceId: TraceId.random().hexString, | ||
type: .performance, | ||
data: Data(), | ||
startTime: Date() | ||
) | ||
|
||
try storage.upsertSpan(span) | ||
} | ||
|
||
// insert 3 .networkHTTP spans | ||
for i in 0..<3 { | ||
let span = SpanRecord( | ||
id: SpanId.random().hexString, | ||
name: "network \(i)", | ||
traceId: TraceId.random().hexString, | ||
type: .networkHTTP, | ||
data: Data(), | ||
startTime: Date() | ||
) | ||
|
||
try storage.upsertSpan(span) | ||
} | ||
|
||
let allRecords = try storage.dbQueue.read { db in | ||
try SpanRecord.order(SpanRecord.Schema.startTime).fetchAll(db) | ||
} | ||
XCTAssertEqual(allRecords.count, 4) | ||
XCTAssertEqual( | ||
allRecords.map(\.name), | ||
[ | ||
"performance 0", | ||
"performance 1", | ||
"performance 2", | ||
"network 2" | ||
] | ||
) | ||
} | ||
|
||
func test_upsertSpan_appliesDefaultLimit() throws { | ||
for i in 0..<(EmbraceStorage.defaultSpanLimitByType + 10) { | ||
// given inserted record | ||
let span = SpanRecord( | ||
id: SpanId.random().hexString, | ||
name: "example \(i)", | ||
traceId: TraceId.random().hexString, | ||
type: .performance, | ||
data: Data(), | ||
startTime: Date() | ||
) | ||
|
||
try storage.upsertSpan(span) | ||
} | ||
|
||
try storage.upsertSpan( | ||
SpanRecord( | ||
id: SpanId.random().hexString, | ||
name: "newest", | ||
traceId: TraceId.random().hexString, | ||
type: .performance, | ||
data: Data(), | ||
startTime: Date() | ||
) | ||
) | ||
|
||
let allRecords = try storage.dbQueue.read { db in | ||
try SpanRecord.order(SpanRecord.Schema.startTime).fetchAll(db) | ||
} | ||
XCTAssertEqual(allRecords.count, EmbraceStorage.defaultSpanLimitByType) // 1500 is default limit | ||
} | ||
|
||
} |