From aae43bb87fb6b2d3b525762157ab873af1c74f67 Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Tue, 6 Aug 2024 17:14:03 +1200 Subject: [PATCH] Tracing (#54) * setup tracing utils * setup tracing --- Blockchain/Package.resolved | 155 +++++++++++++++++- Blockchain/Package.swift | 2 + .../Sources/Blockchain/Blockchain.swift | 13 +- .../BlockchainDataProvider.swift | 0 .../InMemoryDataProvider.swift | 0 Boka/Package.resolved | 155 +++++++++++++++++- Boka/Package.swift | 7 + Boka/Sources/Boka.swift | 10 +- Boka/Sources/Tracing.swift | 61 +++++++ JAMTests/Package.resolved | 144 ++++++++++++++++ Makefile | 2 +- Node/Package.resolved | 155 +++++++++++++++++- Node/Package.swift | 2 + Node/Sources/Node/Node.swift | 7 +- TracingUtils/.gitignore | 8 + TracingUtils/Package.resolved | 42 +++++ TracingUtils/Package.swift | 35 ++++ .../Sources/TracingUtils/Tracing.swift | 3 + boka.xcodeproj/project.pbxproj | 2 + .../xcshareddata/swiftpm/Package.resolved | 155 +++++++++++++++++- scripts/runTests.sh | 13 ++ 21 files changed, 960 insertions(+), 11 deletions(-) rename Blockchain/Sources/Blockchain/{ => BlockchainDataProvider}/BlockchainDataProvider.swift (100%) rename Blockchain/Sources/Blockchain/{ => BlockchainDataProvider}/InMemoryDataProvider.swift (100%) create mode 100644 Boka/Sources/Tracing.swift create mode 100644 TracingUtils/.gitignore create mode 100644 TracingUtils/Package.resolved create mode 100644 TracingUtils/Package.swift create mode 100644 TracingUtils/Sources/TracingUtils/Tracing.swift create mode 100755 scripts/runTests.sh diff --git a/Blockchain/Package.resolved b/Blockchain/Package.resolved index c5df876f..aa30ab09 100644 --- a/Blockchain/Package.resolved +++ b/Blockchain/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "b411c6a0bd87c7eb9917abe3f20455e142ffe77b06622bdfbf0cf5a8c002daf8", + "originHash" : "a6bbe74d9b5891b7b3286ff9db782962187c36afbc7f0261e2d03479b2b04bf5", "pins" : [ { "identity" : "blake2.swift", @@ -10,6 +10,15 @@ "version" : "0.2.0" } }, + { + "identity" : "grpc-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/grpc/grpc-swift.git", + "state" : { + "revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1", + "version" : "1.23.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", @@ -19,6 +28,15 @@ "revision" : "dac3e7161de34c60c82794d031de0231b5a5746e" } }, + { + "identity" : "swift-async-algorithms", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-async-algorithms.git", + "state" : { + "revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20", + "version" : "1.0.1" + } + }, { "identity" : "swift-atomics", "kind" : "remoteSourceControl", @@ -28,6 +46,15 @@ "version" : "1.2.0" } }, + { + "identity" : "swift-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-collections.git", + "state" : { + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" + } + }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -37,6 +64,123 @@ "version" : "3.4.0" } }, + { + "identity" : "swift-distributed-tracing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-distributed-tracing.git", + "state" : { + "revision" : "11c756c5c4d7de0eeed8595695cadd7fa107aa19", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-http-types", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-http-types", + "state" : { + "revision" : "ae67c8178eb46944fd85e4dc6dd970e1f3ed6ccd", + "version" : "1.3.0" + } + }, + { + "identity" : "swift-log", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-log.git", + "state" : { + "revision" : "9cb486020ebf03bfa5b5df985387a14a98744537", + "version" : "1.6.1" + } + }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, + { + "identity" : "swift-nio", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio.git", + "state" : { + "revision" : "4c4453b489cf76e6b3b0f300aba663eb78182fad", + "version" : "2.70.0" + } + }, + { + "identity" : "swift-nio-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-extras.git", + "state" : { + "revision" : "05c36b57453d23ea63785d58a7dbc7b70ba1745e", + "version" : "1.23.0" + } + }, + { + "identity" : "swift-nio-http2", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-http2.git", + "state" : { + "revision" : "b5f7062b60e4add1e8c343ba4eb8da2e324b3a94", + "version" : "1.34.0" + } + }, + { + "identity" : "swift-nio-ssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-ssl.git", + "state" : { + "revision" : "a9fa5efd86e7ce2e5c1b6de113262e58035ca251", + "version" : "2.27.1" + } + }, + { + "identity" : "swift-nio-transport-services", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-transport-services.git", + "state" : { + "revision" : "38ac8221dd20674682148d6451367f89c2652980", + "version" : "1.21.0" + } + }, + { + "identity" : "swift-otel", + "kind" : "remoteSourceControl", + "location" : "https://github.com/slashmo/swift-otel.git", + "state" : { + "revision" : "8c271c7fed34a39f29c728598b3358fbdddf8ff4", + "version" : "0.9.0" + } + }, + { + "identity" : "swift-protobuf", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-protobuf.git", + "state" : { + "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106", + "version" : "1.27.1" + } + }, + { + "identity" : "swift-service-context", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-service-context.git", + "state" : { + "revision" : "0c62c5b4601d6c125050b5c3a97f20cce881d32b", + "version" : "1.1.0" + } + }, + { + "identity" : "swift-service-lifecycle", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-server/swift-service-lifecycle.git", + "state" : { + "revision" : "24c800fb494fbee6e42bc156dc94232dc08971af", + "version" : "2.6.1" + } + }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", @@ -46,6 +190,15 @@ "version" : "600.0.0-prerelease-2024-06-12" } }, + { + "identity" : "swift-system", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-system.git", + "state" : { + "revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5", + "version" : "1.3.2" + } + }, { "identity" : "swift-testing", "kind" : "remoteSourceControl", diff --git a/Blockchain/Package.swift b/Blockchain/Package.swift index 97729691..88ff2c83 100644 --- a/Blockchain/Package.swift +++ b/Blockchain/Package.swift @@ -17,6 +17,7 @@ let package = Package( ], dependencies: [ .package(path: "../Utils"), + .package(path: "../TracingUtils"), .package(url: "https://github.com/AcalaNetwork/ScaleCodec.swift.git", branch: "main"), .package(url: "https://github.com/apple/swift-testing.git", branch: "0.10.0"), ], @@ -27,6 +28,7 @@ let package = Package( name: "Blockchain", dependencies: [ "Utils", + "TracingUtils", .product(name: "ScaleCodec", package: "ScaleCodec.swift"), ] ), diff --git a/Blockchain/Sources/Blockchain/Blockchain.swift b/Blockchain/Sources/Blockchain/Blockchain.swift index 75828ff3..813ac9e2 100644 --- a/Blockchain/Sources/Blockchain/Blockchain.swift +++ b/Blockchain/Sources/Blockchain/Blockchain.swift @@ -1,4 +1,5 @@ import Foundation +import TracingUtils import Utils /// Holds the state of the blockchain. @@ -15,10 +16,14 @@ public class Blockchain { } public func importBlock(_ block: BlockRef) async throws { - let runtime = Runtime(config: config) - let parent = try await dataProvider.getState(hash: block.header.parentHash) - let state = try runtime.apply(block: block, state: parent) - try await dataProvider.add(state: state) + try await withSpan("importBlock") { span in + span.attributes["hash"] = block.hash.description + + let runtime = Runtime(config: config) + let parent = try await dataProvider.getState(hash: block.header.parentHash) + let state = try runtime.apply(block: block, state: parent) + try await dataProvider.add(state: state) + } } public func finalize(hash: Data32) async throws { diff --git a/Blockchain/Sources/Blockchain/BlockchainDataProvider.swift b/Blockchain/Sources/Blockchain/BlockchainDataProvider/BlockchainDataProvider.swift similarity index 100% rename from Blockchain/Sources/Blockchain/BlockchainDataProvider.swift rename to Blockchain/Sources/Blockchain/BlockchainDataProvider/BlockchainDataProvider.swift diff --git a/Blockchain/Sources/Blockchain/InMemoryDataProvider.swift b/Blockchain/Sources/Blockchain/BlockchainDataProvider/InMemoryDataProvider.swift similarity index 100% rename from Blockchain/Sources/Blockchain/InMemoryDataProvider.swift rename to Blockchain/Sources/Blockchain/BlockchainDataProvider/InMemoryDataProvider.swift diff --git a/Boka/Package.resolved b/Boka/Package.resolved index e7656085..06ae0b6e 100644 --- a/Boka/Package.resolved +++ b/Boka/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "475c93f2f8fce84eeeffbeb4316d5dea963cbb7149d3e311699cffd826f243ea", + "originHash" : "b09a7b54cb34c5b40b9c30546e215da07c64f3935c5d0c0b0daca60d07584231", "pins" : [ { "identity" : "blake2.swift", @@ -10,6 +10,15 @@ "version" : "0.2.0" } }, + { + "identity" : "grpc-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/grpc/grpc-swift.git", + "state" : { + "revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1", + "version" : "1.23.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", @@ -28,6 +37,15 @@ "version" : "1.4.0" } }, + { + "identity" : "swift-async-algorithms", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-async-algorithms.git", + "state" : { + "revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20", + "version" : "1.0.1" + } + }, { "identity" : "swift-atomics", "kind" : "remoteSourceControl", @@ -37,6 +55,15 @@ "version" : "1.2.0" } }, + { + "identity" : "swift-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-collections.git", + "state" : { + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" + } + }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -46,6 +73,132 @@ "version" : "3.4.0" } }, + { + "identity" : "swift-distributed-tracing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-distributed-tracing.git", + "state" : { + "revision" : "11c756c5c4d7de0eeed8595695cadd7fa107aa19", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-http-types", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-http-types", + "state" : { + "revision" : "ae67c8178eb46944fd85e4dc6dd970e1f3ed6ccd", + "version" : "1.3.0" + } + }, + { + "identity" : "swift-log", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-log.git", + "state" : { + "revision" : "9cb486020ebf03bfa5b5df985387a14a98744537", + "version" : "1.6.1" + } + }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, + { + "identity" : "swift-nio", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio.git", + "state" : { + "revision" : "4c4453b489cf76e6b3b0f300aba663eb78182fad", + "version" : "2.70.0" + } + }, + { + "identity" : "swift-nio-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-extras.git", + "state" : { + "revision" : "05c36b57453d23ea63785d58a7dbc7b70ba1745e", + "version" : "1.23.0" + } + }, + { + "identity" : "swift-nio-http2", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-http2.git", + "state" : { + "revision" : "b5f7062b60e4add1e8c343ba4eb8da2e324b3a94", + "version" : "1.34.0" + } + }, + { + "identity" : "swift-nio-ssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-ssl.git", + "state" : { + "revision" : "a9fa5efd86e7ce2e5c1b6de113262e58035ca251", + "version" : "2.27.1" + } + }, + { + "identity" : "swift-nio-transport-services", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-transport-services.git", + "state" : { + "revision" : "38ac8221dd20674682148d6451367f89c2652980", + "version" : "1.21.0" + } + }, + { + "identity" : "swift-otel", + "kind" : "remoteSourceControl", + "location" : "https://github.com/slashmo/swift-otel.git", + "state" : { + "revision" : "8c271c7fed34a39f29c728598b3358fbdddf8ff4", + "version" : "0.9.0" + } + }, + { + "identity" : "swift-protobuf", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-protobuf.git", + "state" : { + "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106", + "version" : "1.27.1" + } + }, + { + "identity" : "swift-service-context", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-service-context.git", + "state" : { + "revision" : "0c62c5b4601d6c125050b5c3a97f20cce881d32b", + "version" : "1.1.0" + } + }, + { + "identity" : "swift-service-lifecycle", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-server/swift-service-lifecycle.git", + "state" : { + "revision" : "24c800fb494fbee6e42bc156dc94232dc08971af", + "version" : "2.6.1" + } + }, + { + "identity" : "swift-system", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-system.git", + "state" : { + "revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5", + "version" : "1.3.2" + } + }, { "identity" : "tuples.swift", "kind" : "remoteSourceControl", diff --git a/Boka/Package.swift b/Boka/Package.swift index e9d87808..1cdd988c 100644 --- a/Boka/Package.swift +++ b/Boka/Package.swift @@ -10,7 +10,10 @@ let package = Package( ], dependencies: [ .package(path: "../Node"), + .package(path: "../TracingUtils"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.4.0"), + .package(url: "https://github.com/slashmo/swift-otel.git", from: "0.9.0"), + .package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.6.0"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. @@ -19,7 +22,11 @@ let package = Package( name: "Boka", dependencies: [ "Node", + "TracingUtils", .product(name: "ArgumentParser", package: "swift-argument-parser"), + .product(name: "ServiceLifecycle", package: "swift-service-lifecycle"), + .product(name: "OTel", package: "swift-otel"), + .product(name: "OTLPGRPC", package: "swift-otel"), ] ), .testTarget( diff --git a/Boka/Sources/Boka.swift b/Boka/Sources/Boka.swift index 205cbbdf..aea94e87 100644 --- a/Boka/Sources/Boka.swift +++ b/Boka/Sources/Boka.swift @@ -6,11 +6,19 @@ import ArgumentParser import Node +import ServiceLifecycle +import TracingUtils @main -struct Boka: ParsableCommand { +struct Boka: AsyncParsableCommand { mutating func run() async throws { + let services = try await Tracing.bootstrap("Boka") let node = try await Node(genesis: .dev, config: .dev) node.sayHello() + + let config = ServiceGroupConfiguration(services: services, logger: Logger(label: "boka")) + let serviceGroup = ServiceGroup(configuration: config) + + try await serviceGroup.run() } } diff --git a/Boka/Sources/Tracing.swift b/Boka/Sources/Tracing.swift new file mode 100644 index 00000000..43e165e7 --- /dev/null +++ b/Boka/Sources/Tracing.swift @@ -0,0 +1,61 @@ +import OTel +import OTLPGRPC +import ServiceLifecycle +import TracingUtils + +public enum Tracing { + public static func bootstrap(_ serviceName: String) async throws -> [Service] { + // Bootstrap the logging backend with the OTel metadata provider which includes span IDs in logging messages. + LoggingSystem.bootstrap { label in + var handler = StreamLogHandler.standardError(label: label, metadataProvider: .otel) + handler.logLevel = .trace + return handler + } + + // Configure OTel resource detection to automatically apply helpful attributes to events. + let environment = OTelEnvironment.detected() + let resourceDetection = OTelResourceDetection(detectors: [ + OTelProcessResourceDetector(), + OTelEnvironmentResourceDetector(environment: environment), + .manual( + OTelResource( + attributes: SpanAttributes(["service.name": serviceName.toSpanAttribute()]) + ) + ), + ]) + let resource = await resourceDetection.resource(environment: environment, logLevel: .trace) + + // Bootstrap the metrics backend to export metrics periodically in OTLP/gRPC. + let registry = OTelMetricRegistry() + let metricsExporter = try OTLPGRPCMetricExporter( + configuration: .init(environment: environment) + ) + let metrics = OTelPeriodicExportingMetricsReader( + resource: resource, + producer: registry, + exporter: metricsExporter, + configuration: .init( + environment: environment, + exportInterval: .seconds(5) // NOTE: This is overridden for the example; the default is 60 seconds. + ) + ) + MetricsSystem.bootstrap(OTLPMetricsFactory(registry: registry)) + + // Bootstrap the tracing backend to export traces periodically in OTLP/gRPC. + let exporter = try OTLPGRPCSpanExporter(configuration: .init(environment: environment)) + let processor = OTelBatchSpanProcessor( + exporter: exporter, configuration: .init(environment: environment) + ) + let tracer = OTelTracer( + idGenerator: OTelRandomIDGenerator(), + sampler: OTelConstantSampler(isOn: true), + propagator: OTelW3CPropagator(), + processor: processor, + environment: environment, + resource: resource + ) + InstrumentationSystem.bootstrap(tracer) + + return [tracer, metrics] + } +} diff --git a/JAMTests/Package.resolved b/JAMTests/Package.resolved index ffc01582..5d1cffcb 100644 --- a/JAMTests/Package.resolved +++ b/JAMTests/Package.resolved @@ -10,6 +10,15 @@ "version" : "0.2.0" } }, + { + "identity" : "grpc-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/grpc/grpc-swift.git", + "state" : { + "revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1", + "version" : "1.23.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", @@ -19,6 +28,15 @@ "revision" : "dac3e7161de34c60c82794d031de0231b5a5746e" } }, + { + "identity" : "swift-async-algorithms", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-async-algorithms.git", + "state" : { + "revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20", + "version" : "1.0.1" + } + }, { "identity" : "swift-atomics", "kind" : "remoteSourceControl", @@ -28,6 +46,15 @@ "version" : "1.2.0" } }, + { + "identity" : "swift-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-collections.git", + "state" : { + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" + } + }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -37,6 +64,24 @@ "version" : "3.5.0" } }, + { + "identity" : "swift-distributed-tracing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-distributed-tracing.git", + "state" : { + "revision" : "11c756c5c4d7de0eeed8595695cadd7fa107aa19", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-http-types", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-http-types", + "state" : { + "revision" : "ae67c8178eb46944fd85e4dc6dd970e1f3ed6ccd", + "version" : "1.3.0" + } + }, { "identity" : "swift-log", "kind" : "remoteSourceControl", @@ -46,6 +91,96 @@ "version" : "1.6.1" } }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, + { + "identity" : "swift-nio", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio.git", + "state" : { + "revision" : "4c4453b489cf76e6b3b0f300aba663eb78182fad", + "version" : "2.70.0" + } + }, + { + "identity" : "swift-nio-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-extras.git", + "state" : { + "revision" : "05c36b57453d23ea63785d58a7dbc7b70ba1745e", + "version" : "1.23.0" + } + }, + { + "identity" : "swift-nio-http2", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-http2.git", + "state" : { + "revision" : "b5f7062b60e4add1e8c343ba4eb8da2e324b3a94", + "version" : "1.34.0" + } + }, + { + "identity" : "swift-nio-ssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-ssl.git", + "state" : { + "revision" : "a9fa5efd86e7ce2e5c1b6de113262e58035ca251", + "version" : "2.27.1" + } + }, + { + "identity" : "swift-nio-transport-services", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-transport-services.git", + "state" : { + "revision" : "38ac8221dd20674682148d6451367f89c2652980", + "version" : "1.21.0" + } + }, + { + "identity" : "swift-otel", + "kind" : "remoteSourceControl", + "location" : "https://github.com/slashmo/swift-otel.git", + "state" : { + "revision" : "8c271c7fed34a39f29c728598b3358fbdddf8ff4", + "version" : "0.9.0" + } + }, + { + "identity" : "swift-protobuf", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-protobuf.git", + "state" : { + "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106", + "version" : "1.27.1" + } + }, + { + "identity" : "swift-service-context", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-service-context.git", + "state" : { + "revision" : "0c62c5b4601d6c125050b5c3a97f20cce881d32b", + "version" : "1.1.0" + } + }, + { + "identity" : "swift-service-lifecycle", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-server/swift-service-lifecycle.git", + "state" : { + "revision" : "24c800fb494fbee6e42bc156dc94232dc08971af", + "version" : "2.6.1" + } + }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", @@ -55,6 +190,15 @@ "version" : "600.0.0-prerelease-2024-06-12" } }, + { + "identity" : "swift-system", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-system.git", + "state" : { + "revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5", + "version" : "1.3.2" + } + }, { "identity" : "swift-testing", "kind" : "remoteSourceControl", diff --git a/Makefile b/Makefile index 0a951eef..8635761b 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ deps: .lib/libblst.a .lib/libbandersnatch_vrfs.a .lib/librocksdb.a .PHONY: test test: githooks deps - ./scripts/run.sh test + ./scripts/runTests.sh test .PHONY: build build: githooks deps diff --git a/Node/Package.resolved b/Node/Package.resolved index 3600e1bd..7dcbaed3 100644 --- a/Node/Package.resolved +++ b/Node/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "2447480b6b36d9ab131cbbcd738cbd43adf06e47c8bcf92541d8ec6d946ee381", + "originHash" : "9029b5282681161e046bf609299ad38f9148736fe1e0db6b71446a8b1ce5274b", "pins" : [ { "identity" : "blake2.swift", @@ -10,6 +10,15 @@ "version" : "0.2.0" } }, + { + "identity" : "grpc-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/grpc/grpc-swift.git", + "state" : { + "revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1", + "version" : "1.23.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", @@ -19,6 +28,15 @@ "revision" : "dac3e7161de34c60c82794d031de0231b5a5746e" } }, + { + "identity" : "swift-async-algorithms", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-async-algorithms.git", + "state" : { + "revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20", + "version" : "1.0.1" + } + }, { "identity" : "swift-atomics", "kind" : "remoteSourceControl", @@ -28,6 +46,15 @@ "version" : "1.2.0" } }, + { + "identity" : "swift-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-collections.git", + "state" : { + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" + } + }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -37,6 +64,132 @@ "version" : "3.4.0" } }, + { + "identity" : "swift-distributed-tracing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-distributed-tracing.git", + "state" : { + "revision" : "11c756c5c4d7de0eeed8595695cadd7fa107aa19", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-http-types", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-http-types", + "state" : { + "revision" : "ae67c8178eb46944fd85e4dc6dd970e1f3ed6ccd", + "version" : "1.3.0" + } + }, + { + "identity" : "swift-log", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-log.git", + "state" : { + "revision" : "9cb486020ebf03bfa5b5df985387a14a98744537", + "version" : "1.6.1" + } + }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, + { + "identity" : "swift-nio", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio.git", + "state" : { + "revision" : "4c4453b489cf76e6b3b0f300aba663eb78182fad", + "version" : "2.70.0" + } + }, + { + "identity" : "swift-nio-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-extras.git", + "state" : { + "revision" : "05c36b57453d23ea63785d58a7dbc7b70ba1745e", + "version" : "1.23.0" + } + }, + { + "identity" : "swift-nio-http2", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-http2.git", + "state" : { + "revision" : "b5f7062b60e4add1e8c343ba4eb8da2e324b3a94", + "version" : "1.34.0" + } + }, + { + "identity" : "swift-nio-ssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-ssl.git", + "state" : { + "revision" : "a9fa5efd86e7ce2e5c1b6de113262e58035ca251", + "version" : "2.27.1" + } + }, + { + "identity" : "swift-nio-transport-services", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-transport-services.git", + "state" : { + "revision" : "38ac8221dd20674682148d6451367f89c2652980", + "version" : "1.21.0" + } + }, + { + "identity" : "swift-otel", + "kind" : "remoteSourceControl", + "location" : "https://github.com/slashmo/swift-otel.git", + "state" : { + "revision" : "8c271c7fed34a39f29c728598b3358fbdddf8ff4", + "version" : "0.9.0" + } + }, + { + "identity" : "swift-protobuf", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-protobuf.git", + "state" : { + "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106", + "version" : "1.27.1" + } + }, + { + "identity" : "swift-service-context", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-service-context.git", + "state" : { + "revision" : "0c62c5b4601d6c125050b5c3a97f20cce881d32b", + "version" : "1.1.0" + } + }, + { + "identity" : "swift-service-lifecycle", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-server/swift-service-lifecycle.git", + "state" : { + "revision" : "24c800fb494fbee6e42bc156dc94232dc08971af", + "version" : "2.6.1" + } + }, + { + "identity" : "swift-system", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-system.git", + "state" : { + "revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5", + "version" : "1.3.2" + } + }, { "identity" : "tuples.swift", "kind" : "remoteSourceControl", diff --git a/Node/Package.swift b/Node/Package.swift index 8e39bddd..99f96c09 100644 --- a/Node/Package.swift +++ b/Node/Package.swift @@ -18,6 +18,7 @@ let package = Package( dependencies: [ .package(path: "../Utils"), .package(path: "../Blockchain"), + .package(path: "../TracingUtils"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. @@ -26,6 +27,7 @@ let package = Package( name: "Node", dependencies: [ "Utils", "Blockchain", + "TracingUtils", ] ), .testTarget( diff --git a/Node/Sources/Node/Node.swift b/Node/Sources/Node/Node.swift index aac58c98..60872856 100644 --- a/Node/Sources/Node/Node.swift +++ b/Node/Sources/Node/Node.swift @@ -1,15 +1,20 @@ import Blockchain +import TracingUtils + +let logger = Logger(label: "node") public class Node { public private(set) var blockchain: Blockchain public init(genesis: Genesis, config: ProtocolConfigRef) async throws { + logger.debug("Initializing node") + let genesisState = try genesis.toState(config: config) let dataProvider = await InMemoryDataProvider(genesis: genesisState) blockchain = await Blockchain(config: config, dataProvider: dataProvider) } public func sayHello() { - print("Hello, World!") + logger.info("Hello, World!") } } diff --git a/TracingUtils/.gitignore b/TracingUtils/.gitignore new file mode 100644 index 00000000..0023a534 --- /dev/null +++ b/TracingUtils/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/TracingUtils/Package.resolved b/TracingUtils/Package.resolved new file mode 100644 index 00000000..a75092e4 --- /dev/null +++ b/TracingUtils/Package.resolved @@ -0,0 +1,42 @@ +{ + "originHash" : "dafdfe8c8a201d8118f12ef271e841774867a4498bc06527ccf8cea1b522a154", + "pins" : [ + { + "identity" : "swift-distributed-tracing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-distributed-tracing.git", + "state" : { + "revision" : "11c756c5c4d7de0eeed8595695cadd7fa107aa19", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-log", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-log.git", + "state" : { + "revision" : "9cb486020ebf03bfa5b5df985387a14a98744537", + "version" : "1.6.1" + } + }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, + { + "identity" : "swift-service-context", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-service-context.git", + "state" : { + "revision" : "0c62c5b4601d6c125050b5c3a97f20cce881d32b", + "version" : "1.1.0" + } + } + ], + "version" : 3 +} diff --git a/TracingUtils/Package.swift b/TracingUtils/Package.swift new file mode 100644 index 00000000..3c6f123a --- /dev/null +++ b/TracingUtils/Package.swift @@ -0,0 +1,35 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "TracingUtils", + platforms: [ + .macOS(.v14), + ], + products: [ + // Products define the executables and libraries a package produces, making them visible to other packages. + .library( + name: "TracingUtils", + targets: ["TracingUtils"] + ), + ], + dependencies: [ + .package(url: "https://github.com/apple/swift-log.git", from: "1.6.0"), + .package(url: "https://github.com/apple/swift-metrics.git", from: "2.5.0"), + .package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "1.1.0"), + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .target( + name: "TracingUtils", + dependencies: [ + .product(name: "Logging", package: "swift-log"), + .product(name: "Metrics", package: "swift-metrics"), + .product(name: "Tracing", package: "swift-distributed-tracing"), + ] + ), + ] +) diff --git a/TracingUtils/Sources/TracingUtils/Tracing.swift b/TracingUtils/Sources/TracingUtils/Tracing.swift new file mode 100644 index 00000000..77648802 --- /dev/null +++ b/TracingUtils/Sources/TracingUtils/Tracing.swift @@ -0,0 +1,3 @@ +@_exported import Logging +@_exported import Metrics +@_exported import Tracing diff --git a/boka.xcodeproj/project.pbxproj b/boka.xcodeproj/project.pbxproj index 18ff0122..edef6433 100644 --- a/boka.xcodeproj/project.pbxproj +++ b/boka.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 066C926E2C095D67005DDE4F /* Boka */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Boka; sourceTree = ""; }; 066C926F2C095D76005DDE4F /* Blockchain */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Blockchain; sourceTree = ""; }; 066C92702C095D85005DDE4F /* Node */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Node; sourceTree = ""; }; + 0674602A2C61BF63008FD993 /* TracingUtils */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = TracingUtils; sourceTree = ""; }; 06AAEF0C2C47746C0064995D /* PolkaVM */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PolkaVM; sourceTree = ""; }; 06F2335F2C0B306000A5E2E0 /* Database */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Database; sourceTree = ""; }; 06F233602C0C69F100A5E2E0 /* Utils */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Utils; sourceTree = ""; }; @@ -20,6 +21,7 @@ 066C92652C095CE5005DDE4F = { isa = PBXGroup; children = ( + 0674602A2C61BF63008FD993 /* TracingUtils */, 06AAEF0C2C47746C0064995D /* PolkaVM */, 06F233602C0C69F100A5E2E0 /* Utils */, 06F2335F2C0B306000A5E2E0 /* Database */, diff --git a/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 21499750..eb6cb751 100644 --- a/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "c70167db8b9aae420525a783ce4b7c6139e70ed2b5033d12314879a65299c15d", + "originHash" : "ec983f6f6ba2efba7032a1dd699e91f2ddb7f9aa70c6d2285cd63103b6445a5c", "pins" : [ { "identity" : "blake2.swift", @@ -10,6 +10,15 @@ "version" : "0.2.0" } }, + { + "identity" : "grpc-swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/grpc/grpc-swift.git", + "state" : { + "revision" : "6a90b7e77e29f9bda6c2b3a4165a40d6c02cfda1", + "version" : "1.23.0" + } + }, { "identity" : "scalecodec.swift", "kind" : "remoteSourceControl", @@ -28,6 +37,33 @@ "version" : "1.4.0" } }, + { + "identity" : "swift-async-algorithms", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-async-algorithms.git", + "state" : { + "revision" : "6ae9a051f76b81cc668305ceed5b0e0a7fd93d20", + "version" : "1.0.1" + } + }, + { + "identity" : "swift-atomics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-atomics.git", + "state" : { + "revision" : "cd142fd2f64be2100422d658e7411e39489da985", + "version" : "1.2.0" + } + }, + { + "identity" : "swift-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-collections.git", + "state" : { + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" + } + }, { "identity" : "swift-crypto", "kind" : "remoteSourceControl", @@ -37,6 +73,24 @@ "version" : "3.4.0" } }, + { + "identity" : "swift-distributed-tracing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-distributed-tracing.git", + "state" : { + "revision" : "11c756c5c4d7de0eeed8595695cadd7fa107aa19", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-http-types", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-http-types", + "state" : { + "revision" : "ae67c8178eb46944fd85e4dc6dd970e1f3ed6ccd", + "version" : "1.3.0" + } + }, { "identity" : "swift-log", "kind" : "remoteSourceControl", @@ -46,6 +100,96 @@ "version" : "1.6.1" } }, + { + "identity" : "swift-metrics", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-metrics.git", + "state" : { + "revision" : "e0165b53d49b413dd987526b641e05e246782685", + "version" : "2.5.0" + } + }, + { + "identity" : "swift-nio", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio.git", + "state" : { + "revision" : "4c4453b489cf76e6b3b0f300aba663eb78182fad", + "version" : "2.70.0" + } + }, + { + "identity" : "swift-nio-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-extras.git", + "state" : { + "revision" : "05c36b57453d23ea63785d58a7dbc7b70ba1745e", + "version" : "1.23.0" + } + }, + { + "identity" : "swift-nio-http2", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-http2.git", + "state" : { + "revision" : "b5f7062b60e4add1e8c343ba4eb8da2e324b3a94", + "version" : "1.34.0" + } + }, + { + "identity" : "swift-nio-ssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-ssl.git", + "state" : { + "revision" : "a9fa5efd86e7ce2e5c1b6de113262e58035ca251", + "version" : "2.27.1" + } + }, + { + "identity" : "swift-nio-transport-services", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-nio-transport-services.git", + "state" : { + "revision" : "38ac8221dd20674682148d6451367f89c2652980", + "version" : "1.21.0" + } + }, + { + "identity" : "swift-otel", + "kind" : "remoteSourceControl", + "location" : "https://github.com/slashmo/swift-otel.git", + "state" : { + "revision" : "8c271c7fed34a39f29c728598b3358fbdddf8ff4", + "version" : "0.9.0" + } + }, + { + "identity" : "swift-protobuf", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-protobuf.git", + "state" : { + "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106", + "version" : "1.27.1" + } + }, + { + "identity" : "swift-service-context", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-service-context.git", + "state" : { + "revision" : "0c62c5b4601d6c125050b5c3a97f20cce881d32b", + "version" : "1.1.0" + } + }, + { + "identity" : "swift-service-lifecycle", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swift-server/swift-service-lifecycle.git", + "state" : { + "revision" : "24c800fb494fbee6e42bc156dc94232dc08971af", + "version" : "2.6.1" + } + }, { "identity" : "swift-syntax", "kind" : "remoteSourceControl", @@ -55,6 +199,15 @@ "version" : "600.0.0-prerelease-2024-06-12" } }, + { + "identity" : "swift-system", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-system.git", + "state" : { + "revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5", + "version" : "1.3.2" + } + }, { "identity" : "swift-testing", "kind" : "remoteSourceControl", diff --git a/scripts/runTests.sh b/scripts/runTests.sh new file mode 100755 index 00000000..ebdfd1f3 --- /dev/null +++ b/scripts/runTests.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e + +COMMAND=$1 + +shift + +set -x + +for file in **/Tests; do + swift $COMMAND $@ --package-path "$(dirname "$file")"; +done