-
Notifications
You must be signed in to change notification settings - Fork 773
/
Program.cs
102 lines (85 loc) · 3.97 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// <copyright file="Program.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.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
namespace CustomizingTheSdk;
public class Program
{
private static readonly Meter Meter1 = new("CompanyA.ProductA.Library1", "1.0");
private static readonly Meter Meter2 = new("CompanyA.ProductB.Library2", "1.0");
public static void Main()
{
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.ConfigureResource(res => res.AddService("example-service"))
.AddMeter(Meter1.Name)
.AddMeter(Meter2.Name)
// Rename an instrument to new name.
.AddView(instrumentName: "MyCounter", name: "MyCounterRenamed")
// Change Histogram boundaries
.AddView(instrumentName: "MyHistogram", new ExplicitBucketHistogramConfiguration() { Boundaries = new double[] { 10, 20 } })
// For the instrument "MyCounterCustomTags", aggregate with only the keys "tag1", "tag2".
.AddView(instrumentName: "MyCounterCustomTags", new MetricStreamConfiguration() { TagKeys = new string[] { "tag1", "tag2" } })
// Drop the instrument "MyCounterDrop".
.AddView(instrumentName: "MyCounterDrop", MetricStreamConfiguration.Drop)
// Advanced selection criteria and config via Func<Instrument, MetricStreamConfiguration>
.AddView((instrument) =>
{
if (instrument.Meter.Name.Equals("CompanyA.ProductB.Library2") &&
instrument.GetType().Name.Contains("Histogram"))
{
return new ExplicitBucketHistogramConfiguration() { Boundaries = new double[] { 10, 20 } };
}
return null;
})
// An instrument which does not match any views
// gets processed with default behavior. (SDK default)
// Uncommenting the following line will
// turn off the above default. i.e any
// instrument which does not match any views
// gets dropped.
// .AddView(instrumentName: "*", MetricStreamConfiguration.Drop)
.AddConsoleExporter()
.Build();
var random = new Random();
var counter = Meter1.CreateCounter<long>("MyCounter");
for (int i = 0; i < 20000; i++)
{
counter.Add(1, new("tag1", "value1"), new("tag2", "value2"));
}
var histogram = Meter1.CreateHistogram<long>("MyHistogram");
for (int i = 0; i < 20000; i++)
{
histogram.Record(random.Next(1, 1000), new("tag1", "value1"), new("tag2", "value2"));
}
var counterCustomTags = Meter1.CreateCounter<long>("MyCounterCustomTags");
for (int i = 0; i < 20000; i++)
{
counterCustomTags.Add(1, new("tag1", "value1"), new("tag2", "value2"), new("tag3", "value4"));
}
var counterDrop = Meter1.CreateCounter<long>("MyCounterDrop");
for (int i = 0; i < 20000; i++)
{
counterDrop.Add(1, new("tag1", "value1"), new("tag2", "value2"));
}
var histogram2 = Meter2.CreateHistogram<long>("MyHistogram2");
for (int i = 0; i < 20000; i++)
{
histogram2.Record(random.Next(1, 1000), new("tag1", "value1"), new("tag2", "value2"));
}
}
}