Skip to content

Commit 0b1a4dd

Browse files
authored
updated protobufs to v0.20.0 (#433)
1 parent ea34649 commit 0b1a4dd

File tree

15 files changed

+717
-1123
lines changed

15 files changed

+717
-1123
lines changed

CONTRIBUTING.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,60 @@ merge. You'll probably get some feedback from these fine folks which helps to
114114
make the project that much better. Respond to the feedback and work with your
115115
reviewer(s) to resolve any issues.
116116

117+
### Generating OTLP protobuf files
118+
Occasionally, the opentelemetry protocol's protobuf definitions are updated and need to be regenerated for the OTLP exporters. This is documentation on how to accomplish that for this project. Other projects can regenerate their otlp protobuf files using the [Open Telemetry build tools][build-tools].
119+
120+
#### Requirements
121+
- [protoc]
122+
- [grpc-swift]
123+
- [opentelemetry-proto]
124+
125+
##### Install protoc
126+
```asciidoc
127+
$ brew install protobuf
128+
$ protoc --version # Ensure compiler version is 3+
129+
```
130+
##### Installing grpc-swift
131+
```
132+
brew install swift-protobuf grpc-swift
133+
```
134+
135+
##### Generating otlp protobuf files
136+
137+
Clone [opentelemetry-proto]
138+
139+
From within opentelemetry-proto:
140+
141+
```shell
142+
# collect the proto definitions:
143+
PROTO_FILES=($(ls opentelemetry/proto/*/*/*/*.proto opentelemetry/proto/*/*/*.proto))
144+
# generate swift proto files
145+
for file in "${PROTO_FILES[@]}"
146+
do
147+
protoc --swift_opt=Visibility=Public --swift_out=./out ${file}
148+
done
149+
150+
# genearate GRPC swift proto files
151+
protoc --swift_opt=Visibility=Public --grpc-swift_opt=Visibility=Public --swift_out=./out --grpc-swift_out=./out opentelemetry/proto/collector/trace/v1/trace_service.proto
152+
protoc --swift_opt=Visibility=Public --grpc-swift_opt=Visibility=Public --swift_out=./out --grpc-swift_out=./out opentelemetry/proto/collector/metrics/v1/metrics_service.proto
153+
protoc --swift_opt=Visibility=Public --grpc-swift_opt=Visibility=Public --swift_out=./out --grpc-swift_out=./out opentelemetry/proto/collector/logs/v1/logs_service.proto
154+
```
155+
156+
Replace the generated files in `Sources/Exporters/OpenTelemetryProtocolCommon/proto` & `Sources/Exporters/OpenTelemetryGrpc/proto`:
157+
###### `OpenTelemetryProtocolGrpc/proto` file list
158+
`logs_service.grpc.swift`
159+
`metrics_serivce.grpc.swift`
160+
`trace_service.grpc.swift`
161+
162+
###### `OpenTelemetryProtocolCommon/proto`
163+
`common.pb.swift`
164+
`logs.pb.swift`
165+
`logs_service.pb.swift`
166+
`metrics.pb.swift`
167+
`metrics_services.pb.swift`
168+
`resource.pb.swift`
169+
`trace.pb.swift`
170+
`trace_service.pb.swift`
117171
118172
[cncf-cla]: https://identity.linuxfoundation.org/projects/cncf
119173
[github-draft]: https://github.blog/2019-02-14-introducing-draft-pull-requests/
@@ -122,3 +176,7 @@ reviewer(s) to resolve any issues.
122176
[otel-github-workflow]: https://github.com/open-telemetry/community/blob/master/CONTRIBUTING.md#github-workflow
123177
[otel-lib-guidelines]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/library-guidelines.md
124178
[otel-specification]: https://github.com/open-telemetry/opentelemetry-specification
179+
[grpc-swift]: https://github.com/grpc/grpc-swift
180+
[opentelemetry-proto]: https://github.com/open-telemetry/opentelemetry-proto
181+
[protoc]: https://grpc.io/docs/protoc-installation/
182+
[build-tools]: https://github.com/open-telemetry/build-tools

Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import OpenTelemetrySdk
99

1010
public struct CommonAdapter {
1111
public static func toProtoAttribute(key: String, attributeValue: AttributeValue)
12-
-> Opentelemetry_Proto_Common_V1_KeyValue
12+
-> Opentelemetry_Proto_Common_V1_KeyValue
1313
{
1414
var keyValue = Opentelemetry_Proto_Common_V1_KeyValue()
1515
keyValue.key = key
@@ -53,17 +53,17 @@ public struct CommonAdapter {
5353
}
5454
return keyValue
5555
}
56-
56+
5757
public static func toProtoInstrumentationScope(instrumentationScopeInfo: InstrumentationScopeInfo)
58-
-> Opentelemetry_Proto_Common_V1_InstrumentationScope
58+
-> Opentelemetry_Proto_Common_V1_InstrumentationScope
5959
{
60-
60+
6161
var instrumentationScope = Opentelemetry_Proto_Common_V1_InstrumentationScope()
6262
instrumentationScope.name = instrumentationScopeInfo.name
6363
if let version = instrumentationScopeInfo.version {
6464
instrumentationScope.version = version
6565
}
6666
return instrumentationScope
6767
}
68-
68+
6969
}

Sources/Exporters/OpenTelemetryProtocolCommon/common/ResourceAdapter.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import Foundation
77
import OpenTelemetrySdk
88

99
public struct ResourceAdapter {
10-
public static func toProtoResource(resource: Resource) -> Opentelemetry_Proto_Resource_V1_Resource {
11-
var outputResource = Opentelemetry_Proto_Resource_V1_Resource()
12-
resource.attributes.forEach {
13-
let protoAttribute = CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value)
14-
outputResource.attributes.append(protoAttribute)
15-
}
16-
return outputResource
10+
public static func toProtoResource(resource: Resource) -> Opentelemetry_Proto_Resource_V1_Resource {
11+
var outputResource = Opentelemetry_Proto_Resource_V1_Resource()
12+
resource.attributes.forEach {
13+
let protoAttribute = CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value)
14+
outputResource.attributes.append(protoAttribute)
1715
}
16+
return outputResource
17+
}
1818
}

Sources/Exporters/OpenTelemetryProtocolCommon/logs/LogRecordAdapter.swift

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,69 @@ import Foundation
77
import OpenTelemetryApi
88
import OpenTelemetrySdk
99
public 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

Comments
 (0)