Skip to content

Commit 0c8bb93

Browse files
author
Dean Ward
committed
Update docs in preparation for 2.0 release
1 parent 332f49a commit 0c8bb93

File tree

6 files changed

+248
-266
lines changed

6 files changed

+248
-266
lines changed

README.md

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,85 @@ MyGet Pre-release feed: https://www.myget.org/gallery/stackoverflow
2121
First, create a `MetricsCollector` object. This is the top-level container which will hold all of your metrics and handle sending them to various metric endpoints. Therefore, you should only instantiate one, and make it a global singleton.
2222

2323
```csharp
24+
public class AppMetricSource : MetricSource
25+
{
26+
public static readonly MetricSourceOptions Options = new MetricSourceOptions
27+
{
28+
DefaultTags =
29+
{
30+
["host"] = Environment.MachineName
31+
}
32+
};
33+
34+
public AppMetricSource() : base(Options)
35+
{
36+
}
37+
}
38+
2439
var collector = new MetricsCollector(
2540
new MetricsCollectorOptions
2641
{
2742
ExceptionHandler = ex => HandleException(ex),
28-
MetricsNamePrefix = "app_name.",
2943
Endpoints = new[] {
3044
new MetricEndpoint("Bosun", new BosunMetricHandler(new Uri("http://bosun.mydomain.com:8070"))),
3145
new MetricEndpoint("SignalFx", new SignalFxMetricHandler(new Uri("https://mydomain.signalfx.com/api", "API_KEY"))),
3246
},
33-
PropertyToTagName = NameTransformers.CamelToLowerSnakeCase,
34-
DefaultTags = new Dictionary<string, string>
35-
{
36-
{"host", NameTransformers.Sanitize(Environment.MachineName.ToLower())},
37-
{"tier", "dev"}
47+
Sources = new[] {
48+
new GarbageCollectorMetricSource(AppMetricSource.DefaultOptions),
49+
new ProcessMetricSource(AppMetricSource.DefaultOptions),
50+
new AppMetricSource()
3851
}
3952
}
4053
);
54+
55+
// start the collector; it'll start sending metrics
56+
collector.Start();
57+
58+
// ...
59+
60+
// and then, during application shutdown, stop the collector
61+
collector.Stop();
62+
4163
```
4264

4365
#### .NET Core
4466

4567
For .NET Core, you can configure a `MetricsCollector` in your `Startup.cs`.
4668

4769
Using the snippet below will register an `IHostedService` in the service collection that manages the lifetime of the `MetricsCollector`
48-
and configures it with the specified endpoints, metric sets and tags.
70+
and configures it with the specified endpoints and metric sources.
4971

5072
```csharp
73+
public class AppMetricSource : MetricSource
74+
{
75+
public AppMetricSource(MetricSourceOptions options) : base(options)
76+
{
77+
}
78+
}
79+
5180
public class Startup
5281
{
5382
public void ConfigureServices(IServiceCollection services)
5483
{
5584
services.AddMetricsCollector()
56-
// add default metric sets
57-
.AddDefaultSets()
58-
// add additional tags across all metrics. By default we add the "host" tag
59-
.AddDefaultTag("tier", "dev");
85+
// configure things like default tags
86+
.ConfigureSources(
87+
o =>
88+
{
89+
// NOTE: default tags include the host name by default
90+
p.DefaultTags.Add("tier", "dev");
91+
}
92+
)
93+
// by default, common metric sources are added
94+
// that includes ProcessMetricSource, AspNetMetricSource & RuntimeMetricSource
95+
// here we add our application-specific metric source
96+
.AddSource<AppMetricSource>()
6097
// add endpoints we care about. By default we add a `LocalMetricHandler` that
6198
// just maintains the latest metrics in memory (useful for debugging)
6299
.AddBosunEndpoint(new Uri("http://bosun.mydomain.com:8070"))
63100
.AddSignalFxEndpoint(new Uri("https://mydomain.signalfx.com/api", "API_KEY"))
64101
.UseExceptionHandler(ex => HandleException(ex))
65-
// tweak other parts of `MetricsCollectionOptions`
102+
// tweak other options in of `MetricsCollectionOptions`
66103
.Configure(
67104
o => {
68105
o.SnapshotInterval = TimeSpan.FromSeconds(5);
@@ -77,34 +114,50 @@ public class Startup
77114
- [LocalMetricHandler](https://github.com/StackExchange/StackExchange.Metrics/blob/master/src/StackExchange.Metrics/Handlers/LocalMetricHandler.cs)
78115
- [SignalFxMetricHandler](https://github.com/StackExchange/StackExchange.Metrics/blob/master/src/StackExchange.Metrics/Handlers/SignalFxMetricHandler.cs)
79116

117+
Metrics are configured in a `MetricSource`. Using our `AppMetricSource` above:
118+
80119
Create a counter with only the default tags:
81120

82-
```csharp
83-
var counter = collector.CreateMetric<Counter>("my_counter", "units", "description");
121+
```cs
122+
public class AppMetricSource : MetricSource
123+
{
124+
public Counter MyCounter { get; }
125+
126+
public AppMetricSource(MetricSourceOptions options) : base(options)
127+
{
128+
MyCounter = AddCounter("my_counter", "units", "description");
129+
}
130+
}
84131
```
85132

