-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: "Integration Tests" | ||
on: [push, pull_request] | ||
jobs: | ||
integration_test: | ||
name: Integration Tests | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
swift: ["5.9", "5.10", "latest"] | ||
|
||
steps: | ||
- name: Install Swift | ||
uses: vapor/swiftly-action@v0.1 | ||
with: | ||
toolchain: ${{ matrix.swift }} | ||
env: | ||
SWIFTLY_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Checkout | ||
uses: actions/checkout@v4.2.0 | ||
|
||
- name: Resolve Swift dependencies | ||
run: | | ||
cd IntegrationTests | ||
swift package resolve | ||
- name: Start OTel Collector | ||
run: | | ||
cd IntegrationTests | ||
docker compose -f docker/docker-compose.yaml up -d | ||
- name: Wait for OTel Collector | ||
uses: iFaxity/wait-on-action@v1.2.1 | ||
with: | ||
resource: "file:./IntegrationTests/docker/otel-collector-output/output.jsonl" | ||
timeout: 3000 | ||
|
||
- name: Run Integration Tests | ||
run: | | ||
cd IntegrationTests | ||
OTEL_COLLECTOR_OUTPUT=$(pwd)/docker/otel-collector-output swift test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// swift-tools-version:5.9 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "swift-otel-integration-tests", | ||
dependencies: [ | ||
.package(path: "../"), | ||
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"), | ||
], | ||
targets: [ | ||
.testTarget( | ||
name: "IntegrationTests", | ||
dependencies: [ | ||
.product(name: "OTel", package: "swift-otel"), | ||
.product(name: "OTLPGRPC", package: "swift-otel"), | ||
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"), | ||
], | ||
swiftSettings: [.enableExperimentalFeature("StrictConcurrency=complete")] | ||
), | ||
], | ||
swiftLanguageVersions: [.version("6"), .v5] | ||
) |
133 changes: 133 additions & 0 deletions
133
IntegrationTests/Tests/IntegrationTests/OTLPGRPCIntegrationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift OTel open source project | ||
// | ||
// Copyright (c) 2024 Moritz Lang and the Swift OTel project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
@testable import Instrumentation | ||
@testable import Logging | ||
import NIO | ||
import OTel | ||
import OTLPGRPC | ||
import ServiceLifecycle | ||
import W3CTraceContext | ||
import XCTest | ||
|
||
final class OTLPGRPCIntegrationTests: XCTestCase, @unchecked Sendable { | ||
func test_example() async throws { | ||
LoggingSystem.bootstrapInternal { label in | ||
var handler = StreamLogHandler.standardOutput(label: label) | ||
handler.logLevel = .trace | ||
return handler | ||
} | ||
let logger = Logger(label: "test") | ||
let group = MultiThreadedEventLoopGroup.singleton | ||
let exporter = try OTLPGRPCSpanExporter( | ||
configuration: OTLPGRPCSpanExporterConfiguration(environment: [:]), | ||
group: group, | ||
requestLogger: logger, | ||
backgroundActivityLogger: logger | ||
) | ||
let processor = OTelBatchSpanProcessor( | ||
exporter: exporter, | ||
configuration: .init( | ||
environment: [:], | ||
scheduleDelay: .zero | ||
) | ||
) | ||
let tracer = OTelTracer( | ||
idGenerator: OTelRandomIDGenerator(), | ||
sampler: OTelConstantSampler(isOn: true), | ||
propagator: OTelW3CPropagator(), | ||
processor: processor, | ||
environment: [:], | ||
resource: OTelResource(attributes: ["service.name": "IntegrationTests"]) | ||
) | ||
|
||
InstrumentationSystem.bootstrapInternal(tracer) | ||
|
||
let serviceGroup = ServiceGroup( | ||
configuration: .init( | ||
services: [ | ||
.init(service: tracer), | ||
.init(service: TestService(), successTerminationBehavior: .gracefullyShutdownGroup), | ||
], | ||
logger: logger | ||
) | ||
) | ||
try await serviceGroup.run() | ||
} | ||
} | ||
|
||
struct TestService: Service { | ||
func run() async throws { | ||
let otelCollectorOutputPath = try XCTUnwrap(ProcessInfo.processInfo.environment["OTEL_COLLECTOR_OUTPUT"]) | ||
let outputFileURL = URL(fileURLWithPath: otelCollectorOutputPath).appendingPathComponent("output.jsonl") | ||
XCTAssertTrue(FileManager.default.fileExists(atPath: outputFileURL.path), outputFileURL.path) | ||
|
||
let span = InstrumentationSystem.tracer.startSpan("test") | ||
span.attributes["foo"] = "bar" | ||
span.setStatus(.init(code: .ok)) | ||
span.end() | ||
|
||
// wait for export | ||
try await Task.sleep(for: .seconds(2)) | ||
|
||
let jsonDecoder = JSONDecoder() | ||
let outputFileContents = try String(contentsOf: outputFileURL).trimmingCharacters(in: .whitespacesAndNewlines) | ||
let lines = outputFileContents.components(separatedBy: .newlines) | ||
let exportLine = try XCTUnwrap(lines.last) | ||
let decodedExportLine = try jsonDecoder.decode(ExportLine.self, from: Data(exportLine.utf8)) | ||
let resourceSpans = try XCTUnwrap(decodedExportLine.resourceSpans.first) | ||
let scopeSpans = try XCTUnwrap(resourceSpans.scopeSpans.first) | ||
let exportedSpan = try XCTUnwrap(scopeSpans.spans.first) | ||
|
||
XCTAssertEqual(exportedSpan.spanID, span.context.spanContext?.spanID.description) | ||
XCTAssertEqual(exportedSpan.traceID, span.context.spanContext?.traceID.description) | ||
XCTAssertEqual(exportedSpan.name, "test") | ||
XCTAssertEqual(exportedSpan.attributes, [.init(key: "foo", value: .init(stringValue: "bar"))]) | ||
} | ||
} | ||
|
||
struct ExportLine: Decodable { | ||
let resourceSpans: [ResourceSpan] | ||
|
||
struct ResourceSpan: Decodable { | ||
let scopeSpans: [ScopeSpans] | ||
|
||
struct ScopeSpans: Decodable { | ||
let spans: [Span] | ||
|
||
struct Span: Decodable { | ||
let traceID: String | ||
let spanID: String | ||
let name: String | ||
let attributes: [Attribute] | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case traceID = "traceId" | ||
case spanID = "spanId" | ||
case name | ||
case attributes | ||
} | ||
|
||
struct Attribute: Decodable, Equatable { | ||
let key: String | ||
let value: Value | ||
|
||
struct Value: Decodable, Equatable { | ||
let stringValue: String | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: swift-otel-integration-tests | ||
services: | ||
otel-collector: | ||
image: otel/opentelemetry-collector-contrib:latest | ||
command: ["--config=/etc/otel-collector-config.yaml"] | ||
volumes: | ||
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml | ||
- ./otel-collector-output:/etc/otel-collector-output | ||
ports: | ||
- "4317:4317" # OTLP gRPC receiver | ||
|
||
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
endpoint: "otel-collector:4317" | ||
|
||
exporters: | ||
file: | ||
path: /etc/otel-collector-output/output.jsonl | ||
|
||
service: | ||
pipelines: | ||
traces: | ||
receivers: [otlp] | ||
exporters: [file] | ||
|
||
# yaml-language-server: $schema=https://raw.githubusercontent.com/srikanthccv/otelcol-jsonschema/main/schema.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters