Skip to content

Commit

Permalink
Initial structure for running Processors (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
victlu authored May 10, 2021
1 parent 4faf1d4 commit 4190b6b
Show file tree
Hide file tree
Showing 27 changed files with 680 additions and 138 deletions.
7 changes: 5 additions & 2 deletions examples/Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Program
/// dotnet run -p Examples.Console.csproj prometheus -i 15 -p 9184 -d 2
/// dotnet run -p Examples.Console.csproj otlp -e "http://localhost:4317"
/// dotnet run -p Examples.Console.csproj zpages
/// dotnet run -p Examples.Console.csproj metrics -p 100
/// dotnet run -p Examples.Console.csproj metrics -p 100 -e 500
///
/// The above must be run from the project root folder
/// (eg: C:\repos\opentelemetry-dotnet\examples\Console\).
Expand All @@ -47,7 +47,7 @@ public static void Main(string[] args)
(JaegerOptions options) => TestJaegerExporter.Run(options.Host, options.Port),
(ZipkinOptions options) => TestZipkinExporter.Run(options.Uri),
(PrometheusOptions options) => TestPrometheusExporter.Run(options.Port, options.PushIntervalInSecs, options.DurationInMins),
(MetricsOptions options) => TestMetrics.Run(options.ObservationPeriodMilliseconds),
(MetricsOptions options) => TestMetrics.Run(options.ObservationPeriodMilliseconds, options.CollectionPeriodMilliseconds),
(GrpcNetClientOptions options) => TestGrpcNetClient.Run(),
(HttpClientOptions options) => TestHttpClient.Run(),
(RedisOptions options) => TestRedis.Run(options.Uri),
Expand Down Expand Up @@ -100,6 +100,9 @@ internal class MetricsOptions
{
[Option('p', "observationPeriodMilliseconds", Default = 100, HelpText = "Observation period.", Required = false)]
public int ObservationPeriodMilliseconds { get; set; }

[Option('c', "collectionPeriodMilliseconds", Default = 500, HelpText = "Collection period.", Required = false)]
public int CollectionPeriodMilliseconds { get; set; }
}

[Verb("grpc", HelpText = "Specify the options required to test Grpc.Net.Client")]
Expand Down
7 changes: 5 additions & 2 deletions examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ namespace Examples.Console
{
internal class TestMetrics
{
internal static object Run(int observationInterval)
internal static object Run(int observationInterval, int collectionInterval)
{
using var provider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestMeter") // All instruments from this meter are enabled.
.SetObservationPeriod(observationInterval)
.SetCollectionPeriod(collectionInterval)
.AddProcessor(new TagEnrichmentProcessor())
.AddExportProcessor(new MetricConsoleExporter())
.Build();

using var meter = new Meter("TestMeter", "0.0.1");
Expand All @@ -56,7 +59,7 @@ internal static object Run(int observationInterval)
};
});

Task.Delay(50).Wait();
Task.Delay(5000).Wait();
System.Console.WriteLine("Press Enter key to exit.");
return null;
}
Expand Down
39 changes: 39 additions & 0 deletions src/OpenTelemetry/Metrics/AggregateState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// <copyright file="AggregateState.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 OpenTelemetry.Metrics
{
internal class AggregateState
{
internal long Count = 0;
internal long Sum = 0;

public virtual void Update(DataPoint? value)
{
long val = 0;

if (value is DataPoint<int> idp)
{
val = idp.Value;
}

this.Count++;
this.Sum += val;
}
}
}
51 changes: 51 additions & 0 deletions src/OpenTelemetry/Metrics/DataPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <copyright file="DataPoint.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;
using System.Collections.Generic;

#nullable enable

namespace OpenTelemetry.Metrics
{
public abstract class DataPoint
{
private KeyValuePair<string, object?>[] tags;

public DataPoint(ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
this.tags = tags.ToArray();
}

public ReadOnlySpan<KeyValuePair<string, object?>> Tags
{
get
{
return new ReadOnlySpan<KeyValuePair<string, object?>>(this.tags);
}
}

public void SetTags(KeyValuePair<string, object?>[] tags)
{
this.tags = tags;
}

public virtual string ValueAsString()
{
throw new NotImplementedException();
}
}
}
46 changes: 46 additions & 0 deletions src/OpenTelemetry/Metrics/DataPoint{T}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <copyright file="DataPoint{T}.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;
using System.Collections.Generic;

#nullable enable

namespace OpenTelemetry.Metrics
{
internal class DataPoint<T> : DataPoint
where T : unmanaged
{
internal readonly T Value;

public DataPoint(T value, params KeyValuePair<string, object?>[] tags)
: base(new ReadOnlySpan<KeyValuePair<string, object?>>(tags))
{
this.Value = value;
}

public DataPoint(T value, ReadOnlySpan<KeyValuePair<string, object?>> tags)
: base(tags)
{
this.Value = value;
}

public override string ValueAsString()
{
return this.Value.ToString();
}
}
}
50 changes: 49 additions & 1 deletion src/OpenTelemetry/Metrics/MeterProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,55 @@ public static MeterProviderBuilder SetObservationPeriod(this MeterProviderBuilde
return meterProviderBuilderSdk.SetObservationPeriod(periodMilliseconds);
}

return null;
return meterProviderBuilder;
}

/// <summary>
/// Sets collection period.
/// </summary>
/// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
/// <param name="periodMilliseconds">Perion in milliseconds.</param>
/// <returns><see cref="MeterProvider"/>.</returns>
public static MeterProviderBuilder SetCollectionPeriod(this MeterProviderBuilder meterProviderBuilder, int periodMilliseconds)
{
if (meterProviderBuilder is MeterProviderBuilderSdk meterProviderBuilderSdk)
{
return meterProviderBuilderSdk.SetCollectionPeriod(periodMilliseconds);
}

return meterProviderBuilder;
}

/// <summary>
/// Add measurement processor.
/// </summary>
/// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
/// <param name="processor">Measurement Processors.</param>
/// <returns><see cref="MeterProvider"/>.</returns>
public static MeterProviderBuilder AddProcessor(this MeterProviderBuilder meterProviderBuilder, MeasurementProcessor processor)
{
if (meterProviderBuilder is MeterProviderBuilderSdk meterProviderBuilderSdk)
{
return meterProviderBuilderSdk.AddMeasurementProcessor(processor);
}

return meterProviderBuilder;
}

/// <summary>
/// Add export processor.
/// </summary>
/// <param name="meterProviderBuilder"><see cref="MeterProviderBuilder"/>.</param>
/// <param name="processor">Measurement Processors.</param>
/// <returns><see cref="MeterProvider"/>.</returns>
public static MeterProviderBuilder AddExportProcessor(this MeterProviderBuilder meterProviderBuilder, MetricProcessor processor)
{
if (meterProviderBuilder is MeterProviderBuilderSdk meterProviderBuilderSdk)
{
return meterProviderBuilderSdk.AddExporter(processor);
}

return meterProviderBuilder;
}

/// <summary>
Expand Down
33 changes: 31 additions & 2 deletions src/OpenTelemetry/Metrics/MeterProviderBuilderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ namespace OpenTelemetry.Metrics
internal class MeterProviderBuilderSdk : MeterProviderBuilder
{
private readonly List<string> meterSources = new List<string>();
private int observationPeriodMilliseconds;
private int observationPeriodMilliseconds = 1000;
private int collectionPeriodMilliseconds = 1000;

internal MeterProviderBuilderSdk()
{
}

internal List<MeasurementProcessor> MeasurementProcessors { get; } = new List<MeasurementProcessor>();

internal List<MetricProcessor> ExportProcessors { get; } = new List<MetricProcessor>();

public override MeterProviderBuilder AddSource(params string[] names)
{
if (names == null)
Expand All @@ -54,9 +59,33 @@ internal MeterProviderBuilderSdk SetObservationPeriod(int periodMilliseconds)
return this;
}

internal MeterProviderBuilderSdk SetCollectionPeriod(int periodMilliseconds)
{
this.collectionPeriodMilliseconds = periodMilliseconds;
return this;
}

internal MeterProviderBuilderSdk AddMeasurementProcessor(MeasurementProcessor processor)
{
this.MeasurementProcessors.Add(processor);
return this;
}

internal MeterProviderBuilderSdk AddExporter(MetricProcessor processor)
{
this.ExportProcessors.Add(processor);
return this;
}

internal MeterProvider Build()
{
return new MeterProviderSdk(this.meterSources, this.observationPeriodMilliseconds);
// TODO: Need to review using a struct for BuildOptions
return new MeterProviderSdk(
this.meterSources,
this.observationPeriodMilliseconds,
this.collectionPeriodMilliseconds,
this.MeasurementProcessors.ToArray(),
this.ExportProcessors.ToArray());
}
}
}
Loading

0 comments on commit 4190b6b

Please sign in to comment.