Skip to content

Commit

Permalink
Add a temp project with Metric API (which will come eventually from r…
Browse files Browse the repository at this point in the history
…untime) (#2033)
  • Loading branch information
cijothomas authored May 4, 2021
1 parent 9d415bf commit c5a8535
Show file tree
Hide file tree
Showing 15 changed files with 1,024 additions and 0 deletions.
6 changes: 6 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "exception-reporting", "docs
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "customizing-the-sdk", "docs\trace\customizing-the-sdk\customizing-the-sdk.csproj", "{64E3D8BB-93AB-4571-93F7-ED8D64DFFD06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Diagnostics.Metrics.Temp", "src\System.Diagnostics.Metrics.Temp\System.Diagnostics.Metrics.Temp.csproj", "{4481390E-52F5-46A6-8510-3B1433ABAFE6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -405,6 +407,10 @@ Global
{64E3D8BB-93AB-4571-93F7-ED8D64DFFD06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64E3D8BB-93AB-4571-93F7-ED8D64DFFD06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64E3D8BB-93AB-4571-93F7-ED8D64DFFD06}.Release|Any CPU.Build.0 = Release|Any CPU
{4481390E-52F5-46A6-8510-3B1433ABAFE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4481390E-52F5-46A6-8510-3B1433ABAFE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4481390E-52F5-46A6-8510-3B1433ABAFE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4481390E-52F5-46A6-8510-3B1433ABAFE6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
39 changes: 39 additions & 0 deletions src/OpenTelemetry.Api/Metrics/MeterProviderBuilder.cs
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);
}
}
30 changes: 30 additions & 0 deletions src/OpenTelemetry/Metrics/MeterProviderBuilderSdk.cs
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;
}
}
}
1 change: 1 addition & 0 deletions src/OpenTelemetry/OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj" />
<ProjectReference Include="$(RepoRoot)\src\System.Diagnostics.Metrics.Temp\System.Diagnostics.Metrics.Temp.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
42 changes: 42 additions & 0 deletions src/System.Diagnostics.Metrics.Temp/Counter.cs
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);
}
}
43 changes: 43 additions & 0 deletions src/System.Diagnostics.Metrics.Temp/CounterFunc.cs
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)();
}
}
}
}
29 changes: 29 additions & 0 deletions src/System.Diagnostics.Metrics.Temp/Distribution.cs
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);
}
}
29 changes: 29 additions & 0 deletions src/System.Diagnostics.Metrics.Temp/Gauge.cs
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);
}
}
}
84 changes: 84 additions & 0 deletions src/System.Diagnostics.Metrics.Temp/Meter.cs
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
}
}
Loading

0 comments on commit c5a8535

Please sign in to comment.