Initial commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2018 The Cartographer Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CartographerSharp.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Counter metric for accumulating values.
|
||||
/// </summary>
|
||||
public abstract class Counter
|
||||
{
|
||||
/// <summary>
|
||||
/// Counter instance that does nothing. Safe for use in static initializers.
|
||||
/// </summary>
|
||||
public static Counter Null()
|
||||
{
|
||||
return new NullCounter();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increments the counter by 1.
|
||||
/// </summary>
|
||||
public abstract void Increment();
|
||||
|
||||
/// <summary>
|
||||
/// Increments the counter by the specified value.
|
||||
/// </summary>
|
||||
public abstract void Increment(double byValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Null implementation of counter that does nothing.
|
||||
/// </summary>
|
||||
internal class NullCounter : Counter
|
||||
{
|
||||
public override void Increment()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public override void Increment(double byValue)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2018 The Cartographer Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CartographerSharp.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Family of metrics with labels.
|
||||
/// </summary>
|
||||
public abstract class Family<TMetricType> where TMetricType : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Family instance that does nothing. Safe for use in static initializers.
|
||||
/// </summary>
|
||||
public static Family<TMetricType> Null()
|
||||
{
|
||||
return new NullFamily<TMetricType>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a metric instance with the specified labels.
|
||||
/// </summary>
|
||||
public abstract TMetricType Add(Dictionary<string, string> labels);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Null implementation of family that does nothing.
|
||||
/// </summary>
|
||||
internal class NullFamily<TMetricType> : Family<TMetricType> where TMetricType : class
|
||||
{
|
||||
public override TMetricType Add(Dictionary<string, string> labels)
|
||||
{
|
||||
// Return null metric instance
|
||||
if (typeof(TMetricType) == typeof(Counter))
|
||||
return (TMetricType)(object)Counter.Null();
|
||||
if (typeof(TMetricType) == typeof(Gauge))
|
||||
return (TMetricType)(object)Gauge.Null();
|
||||
if (typeof(TMetricType) == typeof(Histogram))
|
||||
return (TMetricType)(object)Histogram.Null();
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Factory for creating metric families.
|
||||
/// </summary>
|
||||
public abstract class FamilyFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new counter family.
|
||||
/// </summary>
|
||||
public abstract Family<Counter> NewCounterFamily(string name, string description);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new gauge family.
|
||||
/// </summary>
|
||||
public abstract Family<Gauge> NewGaugeFamily(string name, string description);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new histogram family.
|
||||
/// </summary>
|
||||
public abstract Family<Histogram> NewHistogramFamily(
|
||||
string name,
|
||||
string description,
|
||||
Histogram.BucketBoundaries boundaries);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2018 The Cartographer Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CartographerSharp.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Gauge metric for tracking values that can go up and down.
|
||||
/// </summary>
|
||||
public abstract class Gauge
|
||||
{
|
||||
/// <summary>
|
||||
/// Gauge instance that does nothing. Safe for use in static initializers.
|
||||
/// </summary>
|
||||
public static Gauge Null()
|
||||
{
|
||||
return new NullGauge();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increments the gauge by 1.
|
||||
/// </summary>
|
||||
public abstract void Increment();
|
||||
|
||||
/// <summary>
|
||||
/// Increments the gauge by the specified value.
|
||||
/// </summary>
|
||||
public abstract void Increment(double byValue);
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the gauge by 1.
|
||||
/// </summary>
|
||||
public abstract void Decrement();
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the gauge by the specified value.
|
||||
/// </summary>
|
||||
public abstract void Decrement(double byValue);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the gauge to the specified value.
|
||||
/// </summary>
|
||||
public abstract void Set(double value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Null implementation of gauge that does nothing.
|
||||
/// </summary>
|
||||
internal class NullGauge : Gauge
|
||||
{
|
||||
public override void Increment()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public override void Increment(double byValue)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public override void Decrement()
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public override void Decrement(double byValue)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public override void Set(double value)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2018 The Cartographer Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CartographerSharp.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Histogram metric for tracking value distributions.
|
||||
/// </summary>
|
||||
public abstract class Histogram
|
||||
{
|
||||
/// <summary>
|
||||
/// Bucket boundaries for histogram.
|
||||
/// </summary>
|
||||
public class BucketBoundaries : List<double>
|
||||
{
|
||||
public BucketBoundaries() : base() { }
|
||||
public BucketBoundaries(IEnumerable<double> collection) : base(collection) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Histogram instance that does nothing. Safe for use in static initializers.
|
||||
/// </summary>
|
||||
public static Histogram Null()
|
||||
{
|
||||
return new NullHistogram();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates fixed-width bucket boundaries.
|
||||
/// </summary>
|
||||
public static BucketBoundaries FixedWidth(double width, int numFiniteBuckets)
|
||||
{
|
||||
var result = new BucketBoundaries();
|
||||
double boundary = 0;
|
||||
for (int i = 0; i < numFiniteBuckets; i++)
|
||||
{
|
||||
boundary += width;
|
||||
result.Add(boundary);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates scaled powers-of bucket boundaries.
|
||||
/// </summary>
|
||||
public static BucketBoundaries ScaledPowersOf(double baseValue, double scaleFactor, double maxValue)
|
||||
{
|
||||
if (baseValue <= 1)
|
||||
throw new ArgumentException("Base must be greater than 1", nameof(baseValue));
|
||||
if (scaleFactor <= 0)
|
||||
throw new ArgumentException("Scale factor must be greater than 0", nameof(scaleFactor));
|
||||
|
||||
var result = new BucketBoundaries();
|
||||
double boundary = scaleFactor;
|
||||
while (boundary < maxValue)
|
||||
{
|
||||
result.Add(boundary);
|
||||
boundary *= baseValue;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Observes a value in the histogram.
|
||||
/// </summary>
|
||||
public abstract void Observe(double value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Null implementation of histogram that does nothing.
|
||||
/// </summary>
|
||||
internal class NullHistogram : Histogram
|
||||
{
|
||||
public override void Observe(double value)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user