Skip to content

Commit

Permalink
Add Info
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLotU committed Nov 18, 2018
1 parent c5b6fd8 commit c654c5a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 65 deletions.
31 changes: 31 additions & 0 deletions Sources/Prometheus/MetricTypes/Info.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
internal let prometheus: Prometheus

public let name: String
public let help: String?

public let _type: MetricType = .info

internal var labels = Labels()

internal init(_ name: String, _ help: String? = nil, _ p: Prometheus) {
self.name = name
self.help = help
self.prometheus = p
}

public func info(_ labels: Labels) {
self.labels = labels
}

public func getMetric() -> String {
var output = [String]()

output.append(headers)

let labelsString = encodeLabels(labels)
output.append("\(name)\(labelsString) 1.0")

return output.joined(separator: "\n")
}
}
41 changes: 40 additions & 1 deletion Sources/Prometheus/Prometheus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class Prometheus {
/// - name: Name of the histogram
/// - helpText: Help text for the histogram. Usually a short description
/// - buckets: Buckets to divide values over
/// - labels: Labels to give this Histogram. Can be left out to default to no labels
/// - labels: Labels to give this histogram. Can be left out to default to no labels
///
/// - Returns: Histogram instance
public func createHistogram<T: Numeric, U: HistogramLabels>(
Expand Down Expand Up @@ -136,6 +136,16 @@ public class Prometheus {

// MARK: - Summary

/// Creates a summary with the given values
///
/// - Parameters:
/// - type: The type the summary will observe
/// - name: Name of the summary
/// - helpText: Help text for the summary. Usually a short description
/// - quantiles: Quantiles to caluculate
/// - labels: Labels to give this summary. Can be left out to default to no labels
///
/// - Returns: Summary instance
public func createSummary<T: Numeric, U: SummaryLabels>(
forType type: T.Type,
named name: String,
Expand All @@ -148,6 +158,15 @@ public class Prometheus {
return summary
}

/// Creates a summary with the given values
///
/// - Parameters:
/// - type: The type the summary will observe
/// - name: Name of the summary
/// - helpText: Help text for the summary. Usually a short description
/// - quantiles: Quantiles to caluculate
///
/// - Returns: Summary instance
public func createSummary<T: Numeric>(
forType type: T.Type,
named name: String,
Expand All @@ -157,6 +176,26 @@ public class Prometheus {
return self.createSummary(forType: type, named: name, helpText: helpText, quantiles: quantiles, labels: EmptySummaryCodable.self)
}

// MARK: - Info

/// Creates an Info metric with the given values
///
/// - Parameters
/// - name: Name of the info
/// - helpText: Help text for the info. Usually a short description
/// - labelType: Type of labels this Info can use
///
/// - Returns Info instance
public func createInfo<U: MetricLabels>(
named name: String,
helpText: String? = nil,
labelType: U.Type) -> Info<U>
{
let info = Info<U>(name, helpText, self)
self.metrics.append(info)
return info
}

/// Creates prometheus formatted metrics
///
/// - Returns: Newline seperated string with metrics for all Metric Trackers of this Prometheus instance
Expand Down
147 changes: 83 additions & 64 deletions Sources/PrometheusExample/main.swift
Original file line number Diff line number Diff line change
@@ -1,68 +1,87 @@
import Prometheus

struct MyCodable: MetricLabels {
var thing: String = "*"
}

let codable1 = MyCodable(thing: "Thing1")
let codable2 = MyCodable(thing: "Thing2")

let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)

counter.inc(5)
counter.inc(Int.random(in: 0...100), codable2)
counter.inc(Int.random(in: 0...100), codable1)

let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)

gauge.inc(100)
gauge.inc(Int.random(in: 0...100), codable2)
gauge.inc(Int.random(in: 0...100), codable1)

struct HistogramThing: HistogramLabels {
var le: String = ""
let route: String

init() {
self.route = "*"
}

init(_ route: String) {
self.route = route
}
}

let histogram = Prometheus.shared.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)

for _ in 0...Int.random(in: 10...50) {
histogram.observe(Double.random(in: 0...1))
}

for _ in 0...Int.random(in: 10...50) {
histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
}

struct SummaryThing: SummaryLabels {
var quantile: String = ""
let route: String

init() {
self.route = "*"
}

init(_ route: String) {
self.route = route
}
}

let summary = Prometheus.shared.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)

for _ in 0...Int.random(in: 100...1000) {
summary.observe(Double.random(in: 0...10000))
}

for _ in 0...Int.random(in: 100...1000) {
summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
}
//struct MyCodable: MetricLabels {
// var thing: String = "*"
//}
//
//let codable1 = MyCodable(thing: "Thing1")
//let codable2 = MyCodable(thing: "Thing2")
//
//let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
//
//counter.inc(5)
//counter.inc(Int.random(in: 0...100), codable2)
//counter.inc(Int.random(in: 0...100), codable1)
//
//let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
//
//gauge.inc(100)
//gauge.inc(Int.random(in: 0...100), codable2)
//gauge.inc(Int.random(in: 0...100), codable1)
//
//struct HistogramThing: HistogramLabels {
// var le: String = ""
// let route: String
//
// init() {
// self.route = "*"
// }
//
// init(_ route: String) {
// self.route = route
// }
//}
//
//let histogram = Prometheus.shared.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
//
//for _ in 0...Int.random(in: 10...50) {
// histogram.observe(Double.random(in: 0...1))
//}
//
//for _ in 0...Int.random(in: 10...50) {
// histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
//}
//
//struct SummaryThing: SummaryLabels {
// var quantile: String = ""
// let route: String
//
// init() {
// self.route = "*"
// }
//
// init(_ route: String) {
// self.route = route
// }
//}
//
//let summary = Prometheus.shared.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
//
//for _ in 0...Int.random(in: 100...1000) {
// summary.observe(Double.random(in: 0...10000))
//}
//
//for _ in 0...Int.random(in: 100...1000) {
// summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
//}

//struct MyInfoStruct: MetricLabels {
// let version: String
// let major: String
//
// init() {
// self.version = "1.0.0"
// self.major = "1"
// }
//
// init(_ v: String, _ m: String) {
// self.version = v
// self.major = m
// }
//}
//
//let info = Prometheus.shared.createInfo(named: "my_info", helpText: "Just some info", labelType: MyInfoStruct.self)
//
//info.info(MyInfoStruct("2.0.0", "2"))

print(Prometheus.shared.getMetrics())

0 comments on commit c654c5a

Please sign in to comment.