Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a temp project with Metric API (which will come eventually from runtime) #2033

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is called ObservableCounter (or do you intend to change it later)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes will be change to match https://github.com/dotnet/designs/pull/211/files. This PR is to get started with a temp proj file.

{
// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many places not updated to the latest API spec:

  1. Instrument names (Distribution -> Histogram, Gauge -> ObservableGauge).
  2. Label - Attribute (or Tag?)

{
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