|
| 1 | +import Hummingbird |
| 2 | +import Logging |
| 3 | +import OTel |
| 4 | +import ServiceLifecycle |
| 5 | +import Tracing |
| 6 | +import Valkey |
| 7 | + |
| 8 | +@main |
| 9 | +struct Example { |
| 10 | + static func main() async throws { |
| 11 | + let observability = try bootstrapObservability() |
| 12 | + let logger = Logger(label: "example") |
| 13 | + |
| 14 | + let valkeyClient = ValkeyClient( |
| 15 | + .hostname("localhost"), |
| 16 | + logger: logger |
| 17 | + ) |
| 18 | + |
| 19 | + let router = Router() |
| 20 | + router.add(middleware: TracingMiddleware()) |
| 21 | + router.add(middleware: LogRequestsMiddleware(.info)) |
| 22 | + |
| 23 | + router.get("/:x") { _, context in |
| 24 | + /* |
| 25 | + This demonstrates the span created for pipelined commands where all commands are of the same type. |
| 26 | + The `db.operation.name` indicates that it's multiple `EVAL` commands, |
| 27 | + and `db.operation.batch.size` indicates the number of commands. |
| 28 | + */ |
| 29 | + _ = await valkeyClient.execute( |
| 30 | + EVAL(script: "return '1'"), |
| 31 | + EVAL(script: "return '2'"), |
| 32 | + EVAL(script: "return '3'") |
| 33 | + ) |
| 34 | + |
| 35 | + /* |
| 36 | + This demonstrates the span created for pipelined commands where the commands are of different types. |
| 37 | + The `db.operation.name` resorts to `MULTI`, and `db.operation.batch.size` indicates the number of commands. |
| 38 | + */ |
| 39 | + _ = await valkeyClient.execute( |
| 40 | + EVAL(script: "return '1'"), |
| 41 | + ACL.WHOAMI() |
| 42 | + ) |
| 43 | + |
| 44 | + // This demonstrates the span created for a failed command. |
| 45 | + _ = try? await valkeyClient.execute(EVAL(script: "💩")) |
| 46 | + |
| 47 | + // This demonstrates the span created for a failed pipelined command. |
| 48 | + _ = await valkeyClient.execute( |
| 49 | + EVAL(script: "return 'ok'"), |
| 50 | + EVAL(script: "💩") |
| 51 | + ) |
| 52 | + |
| 53 | + let x = try context.parameters.require("x", as: Int.self) |
| 54 | + |
| 55 | + func expensiveAlgorithm(_ x: Int) async throws -> Int { |
| 56 | + try await withSpan("compute") { span in |
| 57 | + span.attributes["input"] = x |
| 58 | + try await Task.sleep(for: .seconds(3)) |
| 59 | + return x * 2 |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if let cachedResult = try await valkeyClient.hget("values", field: "\(x)") { |
| 64 | + return cachedResult |
| 65 | + } |
| 66 | + |
| 67 | + let result = try await expensiveAlgorithm(x) |
| 68 | + |
| 69 | + try await valkeyClient.hset("values", data: [.init(field: "\(x)", value: "\(result)")]) |
| 70 | + |
| 71 | + return ByteBuffer(string: "\(result)") |
| 72 | + } |
| 73 | + |
| 74 | + var app = Application(router: router) |
| 75 | + app.addServices(observability) |
| 76 | + app.addServices(valkeyClient) |
| 77 | + |
| 78 | + try await app.runService() |
| 79 | + } |
| 80 | + |
| 81 | + private static func bootstrapObservability() throws -> some Service { |
| 82 | + LoggingSystem.bootstrap( |
| 83 | + StreamLogHandler.standardOutput(label:metadataProvider:), |
| 84 | + metadataProvider: OTel.makeLoggingMetadataProvider() |
| 85 | + ) |
| 86 | + |
| 87 | + var configuration = OTel.Configuration.default |
| 88 | + configuration.serviceName = "example" |
| 89 | + |
| 90 | + // For now, valkey-swift only supports Distributed Tracing so we disable the other signals. |
| 91 | + configuration.logs.enabled = false |
| 92 | + configuration.metrics.enabled = false |
| 93 | + |
| 94 | + return try OTel.bootstrap(configuration: configuration) |
| 95 | + } |
| 96 | +} |
0 commit comments