Skip to content

Commit

Permalink
Removing more generics for #392 (#560)
Browse files Browse the repository at this point in the history
* Removing more generics for #392

* Fixing the formatting issue

* Removed Unsafe everywhere

* Fixing method order and file name in the header

Co-authored-by: Sergey Kanzhelev <S.Kanzhelev@live.com>
  • Loading branch information
alexeyzimarev and SergeyKanzhelev authored Apr 15, 2020
1 parent bc684ff commit 48dbba3
Show file tree
Hide file tree
Showing 32 changed files with 935 additions and 610 deletions.
94 changes: 0 additions & 94 deletions src/OpenTelemetry/Metrics/Aggregators/CounterSumAggregator.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// <copyright file="DoubleCounterSumAggregator.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry 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.
// </copyright>

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using OpenTelemetry.Metrics.Export;

namespace OpenTelemetry.Metrics.Aggregators
{
/// <summary>
/// Basic aggregator which calculates a Sum from individual measurements.
/// </summary>
public class DoubleCounterSumAggregator : Aggregator<double>
{
private double sum;
private double checkPoint;

public override void Checkpoint()
{
// checkpoints the current running sum into checkpoint, and starts counting again.
this.checkPoint = Interlocked.Exchange(ref this.sum, 0.0);
}

public override MetricData<double> ToMetricData()
{
return new SumData<double>
{
Sum = this.checkPoint,
Timestamp = DateTime.UtcNow,
};
}

public override AggregationType GetAggregationType()
{
return AggregationType.DoubleSum;
}

public override void Update(double value)
{
// Adds value to the running total in a thread safe manner.
double initialTotal, computedTotal;
do
{
initialTotal = this.sum;
computedTotal = initialTotal + value;
}
while (initialTotal != Interlocked.CompareExchange(ref this.sum, computedTotal, initialTotal));
}
}
}
55 changes: 55 additions & 0 deletions src/OpenTelemetry/Metrics/Aggregators/DoubleLastValueAggregator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// <copyright file="DoubleLastValueAggregator.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry 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.
// </copyright>

using System;
using System.Threading;
using OpenTelemetry.Metrics.Export;

namespace OpenTelemetry.Metrics.Aggregators
{
/// <summary>
/// Simple aggregator that only keeps the last value.
/// </summary>
public class DoubleLastValueAggregator : Aggregator<double>
{
private double value;
private double checkpoint;

public override void Checkpoint()
{
Interlocked.Exchange(ref this.checkpoint, this.value);
}

public override MetricData<double> ToMetricData()
{
return new SumData<double>
{
Sum = this.checkpoint,
Timestamp = DateTime.UtcNow,
};
}

public override AggregationType GetAggregationType()
{
return AggregationType.DoubleSum;
}

public override void Update(double newValue)
{
Interlocked.Exchange(ref this.value, newValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// <copyright file="DoubleMeasureMinMaxSumCountAggregator.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry 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.
// </copyright>

using System;
using System.Threading;
using OpenTelemetry.Metrics.Export;

namespace OpenTelemetry.Metrics.Aggregators
{
/// <summary>
/// Aggregator which calculates summary (Min,Max,Sum,Count) from measures.
/// </summary>
public class DoubleMeasureMinMaxSumCountAggregator : Aggregator<double>
{
private DoubleSummary summary = new DoubleSummary();
private DoubleSummary checkPoint = new DoubleSummary();
private object updateLock = new object();

public override void Checkpoint()
{
this.checkPoint = Interlocked.Exchange(ref this.summary, new DoubleSummary());
}

public override AggregationType GetAggregationType()
{
return AggregationType.Summary;
}

public override MetricData<double> ToMetricData()
{
return new SummaryData<double>
{
Count = this.checkPoint.Count,
Sum = this.checkPoint.Sum,
Min = this.checkPoint.Min,
Max = this.checkPoint.Max,
Timestamp = DateTime.UtcNow,
};
}

public override void Update(double value)
{
lock (this.updateLock)
{
this.summary.Count++;
this.summary.Sum += value;
this.summary.Max = Math.Max(this.summary.Max, value);
this.summary.Min = Math.Min(this.summary.Min, value);
}
}

private class DoubleSummary
{
public long Count;
public double Min;
public double Max;
public double Sum;

public DoubleSummary()
{
this.Min = double.MaxValue;
this.Max = double.MinValue;
}
}
}
}
58 changes: 58 additions & 0 deletions src/OpenTelemetry/Metrics/Aggregators/Int64CounterSumAggregator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <copyright file="Int64CounterSumAggregator.cs" company="OpenTelemetry Authors">
// Copyright 2018, OpenTelemetry 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.
// </copyright>

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using OpenTelemetry.Metrics.Export;

namespace OpenTelemetry.Metrics.Aggregators
{
/// <summary>
/// Basic aggregator which calculates a Sum from individual measurements.
/// </summary>
public class Int64CounterSumAggregator : Aggregator<long>
{
private long sum;
private long checkPoint;

public override void Checkpoint()
{
// checkpoints the current running sum into checkpoint, and starts counting again.
this.checkPoint = Interlocked.Exchange(ref this.sum, 0);
}

public override MetricData<long> ToMetricData()
{
return new SumData<long>
{
Sum = this.checkPoint,
Timestamp = DateTime.UtcNow,
};
}

public override AggregationType GetAggregationType()
{
return AggregationType.LongSum;
}

public override void Update(long value)
{
// Adds value to the running total in a thread safe manner.
Interlocked.Add(ref this.sum, value);
}
}
}
Loading

0 comments on commit 48dbba3

Please sign in to comment.