Skip to content

Commit 725e134

Browse files
authored
Added max value length per span attribute (#634)
1 parent 32a0ff3 commit 725e134

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class RecordEventsReadableSpan: ReadableSpan {
4646
public private(set) var totalRecordedLinks: Int
4747
/// Max number of attributes per span.
4848
public private(set) var maxNumberOfAttributes: Int
49+
/// Max value length of attribute per span.
50+
public private(set) var maxValueLengthPerSpanAttribute: Int
4951
/// Max number of attributes per event.
5052
public private(set) var maxNumberOfAttributesPerEvent: Int
5153

@@ -140,6 +142,7 @@ public class RecordEventsReadableSpan: ReadableSpan {
140142
events = ArrayWithCapacity<SpanData.Event>(capacity: spanLimits.eventCountLimit)
141143
maxNumberOfAttributes = spanLimits.attributeCountLimit
142144
maxNumberOfAttributesPerEvent = spanLimits.attributePerEventCountLimit
145+
maxValueLengthPerSpanAttribute = spanLimits.attributeValueLengthLimit
143146
}
144147

145148
/// Creates and starts a span with the given configuration.
@@ -253,7 +256,13 @@ public class RecordEventsReadableSpan: ReadableSpan {
253256
if attributes[key] == nil, totalAttributeCount > maxNumberOfAttributes {
254257
return
255258
}
256-
attributes[key] = value
259+
/// Process only `string` type value
260+
if case .string(let value) = value {
261+
let formattedValue = value.count > maxValueLengthPerSpanAttribute ? String(value.prefix(maxValueLengthPerSpanAttribute)) : value
262+
attributes[key] = AttributeValue(formattedValue)
263+
} else {
264+
attributes[key] = value
265+
}
257266
}
258267
}
259268

Sources/OpenTelemetrySdk/Trace/SpanLimits.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public struct SpanLimits: Equatable {
2121
public private(set) var attributePerEventCountLimit: Int = 128
2222
/// the global default max number of attributes per Link.
2323
public private(set) var attributePerLinkCountLimit: Int = 128
24+
/// the global default attributes value max length
25+
public private(set) var attributeValueLengthLimit: Int = Int.max
26+
2427
/// Returns the defaultSpanLimits.
2528
public init() {}
2629

@@ -30,6 +33,12 @@ public struct SpanLimits: Equatable {
3033
return spanLimits
3134
}
3235

36+
@discardableResult public func settingAttributeValueLengthLimit(_ number: UInt) -> Self {
37+
var spanLimits = self
38+
spanLimits.attributeValueLengthLimit = number > 0 ? Int(number) : 0
39+
return spanLimits
40+
}
41+
3342
@discardableResult public func settingEventCountLimit(_ number: UInt) -> Self {
3443
var spanLimits = self
3544
spanLimits.eventCountLimit = number > 0 ? Int(number) : 0
@@ -56,6 +65,7 @@ public struct SpanLimits: Equatable {
5665

5766
public static func == (lhs: SpanLimits, rhs: SpanLimits) -> Bool {
5867
return lhs.attributeCountLimit == rhs.attributeCountLimit &&
68+
lhs.attributeValueLengthLimit == rhs.attributeValueLengthLimit &&
5969
lhs.eventCountLimit == rhs.eventCountLimit &&
6070
lhs.linkCountLimit == rhs.linkCountLimit &&
6171
lhs.attributePerEventCountLimit == rhs.attributePerEventCountLimit &&

Tests/OpenTelemetrySdkTests/Trace/RecordEventsReadableSpanTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ class RecordEventsReadableSpanTest: XCTestCase {
417417
XCTAssertEqual(spanData.totalAttributeCount, 2 * maxNumberOfAttributes)
418418
}
419419

420+
func testAttributesValueLength() {
421+
let maxValueLength = 8
422+
let spanLimits = SpanLimits().settingAttributeValueLengthLimit(UInt(maxValueLength))
423+
let span = createTestSpan(config: spanLimits)
424+
span.setAttribute(key: "max_value_length", value: .string("this is a big text that is longer than \(maxValueLength) characters"))
425+
span.end()
426+
let spanData = span.toSpanData()
427+
if case .string(let value) = spanData.attributes["max_value_length"] {
428+
XCTAssertEqual(span.maxValueLengthPerSpanAttribute, maxValueLength)
429+
XCTAssertEqual(value, "this is ")
430+
} else {
431+
XCTFail()
432+
}
433+
}
434+
420435
func testDroppingAndAddingAttributes() {
421436
let maxNumberOfAttributes = 8
422437
let spanLimits = SpanLimits().settingAttributeCountLimit(UInt(maxNumberOfAttributes))

0 commit comments

Comments
 (0)