-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a temp project with Metric API (which will come eventually from r…
…untime) (#2033)
- Loading branch information
1 parent
9d415bf
commit c5a8535
Showing
15 changed files
with
1,024 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// <copyright file="MeterProviderBuilder.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// MeterProviderBuilder base class. | ||
/// </summary> | ||
public abstract class MeterProviderBuilder | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MeterProviderBuilder"/> class. | ||
/// </summary> | ||
protected MeterProviderBuilder() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Adds given meter source names to the list of subscribed sources. | ||
/// </summary> | ||
/// <param name="names">Meter source names.</param> | ||
/// <returns>Returns <see cref="MeterProviderBuilder"/> for chaining.</returns> | ||
public abstract MeterProviderBuilder AddSource(params string[] names); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// <copyright file="MeterProviderBuilderSdk.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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> | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
public class MeterProviderBuilderSdk : MeterProviderBuilder | ||
{ | ||
protected MeterProviderBuilderSdk() | ||
{ | ||
} | ||
|
||
public override MeterProviderBuilder AddSource(params string[] names) | ||
{ | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// <copyright file="ActivityExtensions.cs" company="OpenTelemetry Authors"> | ||
// Copyright The 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> | ||
|
||
#nullable enable | ||
|
||
namespace System.Diagnostics.Metrics | ||
{ | ||
public class Counter<T> : MeterInstrument<T> | ||
where T : unmanaged | ||
{ | ||
internal Counter(Meter meter, string name, string? description, string? unit) : | ||
base(meter, name, description, unit) | ||
{ | ||
Publish(); | ||
} | ||
|
||
public void Add(T measurement) => RecordMeasurement(measurement); | ||
public void Add(T measurement, | ||
(string LabelName, object LabelValue) label1) => RecordMeasurement(measurement, label1); | ||
public void Add(T measurement, | ||
(string LabelName, object LabelValue) label1, | ||
(string LabelName, object LabelValue) label2) => RecordMeasurement(measurement, label1, label2); | ||
public void Add(T measurement, | ||
(string LabelName, object LabelValue) label1, | ||
(string LabelName, object LabelValue) label2, | ||
(string LabelName, object LabelValue) label3) => RecordMeasurement(measurement, label1, label2, label3); | ||
public void Add(T measurement, params (string LabelName, object LabelValue)[] labels) => RecordMeasurement(measurement, labels); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace System.Diagnostics.Metrics | ||
{ | ||
public class CounterFunc<T> : ObservableMeterInstrument<T> where T : unmanaged | ||
{ | ||
// This is either a Func<T> or an Func<IEnumerable<Measurement<T>>> | ||
object _observeValueFunc; | ||
|
||
public CounterFunc(Meter meter, string name, Func<T> observeValue, string? description, string? unit) : | ||
base(meter, name, description, unit) | ||
{ | ||
_observeValueFunc = observeValue; | ||
Publish(); | ||
} | ||
|
||
public CounterFunc(Meter meter, string name, Func<IEnumerable<Measurement<T>>> observeValues, string? description, string? unit) : | ||
base(meter, name, description, unit) | ||
{ | ||
_observeValueFunc = observeValues; | ||
Publish(); | ||
} | ||
|
||
protected override IEnumerable<Measurement<T>> Observe() | ||
{ | ||
if (_observeValueFunc is Func<T>) | ||
{ | ||
T value = ((Func<T>)_observeValueFunc)(); | ||
return new Measurement<T>[] { new Measurement<T>(value) }; | ||
} | ||
else | ||
{ | ||
return ((Func<IEnumerable<Measurement<T>>>)_observeValueFunc)(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Diagnostics.Metrics | ||
{ | ||
public class Distribution<T> : MeterInstrument<T> where T : unmanaged | ||
{ | ||
internal Distribution(Meter meter, string name, string? description, string? unit) | ||
: base(meter, name, description, unit) | ||
{ | ||
Publish(); | ||
} | ||
|
||
public void Record(T measurement) => RecordMeasurement(measurement); | ||
public void Record(T measurement, | ||
(string LabelName, object LabelValue) label1) => RecordMeasurement(measurement, label1); | ||
public void Record(T measurement, | ||
(string LabelName, object LabelValue) label1, | ||
(string LabelName, object LabelValue) label2) => RecordMeasurement(measurement, label1, label2); | ||
public void Record(T measurement, | ||
(string LabelName, object LabelValue) label1, | ||
(string LabelName, object LabelValue) label2, | ||
(string LabelName, object LabelValue) label3) => RecordMeasurement(measurement, label1, label2, label3); | ||
public void Record(T measurement, params (string LabelName, object LabelValue)[] labels) => RecordMeasurement(measurement, labels); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace System.Diagnostics.Metrics | ||
{ | ||
public class Gauge<T> : MeterInstrument<T> where T : unmanaged | ||
{ | ||
internal Gauge(Meter meter, string name, string? description, string? unit) : | ||
base(meter, name, description, unit) | ||
{ | ||
Publish(); | ||
} | ||
|
||
public void Set(T val) | ||
{ | ||
RecordMeasurement(val); | ||
} | ||
|
||
public void Set(T val, params (string LabelName, object LabelValue)[] labels) | ||
{ | ||
RecordMeasurement(val, labels); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace System.Diagnostics.Metrics | ||
{ | ||
public class Meter : IDisposable | ||
{ | ||
List<MeterInstrument>? _instruments = new List<MeterInstrument>(); | ||
|
||
public Meter(string name) : this(name, "") { } | ||
|
||
public Meter(string name, string version) | ||
{ | ||
Name = name; | ||
Version = version; | ||
lock (MeterInstrumentCollection.Lock) | ||
{ | ||
MeterInstrumentCollection.Instance.AddMeter(this); | ||
} | ||
} | ||
|
||
public Counter<T> CreateCounter<T>(string name, string? description = null, string? unit = null) where T : unmanaged | ||
{ | ||
return new Counter<T>(this, name, description, unit); | ||
} | ||
|
||
public CounterFunc<T> CreateCounterFunc<T>(string name, Func<T> observeValue, string? description = null, string? unit = null) where T : unmanaged | ||
{ | ||
return new CounterFunc<T>(this, name, observeValue, description, unit); | ||
} | ||
|
||
public CounterFunc<T> CreateCounterFunc<T>(string name, Func<IEnumerable<Measurement<T>>> observeValues, string? description = null, string? unit = null) where T : unmanaged | ||
{ | ||
return new CounterFunc<T>(this, name, observeValues, description, unit); | ||
} | ||
|
||
public Gauge<T> CreateGauge<T>(string name, string? description = null, string? unit = null) where T : unmanaged | ||
{ | ||
return new Gauge<T>(this, name, description, unit); | ||
} | ||
|
||
public Distribution<T> CreateDistribution<T>(string name, string? description = null, string? unit = null) where T : unmanaged | ||
{ | ||
return new Distribution<T>(this, name, description, unit); | ||
} | ||
|
||
public string Name { get; } | ||
public string Version { get; } | ||
|
||
|
||
internal void PublishInstrument(MeterInstrument instrument) | ||
{ | ||
lock (MeterInstrumentCollection.Lock) | ||
{ | ||
if (_instruments != null) // if not disposed | ||
{ | ||
_instruments.Add(instrument); | ||
MeterInstrumentCollection.Instance.PublishInstrument(instrument); | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
lock (MeterInstrumentCollection.Lock) | ||
{ | ||
MeterInstrumentCollection.Instance.RemoveMeter(this); | ||
_instruments = null; | ||
} | ||
} | ||
|
||
internal IEnumerable<MeterInstrument> Instruments => | ||
#if NET452 | ||
(IEnumerable<MeterInstrument>?)_instruments ?? new MeterInstrument[0]; | ||
#else | ||
(IEnumerable<MeterInstrument>?)_instruments ?? Array.Empty<MeterInstrument>(); | ||
#endif | ||
} | ||
} |
Oops, something went wrong.