@@ -7,69 +7,69 @@ import Foundation
77import OpenTelemetryApi
88import OpenTelemetrySdk
99public class LogRecordAdapter {
10- public static func toProtoResourceRecordLog( logRecordList: [ ReadableLogRecord ] ) -> [ Opentelemetry_Proto_Logs_V1_ResourceLogs ] {
11- let resourceAndScopeMap = groupByResourceAndScope ( logRecordList: logRecordList)
12- var resourceLogs = [ Opentelemetry_Proto_Logs_V1_ResourceLogs] ( )
13- resourceAndScopeMap. forEach { resMap in
14- var scopeLogs = [ Opentelemetry_Proto_Logs_V1_ScopeLogs] ( )
15- resMap. value. forEach { scopeInfo, logRecords in
16- var protoScopeLogs = Opentelemetry_Proto_Logs_V1_ScopeLogs ( )
17- protoScopeLogs. scope = CommonAdapter . toProtoInstrumentationScope ( instrumentationScopeInfo: scopeInfo)
18- logRecords. forEach { record in
19- protoScopeLogs. logRecords. append ( record)
20- }
21- scopeLogs. append ( protoScopeLogs)
22- }
23- var resourceLog = Opentelemetry_Proto_Logs_V1_ResourceLogs ( )
24- resourceLog. resource = ResourceAdapter . toProtoResource ( resource: resMap. key)
25- resourceLog. scopeLogs. append ( contentsOf: scopeLogs)
26- resourceLogs. append ( resourceLog)
10+ public static func toProtoResourceRecordLog( logRecordList: [ ReadableLogRecord ] ) -> [ Opentelemetry_Proto_Logs_V1_ResourceLogs ] {
11+ let resourceAndScopeMap = groupByResourceAndScope ( logRecordList: logRecordList)
12+ var resourceLogs = [ Opentelemetry_Proto_Logs_V1_ResourceLogs] ( )
13+ resourceAndScopeMap. forEach { resMap in
14+ var scopeLogs = [ Opentelemetry_Proto_Logs_V1_ScopeLogs] ( )
15+ resMap. value. forEach { scopeInfo, logRecords in
16+ var protoScopeLogs = Opentelemetry_Proto_Logs_V1_ScopeLogs ( )
17+ protoScopeLogs. scope = CommonAdapter . toProtoInstrumentationScope ( instrumentationScopeInfo: scopeInfo)
18+ logRecords. forEach { record in
19+ protoScopeLogs. logRecords. append ( record)
2720 }
28- return resourceLogs
21+ scopeLogs. append ( protoScopeLogs)
22+ }
23+ var resourceLog = Opentelemetry_Proto_Logs_V1_ResourceLogs ( )
24+ resourceLog. resource = ResourceAdapter . toProtoResource ( resource: resMap. key)
25+ resourceLog. scopeLogs. append ( contentsOf: scopeLogs)
26+ resourceLogs. append ( resourceLog)
2927 }
30-
31- public static func groupByResourceAndScope( logRecordList: [ ReadableLogRecord ] ) -> [ Resource : [ InstrumentationScopeInfo : [ Opentelemetry_Proto_Logs_V1_LogRecord ] ] ] {
32- var result = [ Resource: [ InstrumentationScopeInfo: [ Opentelemetry_Proto_Logs_V1_LogRecord] ] ] ( )
33- logRecordList. forEach { logRecord in
34- result [ logRecord. resource, default: [ InstrumentationScopeInfo: [ Opentelemetry_Proto_Logs_V1_LogRecord] ] ( ) ] [ logRecord. instrumentationScopeInfo, default: [ Opentelemetry_Proto_Logs_V1_LogRecord] ( ) ] . append ( toProtoLogRecord ( logRecord: logRecord) )
35- }
36- return result
37- }
38-
39- public static func toProtoLogRecord( logRecord: ReadableLogRecord ) -> Opentelemetry_Proto_Logs_V1_LogRecord {
40- var protoLogRecord = Opentelemetry_Proto_Logs_V1_LogRecord ( )
41-
42- if let observedTimestamp = logRecord. observedTimestamp {
43- protoLogRecord. observedTimeUnixNano = observedTimestamp. timeIntervalSince1970. toNanoseconds
44- }
45-
46- protoLogRecord. timeUnixNano = logRecord. timestamp. timeIntervalSince1970. toNanoseconds
47-
48- if let body = logRecord. body, !body. isEmpty {
49- var protoBody = Opentelemetry_Proto_Common_V1_AnyValue ( )
50- protoBody. stringValue = body
51- protoLogRecord. body = protoBody
52- }
53-
54-
55- if let severity = logRecord. severity {
56- protoLogRecord. severityText = severity. description
57- if let protoSeverity = Opentelemetry_Proto_Logs_V1_SeverityNumber ( rawValue: severity. rawValue) {
58- protoLogRecord. severityNumber = protoSeverity
59- }
60- }
61-
62- if let context = logRecord. spanContext {
63- protoLogRecord. spanID = TraceProtoUtils . toProtoSpanId ( spanId: context. spanId)
64- protoLogRecord. traceID = TraceProtoUtils . toProtoTraceId ( traceId: context. traceId)
65- protoLogRecord. flags = UInt32 ( context. traceFlags. byte)
66- }
67-
68- var protoAttributes = [ Opentelemetry_Proto_Common_V1_KeyValue] ( )
69- logRecord. attributes. forEach { key, value in
70- protoAttributes. append ( CommonAdapter . toProtoAttribute ( key: key, attributeValue: value) )
71- }
72- protoLogRecord. attributes = protoAttributes
73- return protoLogRecord
28+ return resourceLogs
29+ }
30+
31+ static func groupByResourceAndScope( logRecordList: [ ReadableLogRecord ] ) -> [ Resource : [ InstrumentationScopeInfo : [ Opentelemetry_Proto_Logs_V1_LogRecord ] ] ] {
32+ var result = [ Resource: [ InstrumentationScopeInfo: [ Opentelemetry_Proto_Logs_V1_LogRecord] ] ] ( )
33+ logRecordList. forEach { logRecord in
34+ result [ logRecord. resource, default: [ InstrumentationScopeInfo: [ Opentelemetry_Proto_Logs_V1_LogRecord] ] ( ) ] [ logRecord. instrumentationScopeInfo, default: [ Opentelemetry_Proto_Logs_V1_LogRecord] ( ) ] . append ( toProtoLogRecord ( logRecord: logRecord) )
35+ }
36+ return result
37+ }
38+
39+ static func toProtoLogRecord( logRecord: ReadableLogRecord ) -> Opentelemetry_Proto_Logs_V1_LogRecord {
40+ var protoLogRecord = Opentelemetry_Proto_Logs_V1_LogRecord ( )
41+
42+ if let observedTimestamp = logRecord. observedTimestamp {
43+ protoLogRecord. observedTimeUnixNano = observedTimestamp. timeIntervalSince1970. toNanoseconds
44+ }
45+
46+ protoLogRecord. timeUnixNano = logRecord. timestamp. timeIntervalSince1970. toNanoseconds
47+
48+ if let body = logRecord. body, !body. isEmpty {
49+ var protoBody = Opentelemetry_Proto_Common_V1_AnyValue ( )
50+ protoBody. stringValue = body
51+ protoLogRecord. body = protoBody
52+ }
53+
54+
55+ if let severity = logRecord. severity {
56+ protoLogRecord. severityText = severity. description
57+ if let protoSeverity = Opentelemetry_Proto_Logs_V1_SeverityNumber ( rawValue: severity. rawValue) {
58+ protoLogRecord. severityNumber = protoSeverity
59+ }
60+ }
61+
62+ if let context = logRecord. spanContext {
63+ protoLogRecord. spanID = TraceProtoUtils . toProtoSpanId ( spanId: context. spanId)
64+ protoLogRecord. traceID = TraceProtoUtils . toProtoTraceId ( traceId: context. traceId)
65+ protoLogRecord. flags = UInt32 ( context. traceFlags. byte)
66+ }
67+
68+ var protoAttributes = [ Opentelemetry_Proto_Common_V1_KeyValue] ( )
69+ logRecord. attributes. forEach { key, value in
70+ protoAttributes. append ( CommonAdapter . toProtoAttribute ( key: key, attributeValue: value) )
7471 }
72+ protoLogRecord. attributes = protoAttributes
73+ return protoLogRecord
74+ }
7575}
0 commit comments