Skip to content

Commit

Permalink
updated protobufs to v0.20.0 (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-b authored Jul 5, 2023
1 parent ea34649 commit 0b1a4dd
Show file tree
Hide file tree
Showing 15 changed files with 717 additions and 1,123 deletions.
58 changes: 58 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,60 @@ merge. You'll probably get some feedback from these fine folks which helps to
make the project that much better. Respond to the feedback and work with your
reviewer(s) to resolve any issues.

### Generating OTLP protobuf files
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].

#### Requirements
- [protoc]
- [grpc-swift]
- [opentelemetry-proto]

##### Install protoc
```asciidoc
$ brew install protobuf
$ protoc --version # Ensure compiler version is 3+
```
##### Installing grpc-swift
```
brew install swift-protobuf grpc-swift
```

##### Generating otlp protobuf files

Clone [opentelemetry-proto]

From within opentelemetry-proto:

```shell
# collect the proto definitions:
PROTO_FILES=($(ls opentelemetry/proto/*/*/*/*.proto opentelemetry/proto/*/*/*.proto))
# generate swift proto files
for file in "${PROTO_FILES[@]}"
do
protoc --swift_opt=Visibility=Public --swift_out=./out ${file}
done
# genearate GRPC swift proto files
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
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
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
```
Replace the generated files in `Sources/Exporters/OpenTelemetryProtocolCommon/proto` & `Sources/Exporters/OpenTelemetryGrpc/proto`:
###### `OpenTelemetryProtocolGrpc/proto` file list
`logs_service.grpc.swift`
`metrics_serivce.grpc.swift`
`trace_service.grpc.swift`
###### `OpenTelemetryProtocolCommon/proto`
`common.pb.swift`
`logs.pb.swift`
`logs_service.pb.swift`
`metrics.pb.swift`
`metrics_services.pb.swift`
`resource.pb.swift`
`trace.pb.swift`
`trace_service.pb.swift`
[cncf-cla]: https://identity.linuxfoundation.org/projects/cncf
[github-draft]: https://github.blog/2019-02-14-introducing-draft-pull-requests/
Expand All @@ -122,3 +176,7 @@ reviewer(s) to resolve any issues.
[otel-github-workflow]: https://github.com/open-telemetry/community/blob/master/CONTRIBUTING.md#github-workflow
[otel-lib-guidelines]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/library-guidelines.md
[otel-specification]: https://github.com/open-telemetry/opentelemetry-specification
[grpc-swift]: https://github.com/grpc/grpc-swift
[opentelemetry-proto]: https://github.com/open-telemetry/opentelemetry-proto
[protoc]: https://grpc.io/docs/protoc-installation/
[build-tools]: https://github.com/open-telemetry/build-tools
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import OpenTelemetrySdk

