Skip to content

Commit 1535a05

Browse files
authored
Update attribute processor for stable metrics (#524)
Previously AttribtueProcessor is a class and not open. It can't be customized externally. Now it's changed to procotol and its static functions are moved into SimpleAttributeProcessor (now it's public). Also duplicated NoopAttributesProcessor is removed and more tests are added.
1 parent 4d1502b commit 1535a05

File tree

5 files changed

+78
-70
lines changed

5 files changed

+78
-70
lines changed

Sources/OpenTelemetrySdk/Metrics/Stable/NoopAttributesProcessor.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/OpenTelemetrySdk/Metrics/Stable/View/AttributeProcessor.swift

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,80 @@
66
import Foundation
77
import OpenTelemetryApi
88

9-
public protocol AttributeProcessorProtocol {
9+
public protocol AttributeProcessor {
1010
func process(incoming : [String: AttributeValue]) -> [String: AttributeValue]
1111
}
12-
public class AttributeProcessor : AttributeProcessorProtocol {
13-
14-
public func then(other : AttributeProcessor) -> AttributeProcessor {
12+
13+
public extension AttributeProcessor {
14+
func then(other : AttributeProcessor) -> AttributeProcessor {
1515
if type(of: other) == NoopAttributeProcessor.self {
1616
return self
1717
}
1818
if type(of: self) == NoopAttributeProcessor.self {
1919
return other
2020
}
2121

22-
if type(of: other) == JoinedAttributeProcessor.self {
23-
return (other as! JoinedAttributeProcessor).prepend(processor:self)
22+
if let joined = self as? JoinedAttributeProcessor {
23+
return joined.append(processor:other)
2424
}
25+
if let joined = other as? JoinedAttributeProcessor {
26+
return joined.prepend(processor:self)
27+
}
28+
2529
return JoinedAttributeProcessor([self, other])
2630
}
31+
}
32+
33+
public class SimpleAttributeProcessor : AttributeProcessor {
34+
let attributeProcessor : ([String: AttributeValue]) -> [String:AttributeValue]
2735

36+
init(attributeProcessor : @escaping ([String: AttributeValue]) -> [String: AttributeValue]) {
37+
self.attributeProcessor = attributeProcessor
38+
}
2839

2940
public func process(incoming: [String : AttributeValue]) -> [String : AttributeValue] {
30-
return incoming
41+
return attributeProcessor(incoming)
3142
}
3243

33-
public static func filterByKeyName( nameFilter : @escaping (String) -> Bool) -> AttributeProcessor {
44+
static func filterByKeyName( nameFilter : @escaping (String) -> Bool) -> AttributeProcessor {
3445
return SimpleAttributeProcessor { attributes in
3546
attributes.filter { key, value in
3647
nameFilter(key)
3748
}
3849
}
3950
}
4051

41-
public static func append(attributes: [String : AttributeValue]) -> AttributeProcessor {
52+
static func append(attributes: [String : AttributeValue]) -> AttributeProcessor {
4253
SimpleAttributeProcessor { incoming in
4354
incoming.merging(attributes) { a, b in
4455
b
4556
}
4657
}
4758
}
48-
49-
50-
}
51-
52-
internal class SimpleAttributeProcessor : AttributeProcessor {
53-
54-
let attributeProcessor : ([String: AttributeValue]) -> [String:AttributeValue]
55-
56-
init(attributeProcessor : @escaping ([String: AttributeValue]) -> [String: AttributeValue]) {
57-
self.attributeProcessor = attributeProcessor
58-
59-
}
60-
61-
override func process(incoming: [String : OpenTelemetryApi.AttributeValue]) -> [String : OpenTelemetryApi.AttributeValue] {
62-
return attributeProcessor(incoming)
63-
}
64-
65-
6659
}
6760

6861

6962
public class JoinedAttributeProcessor : AttributeProcessor {
7063

71-
override public func process(incoming: [String : OpenTelemetryApi.AttributeValue]) -> [String : OpenTelemetryApi.AttributeValue] {
64+
public func process(incoming: [String : AttributeValue]) -> [String : AttributeValue] {
7265
var result = incoming
7366
for processor in processors {
7467
result = processor.process(incoming: result)
7568
}
7669
return result
7770
}
7871

79-
override public func then(other: AttributeProcessor) -> AttributeProcessor {
72+
public func append(processor: AttributeProcessor) -> JoinedAttributeProcessor {
8073
var newList = processors
81-
if let otherJoined = other as? JoinedAttributeProcessor {
82-
newList.append(contentsOf: otherJoined.processors)
74+
if let joinedProcessor = processor as? JoinedAttributeProcessor {
75+
newList.append(contentsOf: joinedProcessor.processors)
8376
} else {
84-
newList.append(other)
77+
newList.append(processor)
8578
}
8679
return JoinedAttributeProcessor(newList)
8780
}
8881

89-
public func prepend(processor: AttributeProcessor) -> AttributeProcessor {
82+
public func prepend(processor: AttributeProcessor) -> JoinedAttributeProcessor {
9083
var newProcessors = [processor]
9184
newProcessors.append(contentsOf: processors)
9285
return JoinedAttributeProcessor(newProcessors)
@@ -101,8 +94,8 @@ public class JoinedAttributeProcessor : AttributeProcessor {
10194

10295
public class NoopAttributeProcessor : AttributeProcessor {
10396
static let noop = NoopAttributeProcessor()
104-
private override init() {}
105-
override public func process(incoming: [String : AttributeValue]) -> [String : AttributeValue] {
97+
private init() {}
98+
public func process(incoming: [String : AttributeValue]) -> [String : AttributeValue] {
10699
return incoming
107100
}
108101
}

Sources/OpenTelemetrySdk/Metrics/Stable/View/ViewBuilder.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ public class ViewBuilder {
3030
return self
3131
}
3232

33+
public func withAttributeProcessor(processor: AttributeProcessor) -> Self {
34+
self.processor = processor
35+
return self
36+
}
37+
3338
public func addAttributeFilter(keyFilter: @escaping (String) -> Bool) -> Self {
34-
addAttributeProcessor(processor: AttributeProcessor.filterByKeyName(nameFilter: keyFilter))
39+
addAttributeProcessor(processor: SimpleAttributeProcessor.filterByKeyName(nameFilter: keyFilter))
3540
}
3641

3742
public func addAttributeProcessor(processor: AttributeProcessor) -> Self {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import Foundation
7+
8+
import OpenTelemetryApi
9+
@testable import OpenTelemetrySdk
10+
import XCTest
11+
12+
class AttributeProcessorTests : XCTestCase {
13+
func testStaticNoop() {
14+
XCTAssertNotNil(NoopAttributeProcessor.noop)
15+
XCTAssertEqual(NoopAttributeProcessor.noop.process(incoming: ["hello": AttributeValue.string("world")]), ["hello" : AttributeValue.string("world")])
16+
}
17+
18+
func testSimpleProcessor() {
19+
let incoming = ["hello": AttributeValue("world")]
20+
21+
var processor: AttributeProcessor = SimpleAttributeProcessor.append(attributes: ["foo" : .string("bar")])
22+
XCTAssertEqual(processor.process(incoming: incoming), ["hello" : .string("world"), "foo": .string("bar")])
23+
24+
processor = processor.then(other: SimpleAttributeProcessor.filterByKeyName(nameFilter: { $0 == "foo" }))
25+
XCTAssertEqual(processor.process(incoming: incoming), ["foo": .string("bar")])
26+
}
27+
28+
func testJoinedProcessor() {
29+
let incoming = ["hello": AttributeValue("world")]
30+
31+
let processor0 = SimpleAttributeProcessor.append(attributes: ["foo" : .string("bar0")])
32+
let processor1 = SimpleAttributeProcessor.append(attributes: ["foo" : .string("bar1")])
33+
let processor2 = SimpleAttributeProcessor.append(attributes: ["foo" : .string("bar2")])
34+
35+
var processor = JoinedAttributeProcessor([processor0])
36+
XCTAssertEqual(processor.process(incoming: incoming), ["hello" : .string("world"), "foo": .string("bar0")])
37+
38+
processor = processor.prepend(processor: processor1)
39+
XCTAssertEqual(processor.process(incoming: incoming), ["hello" : .string("world"), "foo": .string("bar0")])
40+
41+
processor = processor.append(processor: processor2)
42+
XCTAssertEqual(processor.process(incoming: incoming), ["hello" : .string("world"), "foo": .string("bar2")])
43+
}
44+
}

Tests/OpenTelemetrySdkTests/Metrics/StableMetrics/NoopAttributesProcessorTests.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)