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