Skip to content

Commit

Permalink
Merge branch 'main' into mdalmamun/add-batch-span-processor-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mamunto authored Nov 15, 2024
2 parents 43e5b42 + 725e134 commit d910005
Show file tree
Hide file tree
Showing 18 changed files with 440 additions and 32 deletions.
10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let package = Package(
.library(name: "DatadogExporter", type: .static, targets: ["DatadogExporter"]),
.library(name: "NetworkStatus", type: .static, targets: ["NetworkStatus"]),
.library(name: "OTelSwiftLog", type: .static, targets: ["OTelSwiftLog"]),
.library(name: "DataCompression", type: .static, targets: ["DataCompression"]),
.executable(name: "simpleExporter", targets: ["SimpleExporter"]),
.executable(name: "OTLPExporter", targets: ["OTLPExporter"]),
.executable(name: "OTLPHTTPExporter", targets: ["OTLPHTTPExporter"]),
Expand Down Expand Up @@ -110,14 +111,18 @@ let package = Package(
"OpenTelemetryProtocolExporterCommon",
.product(name: "GRPC", package: "grpc-swift")],
path: "Sources/Exporters/OpenTelemetryProtocolGrpc"),
.target(name: "DataCompression",
dependencies: [],
path: "Sources/Exporters/DataCompression"),
.target(name: "StdoutExporter",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Exporters/Stdout"),
.target(name: "InMemoryExporter",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Exporters/InMemory"),
.target(name: "DatadogExporter",
dependencies: ["OpenTelemetrySdk"],
dependencies: ["OpenTelemetrySdk",
"DataCompression"],
path: "Sources/Exporters/DatadogExporter",
exclude: ["NOTICE", "README.md"]),
.target(name: "PersistenceExporter",
Expand Down Expand Up @@ -163,6 +168,7 @@ let package = Package(
.testTarget(name: "OpenTelemetryProtocolExporterTests",
dependencies: ["OpenTelemetryProtocolExporterGrpc",
"OpenTelemetryProtocolExporterHttp",
"DataCompression",
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOTestUtils", package: "swift-nio")],
Expand Down Expand Up @@ -190,7 +196,7 @@ let package = Package(
path: "Examples/OTLP Exporter",
exclude: ["README.md"]),
.target(name: "OTLPHTTPExporter",
dependencies: ["OpenTelemetryProtocolExporterHttp", "StdoutExporter", "ZipkinExporter", "ResourceExtension", "SignPostIntegration"],
dependencies: ["OpenTelemetryProtocolExporterHttp", "StdoutExporter", "ZipkinExporter", "ResourceExtension", "SignPostIntegration", "DataCompression"],
path: "Examples/OTLP HTTP Exporter",
exclude: ["README.md"]),
.target(name: "PrometheusSample",
Expand Down
10 changes: 8 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let package = Package(
.library(name: "DatadogExporter", type: .static, targets: ["DatadogExporter"]),
.library(name: "NetworkStatus", type: .static, targets: ["NetworkStatus"]),
.library(name: "OTelSwiftLog", type: .static, targets: ["OTelSwiftLog"]),
.library(name: "DataCompression", type: .static, targets: ["DataCompression"]),
.executable(name: "simpleExporter", targets: ["SimpleExporter"]),
.executable(name: "OTLPExporter", targets: ["OTLPExporter"]),
.executable(name: "OTLPHTTPExporter", targets: ["OTLPHTTPExporter"]),
Expand Down Expand Up @@ -115,14 +116,18 @@ let package = Package(
"OpenTelemetryProtocolExporterCommon",
.product(name: "GRPC", package: "grpc-swift")],
path: "Sources/Exporters/OpenTelemetryProtocolGrpc"),
.target(name: "DataCompression",
dependencies: [],
path: "Sources/Exporters/DataCompression"),
.target(name: "StdoutExporter",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Exporters/Stdout"),
.target(name: "InMemoryExporter",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Exporters/InMemory"),
.target(name: "DatadogExporter",
dependencies: ["OpenTelemetrySdk"],
dependencies: ["OpenTelemetrySdk",
"DataCompression"],
path: "Sources/Exporters/DatadogExporter",
exclude: ["NOTICE", "README.md"]),
.target(name: "PersistenceExporter",
Expand Down Expand Up @@ -173,6 +178,7 @@ let package = Package(
.testTarget(name: "OpenTelemetryProtocolExporterTests",
dependencies: ["OpenTelemetryProtocolExporterGrpc",
"OpenTelemetryProtocolExporterHttp",
"DataCompression",
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOTestUtils", package: "swift-nio")],
Expand Down Expand Up @@ -207,7 +213,7 @@ let package = Package(
),
.executableTarget(
name: "OTLPHTTPExporter",
dependencies: ["OpenTelemetrySdk", "OpenTelemetryProtocolExporterHttp", "StdoutExporter", "ZipkinExporter", "ResourceExtension", "SignPostIntegration"],
dependencies: ["OpenTelemetrySdk", "OpenTelemetryProtocolExporterHttp", "StdoutExporter", "ZipkinExporter", "ResourceExtension", "SignPostIntegration", "DataCompression"],
path: "Examples/OTLP HTTP Exporter",
exclude: ["README.md"]
),
Expand Down
10 changes: 8 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let package = Package(
.library(name: "PersistenceExporter", targets: ["PersistenceExporter"]),
.library(name: "InMemoryExporter", targets: ["InMemoryExporter"]),
.library(name: "OTelSwiftLog", targets: ["OTelSwiftLog"]),
.library(name: "DataCompression", type: .static, targets: ["DataCompression"]),
.executable(name: "ConcurrencyContext", targets: ["ConcurrencyContext"]),
.executable(name: "loggingTracer", targets: ["LoggingTracer"]),
],
Expand Down Expand Up @@ -70,6 +71,9 @@ let package = Package(
"OpenTelemetryProtocolExporterCommon",
.product(name: "GRPC", package: "grpc-swift")],
path: "Sources/Exporters/OpenTelemetryProtocolGrpc"),
.target(name: "DataCompression",
dependencies: [],
path: "Sources/Exporters/DataCompression"),
.target(name: "StdoutExporter",
dependencies: ["OpenTelemetrySdk"],
path: "Sources/Exporters/Stdout"),
Expand Down Expand Up @@ -100,6 +104,7 @@ let package = Package(
.testTarget(name: "OpenTelemetryProtocolExporterTests",
dependencies: ["OpenTelemetryProtocolExporterGrpc",
"OpenTelemetryProtocolExporterHttp",
"DataCompression",
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOTestUtils", package: "swift-nio")],
Expand Down Expand Up @@ -240,7 +245,7 @@ extension Package {
),
.executableTarget(
name: "OTLPHTTPExporter",
dependencies: ["OpenTelemetrySdk", "OpenTelemetryProtocolExporterHttp", "StdoutExporter", "ZipkinExporter", "ResourceExtension", "SignPostIntegration"],
dependencies: ["OpenTelemetrySdk", "OpenTelemetryProtocolExporterHttp", "StdoutExporter", "ZipkinExporter", "ResourceExtension", "SignPostIntegration", "DataCompression"],
path: "Examples/OTLP HTTP Exporter",
exclude: ["README.md"]
),
Expand All @@ -256,7 +261,8 @@ extension Package {
dependencies: ["ResourceExtension", "OpenTelemetrySdk"],
path: "Tests/InstrumentationTests/SDKResourceExtensionTests"),
.target(name: "DatadogExporter",
dependencies: ["OpenTelemetrySdk"],
dependencies: ["OpenTelemetrySdk",
"DataCompression"],
path: "Sources/Exporters/DatadogExporter",
exclude: ["NOTICE", "README.md"]),
.testTarget(name: "DatadogExporterTests",
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ or
.target(name: "ExampleApp", dependencies: ["OpenTelemetrySdk"]),
```

### Cocoapods

As of version 1.11.0, OpenTelemetry-Swift support cocoapods.
Two pods are provided:

- `OpenTelemetry-Swift-Api`

- `OpenTelemetry-Swift-Sdk`

`OpenTelemetry-Swift-Api` is a dependency of `OpenTelemetry-Swift-Sdk`.

Most users will want to add the following to their pod file:

`pod 'OpenTelemetry-Swift-Sdk'`

This will add both the API and SDK. If you're only interesting in Adding the API add the following:

`pod 'OpenTelemetry-Swift-Api'`

## Documentation

Official documentation for the library can be found in the official opentelemetry [documentation page](https://opentelemetry.io/docs/instrumentation/swift/), including:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@


import Foundation

#if canImport(Compression)
import Compression

extension Data
public extension Data
{
/// Compresses the data.
/// - parameter withAlgorithm: Compression algorithm to use. See the `CompressionAlgorithm` type
Expand Down Expand Up @@ -247,7 +249,7 @@ extension Data

/// Calculate the Adler32 checksum of the data.
/// - returns: Adler32 checksum type. Can still be further advanced.
func adler32() -> Adler32
internal func adler32() -> Adler32
{
var res = Adler32()
res.advance(withChunk: self)
Expand All @@ -256,7 +258,7 @@ extension Data

/// Calculate the Crc32 checksum of the data.
/// - returns: Crc32 checksum type. Can still be further advanced.
func crc32() -> Crc32
internal func crc32() -> Crc32
{
var res = Crc32()
res.advance(withChunk: self)
Expand Down Expand Up @@ -514,3 +516,4 @@ fileprivate func perform(_ config: Config, source: UnsafePointer<UInt8>, sourceS
}
}
}
#endif
3 changes: 3 additions & 0 deletions Sources/Exporters/DatadogExporter/Upload/RequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

#if canImport(Compression)
import DataCompression
#endif
import Foundation

/// Builds `URLRequest` for sending data to Datadog.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

import Foundation

public enum CompressionType {
case gzip
case deflate
case none
}

public struct OtlpConfiguration {
public static let DefaultTimeoutInterval : TimeInterval = TimeInterval(10)

Expand All @@ -23,9 +29,15 @@ public struct OtlpConfiguration {
// let compression
public let headers : [(String,String)]?
public let timeout : TimeInterval
public let compression: CompressionType

public init(timeout : TimeInterval = OtlpConfiguration.DefaultTimeoutInterval, headers: [(String,String)]? = nil) {
public init(
timeout : TimeInterval = OtlpConfiguration.DefaultTimeoutInterval,
compression: CompressionType = .gzip,
headers: [(String,String)]? = nil
) {
self.headers = headers
self.timeout = timeout
self.compression = compression
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// SPDX-License-Identifier: Apache-2.0
//

#if canImport(Compression)
import DataCompression
#endif
import Foundation
import SwiftProtobuf
import OpenTelemetryProtocolExporterCommon
Expand All @@ -28,20 +31,44 @@ public class OtlpHttpExporterBase {
}
}

public func createRequest(body: Message, endpoint: URL) -> URLRequest {
var request = URLRequest(url: endpoint)

do {
request.httpMethod = "POST"
request.httpBody = try body.serializedData()
request.setValue(Headers.getUserAgentHeader(), forHTTPHeaderField: Constants.HTTP.userAgent)
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")
} catch {
print("Error serializing body: \(error)")
public func createRequest(body: Message, endpoint: URL) -> URLRequest {
var request = URLRequest(url: endpoint)

do {
let rawData = try body.serializedData()
request.httpMethod = "POST"
request.setValue(Headers.getUserAgentHeader(), forHTTPHeaderField: Constants.HTTP.userAgent)
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")

var compressedData = rawData

#if canImport(Compression)
switch config.compression {
case .gzip:
if let data = rawData.gzip() {
compressedData = data
request.setValue("gzip", forHTTPHeaderField: "Content-Encoding")
}

case .deflate:
if let data = rawData.deflate() {
compressedData = data
request.setValue("deflate", forHTTPHeaderField: "Content-Encoding")
}

case .none:
break
}
#endif
// Apply final data. Could be compressed or raw
// but it doesn't matter here
request.httpBody = compressedData
} catch {
print("Error serializing body: \(error)")
}

return request
}

return request
}

public func shutdown(explicitTimeout: TimeInterval? = nil) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// SPDX-License-Identifier: Apache-2.0
//

#if canImport(Compression)
import DataCompression
#endif
import Foundation
import OpenTelemetryProtocolExporterCommon
import SwiftProtobuf
Expand Down Expand Up @@ -44,14 +47,38 @@ public class StableOtlpHTTPExporterBase {
}

do {
let rawData = try body.serializedData()
request.httpMethod = "POST"
request.httpBody = try body.serializedData()
request.setValue(Headers.getUserAgentHeader(), forHTTPHeaderField: Constants.HTTP.userAgent)
request.setValue("application/x-protobuf", forHTTPHeaderField: "Content-Type")

var compressedData = rawData

#if canImport(Compression)
switch config.compression {
case .gzip:
if let data = rawData.gzip() {
compressedData = data
request.setValue("gzip", forHTTPHeaderField: "Content-Encoding")
}

case .deflate:
if let data = rawData.deflate() {
compressedData = data
request.setValue("deflate", forHTTPHeaderField: "Content-Encoding")
}

case .none:
break
}
#endif

// Apply final data. Could be compressed or raw
// but it doesn't matter here
request.httpBody = compressedData
} catch {
print("Error serializing body: \(error)")
}

return request
}

Expand Down
11 changes: 10 additions & 1 deletion Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class RecordEventsReadableSpan: ReadableSpan {
public private(set) var totalRecordedLinks: Int
/// Max number of attributes per span.
public private(set) var maxNumberOfAttributes: Int
/// Max value length of attribute per span.
public private(set) var maxValueLengthPerSpanAttribute: Int
/// Max number of attributes per event.
public private(set) var maxNumberOfAttributesPerEvent: Int

Expand Down Expand Up @@ -140,6 +142,7 @@ public class RecordEventsReadableSpan: ReadableSpan {
events = ArrayWithCapacity<SpanData.Event>(capacity: spanLimits.eventCountLimit)
maxNumberOfAttributes = spanLimits.attributeCountLimit
maxNumberOfAttributesPerEvent = spanLimits.attributePerEventCountLimit
maxValueLengthPerSpanAttribute = spanLimits.attributeValueLengthLimit
}

/// Creates and starts a span with the given configuration.
Expand Down Expand Up @@ -253,7 +256,13 @@ public class RecordEventsReadableSpan: ReadableSpan {
if attributes[key] == nil, totalAttributeCount > maxNumberOfAttributes {
return
}
attributes[key] = value
/// Process only `string` type value
if case .string(let value) = value {
let formattedValue = value.count > maxValueLengthPerSpanAttribute ? String(value.prefix(maxValueLengthPerSpanAttribute)) : value
attributes[key] = AttributeValue(formattedValue)
} else {
attributes[key] = value
}
}
}

Expand Down
Loading

0 comments on commit d910005

Please sign in to comment.