|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Distributed Tracing open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project |
| 6 | +// authors |
| 7 | +// Licensed under Apache License v2.0 |
| 8 | +// |
| 9 | +// See LICENSE.txt for license information |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | +// |
| 16 | +// This source file is part of the Swift OpenTelemetry open source project |
| 17 | +// |
| 18 | +// Copyright (c) 2021 Moritz Lang and the Swift OpenTelemetry project authors |
| 19 | +// Licensed under Apache License v2.0 |
| 20 | +// |
| 21 | +// See LICENSE.txt for license information |
| 22 | +// |
| 23 | +// SPDX-License-Identifier: Apache-2.0 |
| 24 | +// |
| 25 | +//===----------------------------------------------------------------------===// |
| 26 | + |
| 27 | + |
| 28 | +import Tracing |
| 29 | + |
| 30 | +func makeDinner() async throws -> Meal { |
| 31 | + try await InstrumentationSystem.tracer.withSpan("makeDinner") { _ in |
| 32 | + await sleep(for: .milliseconds(200)) |
| 33 | + |
| 34 | + async let veggies = try chopVegetables() |
| 35 | + async let meat = marinateMeat() |
| 36 | + async let oven = preheatOven(temperature: 350) |
| 37 | + // ... |
| 38 | + return try await cook(veggies, meat, oven) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func chopVegetables() async throws -> [Vegetable] { |
| 43 | + try await otelChopping1.tracer().withSpan("chopVegetables") { _ in |
| 44 | + // Chop the vegetables...! |
| 45 | + // |
| 46 | + // However, since chopping is a very difficult operation, |
| 47 | + // one chopping task can be performed at the same time on a single service! |
| 48 | + // (Imagine that... we cannot parallelize these two tasks, and need to involve another service). |
| 49 | + async let carrot = try chop(.carrot, tracer: otelChopping1.tracer()) |
| 50 | + async let potato = try chop(.potato, tracer: otelChopping2.tracer()) |
| 51 | + return try await [carrot, potato] |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func chop(_ vegetable: Vegetable, tracer: any Tracer) async throws -> Vegetable { |
| 56 | + await tracer.withSpan("chop-\(vegetable)") { _ in |
| 57 | + await sleep(for: .seconds(5)) |
| 58 | + // ... |
| 59 | + return vegetable // "chopped" |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func marinateMeat() async -> Meat { |
| 64 | + await sleep(for: .milliseconds(620)) |
| 65 | + |
| 66 | + return await InstrumentationSystem.tracer.withSpan("marinateMeat") { _ in |
| 67 | + await sleep(for: .seconds(3)) |
| 68 | + // ... |
| 69 | + return Meat() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func preheatOven(temperature: Int) async -> Oven { |
| 74 | + await InstrumentationSystem.tracer.withSpan("preheatOven") { _ in |
| 75 | + // ... |
| 76 | + await sleep(for: .seconds(6)) |
| 77 | + return Oven() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func cook(_: Any, _: Any, _: Any) async -> Meal { |
| 82 | + await InstrumentationSystem.tracer.withSpan("cook") { span in |
| 83 | + span.addEvent("children-asking-if-done-already") |
| 84 | + await sleep(for: .seconds(3)) |
| 85 | + span.addEvent("children-asking-if-done-already-again") |
| 86 | + await sleep(for: .seconds(2)) |
| 87 | + // ... |
| 88 | + return Meal() |
| 89 | + } |
| 90 | +} |
0 commit comments