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

Initial structure for running Processors #2038

Merged
merged 10 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions examples/AspNet/Examples.AspNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<PackageReference Include="Microsoft.AspNet.WebApi.WebHost" Version="$(MicrosoftAspNetWebApiWebHostPkgVer)" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="$(MicrosoftAspNetMvcPkgVer)" />
<PackageReference Include="Microsoft.AspNet.WebPages" Version="$(MicrosoftAspNetWebPagesPkgVer)" />
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />
victlu marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Api\OpenTelemetry.Api.csproj">
Expand Down
4 changes: 3 additions & 1 deletion examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal static object Run(int observationInterval)
{
using var provider = Sdk.CreateMeterProviderBuilder()
.AddSource("TestMeter") // All instruments from this meter are enabled.
.AddProcessor(new MeasurementProcessor())
.AddExportProcessor(new ExportMetricProcessor())
.SetObservationPeriod(observationInterval)
.Build();

Expand Down Expand Up @@ -56,7 +58,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
43 changes: 43 additions & 0 deletions src/OpenTelemetry/Metrics/Aggregator/AggState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="AggState.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.Collections.Concurrent;
using System.Diagnostics.Metrics;
using System.Threading;

#nullable enable

namespace OpenTelemetry.Metrics
{
public class AggState
{
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;
}
}
}
34 changes: 34 additions & 0 deletions src/OpenTelemetry/Metrics/Aggregator/AggregateContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="AggregateContext.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;
using System.Diagnostics.Metrics;

#nullable enable

namespace OpenTelemetry.Metrics
{
public class AggregateContext
{
public AggregateContext(MeasurementContext measurment)
{
this.Measurment = measurment;
}

internal MeasurementContext Measurment { get; }
}
}
40 changes: 40 additions & 0 deletions src/OpenTelemetry/Metrics/Aggregator/AggregatorProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <copyright file="AggregatorProcessor.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.Collections.Concurrent;
using System.Diagnostics.Metrics;
using System.Threading;

#nullable enable

namespace OpenTelemetry.Metrics
{
public class AggregatorProcessor : BaseProcessor<MeasurementContext>
{
private ConcurrentDictionary<Instrument, AggState> states = new ConcurrentDictionary<Instrument, AggState>();

public override void OnEnd(MeasurementContext data)
{
var state = this.states.GetOrAdd(data.Instrument, (k) => new AggState());
state.Update(data.Point);
}

public ConcurrentDictionary<Instrument, AggState> Collect()
{
return Interlocked.Exchange(ref this.states, new ConcurrentDictionary<Instrument, AggState>());
}
}
}
69 changes: 69 additions & 0 deletions src/OpenTelemetry/Metrics/DataPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// <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 virtual string ValueAsString()
{
throw new NotImplementedException();
}
}

public 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();
}
}
}
34 changes: 34 additions & 0 deletions src/OpenTelemetry/Metrics/Exporter/ExportMetricContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="ExportMetricContext.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.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

#nullable enable

namespace OpenTelemetry.Metrics
{
public class ExportMetricContext
{
internal List<ConcurrentDictionary<Instrument, AggState>> Exports = new List<ConcurrentDictionary<Instrument, AggState>>();

public ExportMetricContext()
{
}
}
}
40 changes: 40 additions & 0 deletions src/OpenTelemetry/Metrics/Exporter/ExportMetricProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <copyright file="ExportMetricProcessor.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.Concurrent;
using System.Diagnostics.Metrics;
using System.Threading;

#nullable enable

namespace OpenTelemetry.Metrics
{
public class ExportMetricProcessor : BaseProcessor<ExportMetricContext>
{
public override void OnEnd(ExportMetricContext data)
{
foreach (var exports in data.Exports)
{
foreach (var item in exports)
{
var msg = $"{item.Key.Meter.Name}:{item.Key.Name} = count:{item.Value.Count}, sum:{item.Value.Sum}";
Console.WriteLine($"Export: {msg}");
}
}
}
}
}
36 changes: 36 additions & 0 deletions src/OpenTelemetry/Metrics/MeasurementContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <copyright file="MeasurementContext.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;
using System.Diagnostics.Metrics;

#nullable enable

namespace OpenTelemetry.Metrics
{
public class MeasurementContext
{
internal readonly Instrument Instrument;
internal DataPoint? Point;

public MeasurementContext(Instrument instrument, DataPoint point)
{
this.Instrument = instrument;
this.Point = point;
}
}
}
51 changes: 51 additions & 0 deletions src/OpenTelemetry/Metrics/MeasurementProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <copyright file="MeasurementProcessor.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 class MeasurementProcessor
: BaseProcessor<MeasurementContext>
{
public override void OnStart(MeasurementContext data)
{
// Console.WriteLine($"Start: {this.AsString(data)}");

// // Replace datapoint
// var oldArray = data.Point!.Tags.ToArray();
// var newArray = new KeyValuePair<string, object?>[oldArray.Length + 1];
// newArray[0] = new KeyValuePair<string, object?>("newLabel", "newValue");
// oldArray.CopyTo(newArray, 1);
// var dp = new DataPoint<int>(10000, newArray);
// data.Point = dp;
}

public override void OnEnd(MeasurementContext data)
{
// Console.WriteLine($"End: {this.AsString(data)}");
}

public string AsString(MeasurementContext data)
{
var tags = string.Join(",", data.Point!.Tags.ToArray());
return $"{data.Instrument.Meter.Name}:{data.Instrument.Name}{tags} = {data.Point!.ValueAsString()}";
}
}
}
Loading