public struct CommonAdapter {
public static func toProtoAttribute(key: String, attributeValue: AttributeValue)
-> Opentelemetry_Proto_Common_V1_KeyValue
-> Opentelemetry_Proto_Common_V1_KeyValue
{
var keyValue = Opentelemetry_Proto_Common_V1_KeyValue()
keyValue.key = key
Expand Down Expand Up @@ -53,17 +53,17 @@ public struct CommonAdapter {
}
return keyValue
}

public static func toProtoInstrumentationScope(instrumentationScopeInfo: InstrumentationScopeInfo)
-> Opentelemetry_Proto_Common_V1_InstrumentationScope
-> Opentelemetry_Proto_Common_V1_InstrumentationScope
{

var instrumentationScope = Opentelemetry_Proto_Common_V1_InstrumentationScope()
instrumentationScope.name = instrumentationScopeInfo.name
if let version = instrumentationScopeInfo.version {
instrumentationScope.version = version
}
return instrumentationScope
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import Foundation
import OpenTelemetrySdk

public struct ResourceAdapter {
public static func toProtoResource(resource: Resource) -> Opentelemetry_Proto_Resource_V1_Resource {
var outputResource = Opentelemetry_Proto_Resource_V1_Resource()
resource.attributes.forEach {
let protoAttribute = CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value)
outputResource.attributes.append(protoAttribute)
}
return outputResource
public static func toProtoResource(resource: Resource) -> Opentelemetry_Proto_Resource_V1_Resource {
var outputResource = Opentelemetry_Proto_Resource_V1_Resource()
resource.attributes.forEach {
let protoAttribute = CommonAdapter.toProtoAttribute(key: $0.key, attributeValue: $0.value)
outputResource.attributes.append(protoAttribute)
}
return outputResource
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,69 @@ import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk
public class LogRecordAdapter {
public static func toProtoResourceRecordLog(logRecordList: [ReadableLogRecord]) -> [Opentelemetry_Proto_Logs_V1_ResourceLogs] {
let resourceAndScopeMap = groupByResourceAndScope(logRecordList: logRecordList)
var resourceLogs = [Opentelemetry_Proto_Logs_V1_ResourceLogs]()
resourceAndScopeMap.forEach { resMap in
var scopeLogs = [Opentelemetry_Proto_Logs_V1_ScopeLogs]()
resMap.value.forEach { scopeInfo, logRecords in
var protoScopeLogs = Opentelemetry_Proto_Logs_V1_ScopeLogs()
protoScopeLogs.scope = CommonAdapter.toProtoInstrumentationScope(instrumentationScopeInfo: scopeInfo)
logRecords.forEach { record in
protoScopeLogs.logRecords.append(record)
}
scopeLogs.append(protoScopeLogs)
}
var resourceLog = Opentelemetry_Proto_Logs_V1_ResourceLogs()
resourceLog.resource = ResourceAdapter.toProtoResource(resource: resMap.key)
resourceLog.scopeLogs.append(contentsOf: scopeLogs)
resourceLogs.append(resourceLog)
public static func toProtoResourceRecordLog(logRecordList: [ReadableLogRecord]) -> [Opentelemetry_Proto_Logs_V1_ResourceLogs] {
let resourceAndScopeMap = groupByResourceAndScope(logRecordList: logRecordList)
var resourceLogs = [Opentelemetry_Proto_Logs_V1_ResourceLogs]()
resourceAndScopeMap.forEach { resMap in
var scopeLogs = [Opentelemetry_Proto_Logs_V1_ScopeLogs]()
resMap.value.forEach { scopeInfo, logRecords in
var protoScopeLogs = Opentelemetry_Proto_Logs_V1_ScopeLogs()
protoScopeLogs.scope = CommonAdapter.toProtoInstrumentationScope(instrumentationScopeInfo: scopeInfo)
logRecords.forEach { record in
protoScopeLogs.logRecords.append(record)
}
return resourceLogs
scopeLogs.append(protoScopeLogs)
}
var resourceLog = Opentelemetry_Proto_Logs_V1_ResourceLogs()
resourceLog.resource = ResourceAdapter.toProtoResource(resource: resMap.key)
resourceLog.scopeLogs.append(contentsOf: scopeLogs)
resourceLogs.append(resourceLog)
}

public static func groupByResourceAndScope(logRecordList: [ReadableLogRecord]) -> [Resource:[InstrumentationScopeInfo:[Opentelemetry_Proto_Logs_V1_LogRecord]]] {
var result = [Resource:[InstrumentationScopeInfo: [Opentelemetry_Proto_Logs_V1_LogRecord]]]()
logRecordList.forEach { logRecord in
result[logRecord.resource, default:[InstrumentationScopeInfo: [Opentelemetry_Proto_Logs_V1_LogRecord]]()][logRecord.instrumentationScopeInfo,default:[Opentelemetry_Proto_Logs_V1_LogRecord]()].append(toProtoLogRecord(logRecord: logRecord))
}
return result
}

public static func toProtoLogRecord(logRecord: ReadableLogRecord) -> Opentelemetry_Proto_Logs_V1_LogRecord {
var protoLogRecord = Opentelemetry_Proto_Logs_V1_LogRecord()

if let observedTimestamp = logRecord.observedTimestamp {
protoLogRecord.observedTimeUnixNano = observedTimestamp.timeIntervalSince1970.toNanoseconds
}

protoLogRecord.timeUnixNano = logRecord.timestamp.timeIntervalSince1970.toNanoseconds

if let body = logRecord.body, !body.isEmpty {
var protoBody = Opentelemetry_Proto_Common_V1_AnyValue()
protoBody.stringValue = body
protoLogRecord.body = protoBody
}


if let severity = logRecord.severity {
protoLogRecord.severityText = severity.description
if let protoSeverity = Opentelemetry_Proto_Logs_V1_SeverityNumber(rawValue: severity.rawValue) {
protoLogRecord.severityNumber = protoSeverity
}
}

if let context = logRecord.spanContext {
protoLogRecord.spanID = TraceProtoUtils.toProtoSpanId(spanId: context.spanId)
protoLogRecord.traceID = TraceProtoUtils.toProtoTraceId(traceId: context.traceId)
protoLogRecord.flags = UInt32(context.traceFlags.byte)
}

var protoAttributes = [Opentelemetry_Proto_Common_V1_KeyValue]()
logRecord.attributes.forEach { key, value in
protoAttributes.append(CommonAdapter.toProtoAttribute(key: key, attributeValue: value))
}
protoLogRecord.attributes = protoAttributes
return protoLogRecord
return resourceLogs
}

static func groupByResourceAndScope(logRecordList: [ReadableLogRecord]) -> [Resource:[InstrumentationScopeInfo:[Opentelemetry_Proto_Logs_V1_LogRecord]]] {
var result = [Resource:[InstrumentationScopeInfo: [Opentelemetry_Proto_Logs_V1_LogRecord]]]()
logRecordList.forEach { logRecord in
result[logRecord.resource, default:[InstrumentationScopeInfo: [Opentelemetry_Proto_Logs_V1_LogRecord]]()][logRecord.instrumentationScopeInfo,default:[Opentelemetry_Proto_Logs_V1_LogRecord]()].append(toProtoLogRecord(logRecord: logRecord))
}
return result
}

static func toProtoLogRecord(logRecord: ReadableLogRecord) -> Opentelemetry_Proto_Logs_V1_LogRecord {
var protoLogRecord = Opentelemetry_Proto_Logs_V1_LogRecord()

if let observedTimestamp = logRecord.observedTimestamp {
protoLogRecord.observedTimeUnixNano = observedTimestamp.timeIntervalSince1970.toNanoseconds
}

protoLogRecord.timeUnixNano = logRecord.timestamp.timeIntervalSince1970.toNanoseconds

if let body = logRecord.body, !body.isEmpty {
var protoBody = Opentelemetry_Proto_Common_V1_AnyValue()
protoBody.stringValue = body
protoLogRecord.body = protoBody
}


if let severity = logRecord.severity {
protoLogRecord.severityText = severity.description
if let protoSeverity = Opentelemetry_Proto_Logs_V1_SeverityNumber(rawValue: severity.rawValue) {
protoLogRecord.severityNumber = protoSeverity
}
}

if let context = logRecord.spanContext {
protoLogRecord.spanID = TraceProtoUtils.toProtoSpanId(spanId: context.spanId)
protoLogRecord.traceID = TraceProtoUtils.toProtoTraceId(traceId: context.traceId)
protoLogRecord.flags = UInt32(context.traceFlags.byte)
}

var protoAttributes = [Opentelemetry_Proto_Common_V1_KeyValue]()
logRecord.attributes.forEach { key, value in
protoAttributes.append(CommonAdapter.toProtoAttribute(key: key, attributeValue: value))
}
protoLogRecord.attributes = protoAttributes
return protoLogRecord
}
}
Loading

0 comments on commit 0b1a4dd

Please sign in to comment.