86133
Increment the counter by 1:
87134

88-
```csharp
89-
counter.Increment();
135+
```cs
136+
appSource.MyCounter.Increment();
90137
```
91138

92139
### Using Tags
93140

94-
Tags are used to subdivide data in various metric platforms. In StackExchange.Metrics, tag sets are defined as C# classes. For example:
141+
Tags are used to subdivide data in various metric platforms. In StackExchange.Metrics, tags are by specifying additional arguments when creating a metric. For example:
95142

96-
```csharp
97-
public class SomeCounter : Counter
143+
```cs
144+
public class AppMetricSource : MetricSource
98145
{
99-
[MetricTag] public readonly string SomeTag;
100-
101-
public RouteCounter(string tag)
102-
{
103-
SomeTag = tag;
104-
}
146+
public Counter<string> MyCounterWithTag { get; }
147+
148+
public AppMetricSource(MetricSourceOptions options) : base(options)
149+
{
150+
MyCounterWithTag = AddCounter("my_counter", "units", "description", new MetricTag<string>("some_tag"));
151+
}
105152
}
106153
```
107154

155+
Incrementing that counter works exactly the same as incrementing a counter without tags, but we need to specify the values:
156+
157+
```cs
158+
appSource.MyCounter.Increment("tag_value");
159+
```
160+
108161
For more details, see the [Tags Documentation](https://github.com/StackExchange/StackExchange.Metrics/blob/master/docs/Tags.md).
109162

110163
### Metric Types
@@ -130,18 +183,14 @@ __[Gauges](https://github.com/StackExchange/StackExchange.Metrics/blob/master/do
130183

131184
If none of the built-in metric types meet your specific needs, it's easy to [create your own](https://github.com/StackExchange/StackExchange.Metrics/blob/master/docs/MetricTypes.md#create-your-own).
132185

133-
### Metric Groups
134-
135-
Metric groups allow you to easily setup metrics which share the same name, but with different tag values. [See Documentation](https://github.com/StackExchange/StackExchange.Metrics/blob/master/docs/MetricGroup.md).
136-
137-
### Metric sets
186+
### Metric Sources
138187

139-
Metric sets are pre-packaged sets of metrics that are useful across different applications. [See Documentation](https://github.com/StackExchange/StackExchange.Metrics/blob/master/docs/MetricSet.md) for further details.
188+
Metric sets are pre-packaged sources of metrics that are useful across different applications. [See Documentation](https://github.com/StackExchange/StackExchange.Metrics/blob/master/docs/MetricSources.md) for further details.
140189

141190
## Implementation Notes
142191

143-
Periodically a `MetricsCollector` instance serializes all the metrics that it is responsible for collecting.
144-
When it does so it serially calls Serialize on each metric which eventually results in a call to WriteValue.
192+
Periodically a `MetricsCollector` instance serializes all the metrics from the sources attached to it.
193+
When it does so it serially calls `WriteReadings` on each metric.
145194
WriteValue uses an `IMetricBatch` to assist in writing metrics into an endpoint-defined format using an
146195
implementation of `IBufferWriter<byte>` for buffering purposes.
147196

docs/MetricGroup.md

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Metric Sets
1+
# Metric Sources
22

3-
Metric sets are pre-packaged sets of metrics that are useful across different applications. These can be added to a `MetricsCollector` by specifying in the `IEnumerable<IMetricSet>` exposed at `MetricsCollectorOptions.Sets`.
3+
Metric sources are a way to consume metrics and their readings. These can be added to a `MetricsCollector` by specifying in the `IEnumerable<MetricSource>` exposed at `MetricsCollectorOptions.Sources`.
44

5-
## Built-in sets
5+
## Built-in sources
66

7-
### ProcessMetricSet
7+
We provide some built-in metric sources that can report basic metrics within an application. Built-in sources can be added to a collector using the `MetricsCollectorOptions.Sources` property or by calling `AddDefaultSources` on the `IMetricsCollectorBuilder` exposed by `AddMetricsCollector` on an `IServiceCollection`.
8+
9+
### ProcessMetricSource
810

911
Provides basic metrics for a .NET application. This set contains the following metrics:
1012

@@ -13,15 +15,15 @@ Provides basic metrics for a .NET application. This set contains the following m
1315
- `dotnet.mem.virtual` - virtual memory for the process
1416
- `dotnet.mem.paged` - paged memory for the process
1517

16-
### GarbageCollectionMetricSet (.NET Full Framework)
18+
### GarbageCollectionMetricSource (.NET Full Framework only)
1719

1820
Provides metrics about the garbage collector (GC). This set contains the following metrics:
1921

2022
- `dotnet.mem.collections.gen0` - number of gen-0 collections
2123
- `dotnet.mem.collections.gen1` - number of gen-1 collections
2224
- `dotnet.mem.collections.gen2` - number of gen-2 collections
2325

24-
### RuntimeMetricSet (.NET Core)
26+
### RuntimeMetricSource (.NET Core only)
2527

2628
Provides .NET Core runtime metrics which includes:
2729

@@ -42,18 +44,31 @@ Provides .NET Core runtime metrics which includes:
4244
Note: The `dotnet.mem.` generation counters are only updated when the garbage collector runs.
4345
Until it runs, these counters will be zero and if allocations are low, these counters will be infrequently updated.
4446

45-
### AspNetMetricSet (.NET Core)
47+
### AspNetMetricSource (.NET Core only)
4648

4749
- `dotnet.kestrel.requests.per_sec` - requests per second
4850
- `dotnet.kestrel.requests.total` - total requests
4951
- `dotnet.kestrel.requests.current` - current requests
5052
- `dotnet.kestrel.requests.failed` - failed requests
5153

52-
## Custom Metric Sets
54+
## Custom Metric Sources
55+
56+
Custom metric sets can be defined by instantiating `MetricSource` and calling the `Add*` methods or by deriving from `MetricSource`.
5357

54-
Custom metric sets can be defined by implementing the `IMetricSet` interface. Implementors need to implement two methods:
58+
Typically an application will define an `AppMetricSource` as follows:
5559

56-
- `Initialize` - this method is passed an `IMetricsCollector` and should be used to create metrics that the set defines. It can also be used to hook up long-lived metrics monitoring checks
57-
- `Snapshot` - this method can be optionally implemented and is executed everytime the collector snapshots metrics for reporting
60+
```cs
61+
public class AppMetricSource : MetricSource
62+
{
63+
public Counter MyCounter { get; }
64+
public SamplingGauge<string, HttpStatusCode> MyGaugeWithTags { get; }
65+
66+
public AppMetricSource(MetricSourceOptions options) : base(options)
67+
{
68+
MyCounter = AddCounter("my_counter", "units", "description");
69+
MyGaugeWithTags = AddCounter("my_gauge", "units", "description", new MetricTag<string>("route"), new MetricTag<HttpStatusCode>("status_code"));
70+
}
71+
}
72+
```
5873

59-
Once defined `IMetricSet` implementations should be added to the `MetricsCollectorOptions` that is passed to the `MetricsCollector`.
74+
Custom metric sources can be configured using `MetricsCollectorOptions.Sources` in .NET full framework or the `AddSource` method on the `IMetricsCollectorBuilder` exposed by `AddMetricsCollector` on an `IServiceCollection`.

0 commit comments

Comments
 (0)