Skip to content

Commit 5ff604e

Browse files
author
Ignacio Bonafonte
committed
Update SpanLimits attribute names and default limits
1 parent 09cb9a5 commit 5ff604e

File tree

7 files changed

+55
-80
lines changed

7 files changed

+55
-80
lines changed

Sources/Exporters/OpenTelemetryProtocol/trace/utils/TraceProtoUtils.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ struct TraceProtoUtils {
3333

3434
static func spanLimitsFromProto(protoTraceConfig: Opentelemetry_Proto_Trace_V1_TraceConfig) -> SpanLimits {
3535
let spanLimits = SpanLimits()
36-
spanLimits.settingMaxNumberOfAttributes(Int(protoTraceConfig.maxNumberOfAttributes))
37-
.settingMaxNumberOfEvents(Int(protoTraceConfig.maxNumberOfTimedEvents))
38-
.settingMaxNumberOfLinks(Int(protoTraceConfig.maxNumberOfLinks))
39-
.settingMaxNumberOfAttributesPerEvent(Int(protoTraceConfig.maxNumberOfAttributesPerTimedEvent))
40-
.settingMaxNumberOfAttributesPerLink(Int(protoTraceConfig.maxNumberOfAttributesPerLink))
36+
spanLimits.settingAttributeCountLimit(UInt(protoTraceConfig.maxNumberOfAttributes))
37+
.settingEventCountLimit(UInt(protoTraceConfig.maxNumberOfTimedEvents))
38+
.settingLinkCountLimit(UInt(protoTraceConfig.maxNumberOfLinks))
39+
.settingAttributePerEventCountLimit(UInt(protoTraceConfig.maxNumberOfAttributesPerTimedEvent))
40+
.settingAttributePerLinkCountLimit(UInt(protoTraceConfig.maxNumberOfAttributesPerLink))
4141
return spanLimits
4242
}
4343

Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ public class RecordEventsReadableSpan: ReadableSpan {
124124
self.resource = resource
125125
self.startTime = startTime ?? clock.now
126126
self.attributes = attributes
127-
events = ArrayWithCapacity<SpanData.Event>(capacity: spanLimits.maxNumberOfEvents)
128-
maxNumberOfAttributes = spanLimits.maxNumberOfAttributes
129-
maxNumberOfAttributesPerEvent = spanLimits.maxNumberOfAttributesPerEvent
127+
events = ArrayWithCapacity<SpanData.Event>(capacity: spanLimits.eventCountLimit)
128+
maxNumberOfAttributes = spanLimits.attributeCountLimit
129+
maxNumberOfAttributesPerEvent = spanLimits.attributePerEventCountLimit
130130
}
131131

132132
/// Creates and starts a span with the given configuration.

Sources/OpenTelemetrySdk/Trace/SpanBuilderSDK.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class SpanBuilderSdk: SpanBuilder {
5252
self.instrumentationLibraryInfo = instrumentationLibraryInfo
5353
self.tracerSharedState = tracerSharedState
5454
self.spanLimits = spanLimits
55-
attributes = AttributesDictionary(capacity: spanLimits.maxNumberOfAttributes)
55+
attributes = AttributesDictionary(capacity: spanLimits.attributeCountLimit)
5656
}
5757

5858
@discardableResult public func setParent(_ parent: Span) -> Self {
@@ -86,7 +86,7 @@ public class SpanBuilderSdk: SpanBuilder {
8686

8787
@discardableResult public func addLink(_ link: SpanData.Link) -> Self {
8888
totalNumberOfLinksAdded += 1
89-
if links.count >= spanLimits.maxNumberOfLinks {
89+
if links.count >= spanLimits.linkCountLimit {
9090
return self
9191
}
9292
links.append(link)

Sources/OpenTelemetrySdk/Trace/SpanLimits.swift

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,78 +22,53 @@ public struct SpanLimits: Equatable {
2222
// TODO: decide which default sampler to use
2323

2424
/// The global default max number of attributes perSpan.
25-
public private(set) var maxNumberOfAttributes: Int = 1000 {
26-
didSet {
27-
maxNumberOfAttributes < 0 ? maxNumberOfAttributes = 0 : ()
28-
}
29-
}
30-
31-
/// the global default max number of Events per Span.
32-
public private(set) var maxNumberOfEvents: Int = 1000 {
33-
didSet {
34-
maxNumberOfEvents < 0 ? maxNumberOfEvents = 0 : ()
35-
}
36-
}
37-
38-
/// the global default max number of Link entries per Span.
39-
public private(set) var maxNumberOfLinks: Int = 1000 {
40-
didSet {
41-
maxNumberOfLinks < 0 ? maxNumberOfLinks = 0 : ()
42-
}
43-
}
44-
45-
/// the global default max number of attributes per Event.
46-
public private(set) var maxNumberOfAttributesPerEvent: Int = 32 {
47-
didSet {
48-
maxNumberOfAttributesPerEvent < 0 ? maxNumberOfAttributesPerEvent = 0 : ()
49-
}
50-
}
51-
25+
public private(set) var attributeCountLimit: Int = 128
26+
/// the global default max number of Events per Span.
27+
public private(set) var eventCountLimit: Int = 128
28+
/// the global default max number of Link entries per Span.
29+
public private(set) var linkCountLimit: Int = 128
30+
/// the global default max number of attributes per Event.
31+
public private(set) var attributePerEventCountLimit: Int = 128
5232
/// the global default max number of attributes per Link.
53-
public private(set) var maxNumberOfAttributesPerLink: Int = 32 {
54-
didSet {
55-
maxNumberOfAttributesPerLink < 0 ? maxNumberOfAttributesPerLink = 0 : ()
56-
}
57-
}
58-
33+
public private(set) var attributePerLinkCountLimit: Int = 128
5934
/// Returns the defaultSpanLimits.
6035
public init() {}
6136

62-
@discardableResult public func settingMaxNumberOfAttributes(_ number: Int) -> Self {
37+
@discardableResult public func settingAttributeCountLimit(_ number: UInt) -> Self {
6338
var spanLimits = self
64-
spanLimits.maxNumberOfAttributes = number
39+
spanLimits.attributeCountLimit = number > 0 ? Int(number) : 0
6540
return spanLimits
6641
}
6742

68-
@discardableResult public func settingMaxNumberOfEvents(_ number: Int) -> Self {
43+
@discardableResult public func settingEventCountLimit(_ number: UInt) -> Self {
6944
var spanLimits = self
70-
spanLimits.maxNumberOfEvents = number
45+
spanLimits.eventCountLimit = number > 0 ? Int(number) : 0
7146
return spanLimits
7247
}
7348

74-
@discardableResult public func settingMaxNumberOfLinks(_ number: Int) -> Self {
49+
@discardableResult public func settingLinkCountLimit(_ number: UInt) -> Self {
7550
var spanLimits = self
76-
spanLimits.maxNumberOfLinks = number
51+
spanLimits.linkCountLimit = number > 0 ? Int(number) : 0
7752
return spanLimits
7853
}
7954

80-
@discardableResult public func settingMaxNumberOfAttributesPerEvent(_ number: Int) -> Self {
55+
@discardableResult public func settingAttributePerEventCountLimit(_ number: UInt) -> Self {
8156
var spanLimits = self
82-
spanLimits.maxNumberOfAttributesPerEvent = number
57+
spanLimits.attributePerEventCountLimit = number > 0 ? Int(number) : 0
8358
return spanLimits
8459
}
8560

86-
@discardableResult public func settingMaxNumberOfAttributesPerLink(_ number: Int) -> Self {
61+
@discardableResult public func settingAttributePerLinkCountLimit(_ number: UInt) -> Self {
8762
var spanLimits = self
88-
spanLimits.maxNumberOfAttributesPerLink = number
63+
spanLimits.attributePerLinkCountLimit = number > 0 ? Int(number) : 0
8964
return spanLimits
9065
}
9166

9267
public static func == (lhs: SpanLimits, rhs: SpanLimits) -> Bool {
93-
return lhs.maxNumberOfAttributes == rhs.maxNumberOfAttributes &&
94-
lhs.maxNumberOfEvents == rhs.maxNumberOfEvents &&
95-
lhs.maxNumberOfLinks == rhs.maxNumberOfLinks &&
96-
lhs.maxNumberOfAttributesPerEvent == rhs.maxNumberOfAttributesPerEvent &&
97-
lhs.maxNumberOfAttributesPerLink == rhs.maxNumberOfAttributesPerLink
68+
return lhs.attributeCountLimit == rhs.attributeCountLimit &&
69+
lhs.eventCountLimit == rhs.eventCountLimit &&
70+
lhs.linkCountLimit == rhs.linkCountLimit &&
71+
lhs.attributePerEventCountLimit == rhs.attributePerEventCountLimit &&
72+
lhs.attributePerLinkCountLimit == rhs.attributePerLinkCountLimit
9873
}
9974
}

Tests/OpenTelemetrySdkTests/Trace/RecordEventsReadableSpanTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class RecordEventsReadableSpanTest: XCTestCase {
248248

249249
func testDroppingAttributes() {
250250
let maxNumberOfAttributes = 8
251-
let spanLimits = SpanLimits().settingMaxNumberOfAttributes(maxNumberOfAttributes)
251+
let spanLimits = SpanLimits().settingAttributeCountLimit(UInt(maxNumberOfAttributes))
252252
let span = createTestSpan(config: spanLimits)
253253
for i in 0 ..< 2 * maxNumberOfAttributes {
254254
span.setAttribute(key: "MyStringAttributeKey\(i)", value: AttributeValue.int(i))
@@ -264,7 +264,7 @@ class RecordEventsReadableSpanTest: XCTestCase {
264264

265265
func testDroppingAndAddingAttributes() {
266266
let maxNumberOfAttributes = 8
267-
let spanLimits = SpanLimits().settingMaxNumberOfAttributes(maxNumberOfAttributes)
267+
let spanLimits = SpanLimits().settingAttributeCountLimit(UInt(maxNumberOfAttributes))
268268
let span = createTestSpan(config: spanLimits)
269269
for i in 0 ..< 2 * maxNumberOfAttributes {
270270
span.setAttribute(key: "MyStringAttributeKey\(i)", value: AttributeValue.int(i))
@@ -278,7 +278,7 @@ class RecordEventsReadableSpanTest: XCTestCase {
278278
}
279279
spanData = span.toSpanData()
280280
XCTAssertEqual(spanData.attributes.count, maxNumberOfAttributes)
281-
// Test that we still have in the attributes map the latest maxNumberOfAttributes / 2 entries.
281+
// Test that we still have in the attributes map the latest attributeCountLimit / 2 entries.
282282
for i in 0 ..< maxNumberOfAttributes / 2 {
283283
let val = i + maxNumberOfAttributes * 3 / 2
284284
let expectedValue = AttributeValue.int(val)
@@ -294,7 +294,7 @@ class RecordEventsReadableSpanTest: XCTestCase {
294294

295295
func testDroppingEvents() {
296296
let maxNumberOfEvents = 8
297-
let spanLimits = SpanLimits().settingMaxNumberOfEvents(maxNumberOfEvents)
297+
let spanLimits = SpanLimits().settingEventCountLimit(UInt(maxNumberOfEvents))
298298
let span = createTestSpan(config: spanLimits)
299299
for _ in 0 ..< 2 * maxNumberOfEvents {
300300
span.addEvent(name: "event2", attributes: [String: AttributeValue]())
@@ -418,7 +418,7 @@ class RecordEventsReadableSpanTest: XCTestCase {
418418
}
419419

420420
private func createTestSpan(kind: SpanKind, config: SpanLimits, parentContext: SpanContext?, attributes: [String: AttributeValue]) -> RecordEventsReadableSpan {
421-
var attributesWithCapacity = AttributesDictionary(capacity: config.maxNumberOfAttributes)
421+
var attributesWithCapacity = AttributesDictionary(capacity: config.attributeCountLimit)
422422
attributesWithCapacity.updateValues(attributes: attributes)
423423

424424
let span = RecordEventsReadableSpan.startSpan(context: spanContext,

Tests/OpenTelemetrySdkTests/Trace/SpanBuilderSdkTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SpanBuilderSdkTest: XCTestCase {
5353

5454
func testTruncateLink() {
5555
let maxNumberOfLinks = 8
56-
let spanLimits = tracerSdkFactory.getActiveSpanLimits().settingMaxNumberOfLinks(maxNumberOfLinks)
56+
let spanLimits = tracerSdkFactory.getActiveSpanLimits().settingLinkCountLimit(UInt(maxNumberOfLinks))
5757
tracerSdkFactory.updateActiveSpanLimits(spanLimits)
5858
// Verify methods do not crash.
5959
let spanBuilder = tracerSdk.spanBuilder(spanName: spanName)
@@ -103,7 +103,7 @@ class SpanBuilderSdkTest: XCTestCase {
103103

104104
func testDroppingAttributes() {
105105
let maxNumberOfAttrs = 8
106-
let spanLimits = tracerSdkFactory.getActiveSpanLimits().settingMaxNumberOfAttributes(maxNumberOfAttrs)
106+
let spanLimits = tracerSdkFactory.getActiveSpanLimits().settingAttributeCountLimit(UInt(maxNumberOfAttrs))
107107
tracerSdkFactory.updateActiveSpanLimits(spanLimits)
108108
let spanBuilder = tracerSdk.spanBuilder(spanName: spanName)
109109
for i in 0 ..< 2 * maxNumberOfAttrs {

Tests/OpenTelemetrySdkTests/Trace/SpanLimitTests.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ import XCTest
1919

2020
class SpanLimitTests: XCTestCase {
2121
func testDefaultSpanLimits() {
22-
XCTAssertEqual(SpanLimits().maxNumberOfAttributes, 1000)
23-
XCTAssertEqual(SpanLimits().maxNumberOfEvents, 1000)
24-
XCTAssertEqual(SpanLimits().maxNumberOfLinks, 1000)
25-
XCTAssertEqual(SpanLimits().maxNumberOfAttributesPerEvent, 32)
26-
XCTAssertEqual(SpanLimits().maxNumberOfAttributesPerLink, 32)
22+
XCTAssertEqual(SpanLimits().attributeCountLimit, 128)
23+
XCTAssertEqual(SpanLimits().eventCountLimit, 128)
24+
XCTAssertEqual(SpanLimits().linkCountLimit, 128)
25+
XCTAssertEqual(SpanLimits().attributePerEventCountLimit, 128)
26+
XCTAssertEqual(SpanLimits().attributePerLinkCountLimit, 128)
2727
}
2828

2929
func testUpdateSpanLimit_All() {
30-
let spanLimits = SpanLimits().settingMaxNumberOfAttributes(8)
31-
.settingMaxNumberOfEvents(10)
32-
.settingMaxNumberOfLinks(11)
33-
.settingMaxNumberOfAttributesPerEvent(1)
34-
.settingMaxNumberOfAttributesPerLink(2)
35-
XCTAssertEqual(spanLimits.maxNumberOfAttributes, 8)
36-
XCTAssertEqual(spanLimits.maxNumberOfEvents, 10)
37-
XCTAssertEqual(spanLimits.maxNumberOfLinks, 11)
38-
XCTAssertEqual(spanLimits.maxNumberOfAttributesPerEvent, 1)
39-
XCTAssertEqual(spanLimits.maxNumberOfAttributesPerLink, 2)
30+
let spanLimits = SpanLimits().settingAttributeCountLimit(8)
31+
.settingEventCountLimit(10)
32+
.settingLinkCountLimit(11)
33+
.settingAttributePerEventCountLimit(1)
34+
.settingAttributePerLinkCountLimit(2)
35+
XCTAssertEqual(spanLimits.attributeCountLimit, 8)
36+
XCTAssertEqual(spanLimits.eventCountLimit, 10)
37+
XCTAssertEqual(spanLimits.linkCountLimit, 11)
38+
XCTAssertEqual(spanLimits.attributePerEventCountLimit, 1)
39+
XCTAssertEqual(spanLimits.attributePerLinkCountLimit, 2)
4040
}
4141
}

0 commit comments

Comments
 (0)