Skip to content

DEMRUM-2436: Add APIs for global attributes #345

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: feature/next-gen
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions Applications/AgentTestApp/AgentTestApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// Navigation Instrumentation
SplunkRum.shared.navigation.preferences.enableAutomatedTracking = true


// API to update Global Attributes
SplunkRum.shared.globalAttributes.setBool(true, for: "isWorkingHard")
SplunkRum.shared.globalAttributes[string: "secret"] = "Red bull"
return true
}


// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class DefaultEventManager: AgentEventManager {
with: configuration.endpoint.sessionReplayEndpoint,
resources: resources,
runtimeAttributes: agent.runtimeAttributes,
globalAttributes: agent.globalAttributes.all,
globalAttributes: { agent.globalAttributes.getAll() },
initialSessionId: agent.currentSession.currentSessionId,
scriptInstanceId: scriptInstanceId,
debugEnabled: configuration.enableDebugLogging
Expand All @@ -114,7 +114,7 @@ class DefaultEventManager: AgentEventManager {
with: traceUrl,
resources: resources,
runtimeAttributes: agent.runtimeAttributes,
globalAttributes: agent.globalAttributes.all,
globalAttributes: { agent.globalAttributes.getAll() },
debugEnabled: configuration.enableDebugLogging,
spanInterceptor: configuration.spanInterceptor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class SplunkRum: ObservableObject {

lazy var runtimeAttributes: AgentRuntimeAttributes = DefaultRuntimeAttributes(for: self)

lazy var globalAttributes = agentConfiguration.globalAttributes

let logProcessor: LogProcessor
let logger: LogAgent
Expand Down Expand Up @@ -93,6 +92,9 @@ public class SplunkRum: ObservableObject {
/// An object that holds current manages associated session.
public private(set) lazy var session = Session(for: self)

/// An object that contains global attributes added to all signals
public private(set) lazy var globalAttributes: MutableAttributes = agentConfiguration.globalAttributes

/// An object reflects the current state and setting used for the recording.
public private(set) lazy var state = RuntimeState(for: self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@ public class OTLPLogToSpanEventProcessor: LogEventProcessor {

// Initialize LogRecordProcessor
let simpleLogRecordProcessor = SimpleLogRecordProcessor(
logRecordExporter: logToSpanExporter
logRecordExporter: debugEnabled
? SplunkStdoutLogExporter(with: logToSpanExporter)
: logToSpanExporter
)

var processors: [LogRecordProcessor] = [simpleLogRecordProcessor]

// Initialize optional stdout exporter
if debugEnabled {
let stdoutExporter = SplunkStdoutLogExporter()
let stdoutSpanProcessor = SimpleLogRecordProcessor(logRecordExporter: stdoutExporter)

processors.append(stdoutSpanProcessor)
}

// Initialize logger provider
let loggerProviderBuilder = LoggerProviderBuilder()
.with(processors: processors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class OTLPSessionReplayEventProcessor: LogEventProcessor {
with sessionReplayEndpoint: URL?,
resources: AgentResources,
runtimeAttributes: RuntimeAttributes,
globalAttributes: [String: Any],
globalAttributes: @escaping () -> [String: AttributeValue],
initialSessionId: String,
scriptInstanceId: String,
debugEnabled: Bool
Expand All @@ -72,7 +72,10 @@ public class OTLPSessionReplayEventProcessor: LogEventProcessor {
)

// Initialize attribute checker proxy exporter
let attributeCheckerExporter = AttributeCheckerLogExporter(proxy: backgroundLogExporter)
let attributeCheckerExporter = AttributeCheckerLogExporter(
proxy: debugEnabled
? SplunkStdoutLogExporter(with: backgroundLogExporter)
: backgroundLogExporter)

// Initialize LogRecordProcessor
let simpleLogRecordProcessor = SimpleLogRecordProcessor(
Expand Down Expand Up @@ -112,14 +115,6 @@ public class OTLPSessionReplayEventProcessor: LogEventProcessor {

var processors: [LogRecordProcessor] = [globalAttributesLogRecordProcessor]

// Initialize optional stdout exporter
if debugEnabled {
let stdoutExporter = SplunkStdoutLogExporter()
let stdoutSpanProcessor = SimpleLogRecordProcessor(logRecordExporter: stdoutExporter)

processors.append(stdoutSpanProcessor)
}

// Initialize logger provider
let loggerProviderBuilder = LoggerProviderBuilder()
.with(processors: processors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import SplunkCommon

public class OTLPGlobalAttributesLogRecordProcessor: LogRecordProcessor {
private let proxy: LogRecordProcessor
private let globalAttributes: [String: Any]
private let globalAttributes: () -> [String: AttributeValue]

public init(proxy: LogRecordProcessor, with globalAttributes: [String: Any]) {
public init(proxy: LogRecordProcessor, with globalAttributes: @escaping () -> [String: AttributeValue]) {
self.proxy = proxy
self.globalAttributes = globalAttributes
}
Expand All @@ -33,30 +33,13 @@ public class OTLPGlobalAttributesLogRecordProcessor: LogRecordProcessor {

public func onEmit(logRecord: ReadableLogRecord) {

// Convert global attributes to AttributeValue dictionary
var convertedAttributes: [String: AttributeValue] = [:]
for (key, value) in globalAttributes {
if let arrayValue = value as? [Any] {
var attributeValues: [AttributeValue] = []
var updatedAttributes = logRecord.attributes

for element in arrayValue {
if let attributeValue = AttributeValue(element) {
attributeValues.append(attributeValue)
}
}

convertedAttributes[key] = .array(AttributeArray(values: attributeValues))

} else {
if let attributeValue = AttributeValue(value) {
convertedAttributes[key] = attributeValue
}
}
// Add global attributes into the log record's attributes
for (key, value) in globalAttributes() {
updatedAttributes[key] = value
}

var mergedAttributes = convertedAttributes
mergedAttributes.merge(logRecord.attributes) { _, new in new }

// Create a new log record with the merged attributes
let updatedLogRecord = ReadableLogRecord(
resource: logRecord.resource,
Expand All @@ -66,7 +49,7 @@ public class OTLPGlobalAttributesLogRecordProcessor: LogRecordProcessor {
spanContext: logRecord.spanContext,
severity: logRecord.severity,
body: logRecord.body,
attributes: mergedAttributes
attributes: updatedAttributes
)

// Pass the updated log record to the proxy processor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import OpenTelemetryApi
import OpenTelemetrySdk

public class OTLPGlobalAttributesSpanProcessor: SpanProcessor {
private let globalAttributes: [String: Any]
private let globalAttributes: () -> [String: AttributeValue]

public init(with globalAttributes: [String: Any]) {
public init(with globalAttributes: @escaping () -> [String: AttributeValue]) {
self.globalAttributes = globalAttributes
}

Expand All @@ -32,24 +32,9 @@ public class OTLPGlobalAttributesSpanProcessor: SpanProcessor {
public var isEndRequired: Bool { false }

public func onStart(parentContext: SpanContext?, span: ReadableSpan) {
// Add global attributes to the span when it's created
for (key, value) in globalAttributes {
if let arrayValue = value as? [Any] {
var attributeValues: [AttributeValue] = []

for element in arrayValue {
if let attributeValue = AttributeValue(element) {
attributeValues.append(attributeValue)
}
}

span.setAttribute(key: key, value: .array(AttributeArray(values: attributeValues)))

} else {
if let attributeValue = AttributeValue(value) {
span.setAttribute(key: key, value: attributeValue)
}
}
// Add global attributes to the span attributes when it's created
for (key, value) in globalAttributes() {
span.setAttribute(key: key, value: value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SplunkStdoutLogExporter: LogRecordExporter {

// MARK: - Private

private let proxyExporter: LogRecordExporter

// Internal Logger
private let logger = DefaultLogAgent(poolName: PackageIdentifier.instance(), category: "OpenTelemetry")

Expand All @@ -43,7 +45,9 @@ class SplunkStdoutLogExporter: LogRecordExporter {
return dateFormat
}()

init() {}
init(with proxy: LogRecordExporter) {
proxyExporter = proxy
}

func export(logRecords: [OpenTelemetrySdk.ReadableLogRecord], explicitTimeout: TimeInterval?) -> OpenTelemetrySdk.ExportResult {
for logRecord in logRecords {
Expand Down Expand Up @@ -81,12 +85,14 @@ class SplunkStdoutLogExporter: LogRecordExporter {
}
}

return .success
return proxyExporter.export(logRecords: logRecords, explicitTimeout: explicitTimeout)
}

func forceFlush(explicitTimeout: TimeInterval?) -> OpenTelemetrySdk.ExportResult {
return .success
return proxyExporter.forceFlush(explicitTimeout: explicitTimeout)
}

func shutdown(explicitTimeout: TimeInterval?) {}
func shutdown(explicitTimeout: TimeInterval?) {
return proxyExporter.shutdown(explicitTimeout: explicitTimeout)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class OTLPTraceProcessor: TraceProcessor {
with tracesEndpoint: URL,
resources: AgentResources,
runtimeAttributes: RuntimeAttributes,
globalAttributes: [String: Any],
globalAttributes: @escaping () -> [String: AttributeValue],
debugEnabled: Bool,
spanInterceptor: SplunkSpanInterceptor?
) {
Expand Down