Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: update opentelemetry swift #136

Merged
merged 8 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Print Swift version
run: swift --version
- name: Build xcframework
working-directory: ./SplunkRumWorkspace/SplunkRum
run: ./build_binaries.sh
- name: Upload xcframework
uses: actions/upload-artifact@v3
with:
name: SplunkOtel.xcframework
path: ./SplunkRumWorkspace/SplunkRum/xcframeworks/SplunkOtel.xcframework
path: ./SplunkRumWorkspace/SplunkRum/xcframeworks/SplunkOtel.xcframework
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ disabled_rules:
- force_cast
- private_over_fileprivate
- cyclomatic_complexity
- missing_docs
# Not really sure about a bunch of these, just turning most stuff in the 'performance' and 'lint' categories
opt_in_rules:
- missing_docs
- unowned_variable_capture
- array_init
- identical_operands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SmokeTestUITests: XCTestCase {

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
// swiftlint:enable overridden_super_call

let SLEEP_TIME: UInt32 = 30 // batch is currently every 5 so this should be plenty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func startHttpSpan(request: URLRequest?) -> Span? {

fileprivate var ASSOC_KEY_SPAN: UInt8 = 0

// swiftlint:disable missing_docs
extension URLSessionTask {
@objc open func splunk_swizzled_setState(state: URLSessionTask.State) {
defer {
Expand Down
1 change: 1 addition & 0 deletions SplunkRumWorkspace/SplunkRum/SplunkRum/NetworkType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func getNetworkInfo() -> NetworkInfo {
info.hostConnectionSubType = shortTech(technology)
}
}
// swiftlint:enable unavailable_condition

return info
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import Foundation
/// and BaggageManager.
/// The telemetry objects are lazy-loaded singletons resolved via ServiceLoader mechanism.
public struct OpenTelemetry {

public static var version = "v1.7.0"

public static var instance = OpenTelemetry()

/// Registered tracerProvider or default via DefaultTracerProvider.instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct AttributesDictionary {
}
set {
if newValue == nil {
removeValueForKey(key: key)
_ = removeValueForKey(key: key)
} else {
_ = updateValue(value: newValue!, forKey: key)
}
Expand Down Expand Up @@ -80,11 +80,11 @@ public struct AttributesDictionary {
}
}

public mutating func removeValueForKey(key: String) {
public mutating func removeValueForKey(key: String) -> AttributeValue? {
keys = keys.filter {
$0 != key
}
attributes.removeValue(forKey: key)
return attributes.removeValue(forKey: key)
}

public mutating func removeAll(keepCapacity: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ import Foundation
/// Implementation for the Span class that records trace events.
public class RecordEventsReadableSpan: ReadableSpan {
public var isRecording = true

fileprivate let internalStatusQueue = DispatchQueue(label: "org.opentelemetry.RecordEventsReadableSpan.internalStatusQueue", attributes: .concurrent)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the duplicate spans be coming from otel creating spans from different threads? I am wondering if a serial queue would fix the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// The displayed name of the span.
fileprivate var internalName: String
public var name: String {
didSet {
if hasEnded {
name = oldValue
get {
var value: String = ""
internalStatusQueue.sync {
value = internalName
}
return value
}
set {
internalStatusQueue.sync(flags: .barrier) {
if !internalEnd {
internalName = newValue
}
}
}
}
Expand Down Expand Up @@ -54,11 +66,22 @@ public class RecordEventsReadableSpan: ReadableSpan {
public private(set) var totalAttributeCount: Int = 0
/// Number of events recorded.
public private(set) var totalRecordedEvents = 0

/// The status of the span.
public var status = Status.unset {
didSet {
if hasEnded {
status = oldValue
fileprivate var internalStatus: Status = .unset
public var status: Status {
get {
var value: Status = .unset
internalStatusQueue.sync {
value = internalStatus
}
return value
}
set {
internalStatusQueue.sync(flags: .barrier) {
if !internalEnd {
internalStatus = newValue
}
}
}
}
Expand All @@ -71,12 +94,13 @@ public class RecordEventsReadableSpan: ReadableSpan {
/// The end time of the span.
public private(set) var endTime: Date?
/// True if the span is ended.
fileprivate var endAtomic = false
private let endSyncLock = Lock()
fileprivate var internalEnd = false
public var hasEnded: Bool {
endSyncLock.withLock {
return endAtomic
var value = false
internalStatusQueue.sync {
value = internalEnd
}
return value
}

private let eventsSyncLock = Lock()
Expand All @@ -98,7 +122,7 @@ public class RecordEventsReadableSpan: ReadableSpan {
startTime: Date?)
{
self.context = context
self.name = name
self.internalName = name
self.instrumentationScopeInfo = instrumentationScopeInfo
self.parentContext = parentContext
self.hasRemoteParent = hasRemoteParent
Expand Down Expand Up @@ -215,8 +239,12 @@ public class RecordEventsReadableSpan: ReadableSpan {
if !isRecording {
return
}

if value == nil {
attributes.removeValueForKey(key: key)
if attributes.removeValueForKey(key: key) != nil {
totalAttributeCount -= 1
}
return
}
totalAttributeCount += 1
if attributes[key] == nil, totalAttributeCount > maxNumberOfAttributes {
Expand Down Expand Up @@ -261,12 +289,16 @@ public class RecordEventsReadableSpan: ReadableSpan {
}

public func end(time: Date) {
var wasAlreadyEnded = false
endSyncLock.withLockVoid {
wasAlreadyEnded = endAtomic
endAtomic = true
let alreadyEnded = internalStatusQueue.sync(flags: .barrier) { () -> Bool in
if internalEnd {
return true
}

internalEnd = true
return false
}
if wasAlreadyEnded {

if alreadyEnded {
return
}

Expand All @@ -276,8 +308,8 @@ public class RecordEventsReadableSpan: ReadableSpan {
}
}
endTime = time
spanProcessor.onEnd(span: self)
OpenTelemetry.instance.contextProvider.removeContextForSpan(self)
spanProcessor.onEnd(span: self)
}

public var description: String {
Expand All @@ -286,13 +318,12 @@ public class RecordEventsReadableSpan: ReadableSpan {

internal func getTotalRecordedEvents() -> Int {
eventsSyncLock.withLock {
return totalRecordedEvents
totalRecordedEvents
}
}

/// For testing purposes
internal func getDroppedLinksCount() -> Int {
return totalRecordedLinks - links.count
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
import Foundation
import UIKit

// swiftlint:disable missing_docs
extension UIApplication {
// FIXME really only a reasonable solution for storyboard apps/components and not swiftui ones
@objc open func splunk_swizzled_sendAction(_ action: Selector,
Expand Down
2 changes: 1 addition & 1 deletion dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
https://github.com/devicekit/DeviceKit/commit/1291f3789e1a9d8142b949940d4baf0da611cb54
https://github.com/open-telemetry/opentelemetry-swift/commit/fd99c038c954b6858e11a29a442db993d0f0247c
https://github.com/open-telemetry/opentelemetry-swift/commit/fd2171eebddcea03dea61fb217256f1dead2ea7f