A quick guide to using Telemetry.
A Telemeter is used to record events anywhere in your code. Creating one is simple:
let telemeter = StandardTelemeter()A telemeter will need to send data somewhere to be useful, this is where Relays come in. The ConsoleRelay will
output events with the specified Significance to the console. Let's add one that will output all metrons.
telemeter.add(relay: ConsoleRelay(allowedSignificances: .all)You'll define events that you record to a telemeter. An event you define must conform to the Metron protocol.
enum AddToCartEvent: Metron {
case itemAdded(Product)
case addFailed(Error)
var message: String {
switch self {
case .itemAdded(let product):
return "Added item to cart: \(product.name)"
case .addFailed:
return "Failed to add item to cart"
}
}
var facets: [Facet] {
switch self {
case .itemAdded: return [.informational]
case .addFailed(let error): return [Failure(error), .error]
}
}
}Recording an event is easy, just call .record on a Telemeter instance:
telemeter.record(AddToCartEvent.itemAdded(product))