Skip to content

Commit

Permalink
Add function to write metric list to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Oct 24, 2023
1 parent 4f3d4d9 commit 9cfe2f4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Sources/Clairvoyant/Metric/MetricObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,29 @@ public final class MetricObserver {
}
return result
}

/**
Save the info of all currently registered metrics to disk, in a human-readable format.

This can make it easier to determine which log files are available and which belong to which metrics.
- Returns: `true`, if the file was written.
*/
@discardableResult
public func saveCurrentListOfMetricsToLogFolder() -> Bool {
let list = observedMetrics.values
.map { $0.info }
.sorted { $0.id }
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]

let url = logFolder.appendingPathComponent("metrics.json")
do {
let data = try encoder.encode(list)
try data.write(to: url)
return true
} catch {
print("Failed to save metric list: \(error)")
return false
}
}
}

0 comments on commit 9cfe2f4

Please sign in to comment.