@@ -245,6 +245,90 @@ within the maximum number of buckets defined by `MaxSize`. The default
245245 new Base2ExponentialBucketHistogramConfiguration { MaxSize = 40 })
246246```
247247
248+ #### Produce multiple metrics from single instrument
249+
250+ When an instrument matches multiple views, it can generate multiple metrics. For
251+ instance, if an instrument is matched by two different view configurations, it
252+ will result in two separate metrics being produced from that single instrument.
253+ Below is an example demonstrating how to leverage this capability to create two
254+ independent metrics from a single instrument. In this example, a histogram
255+ instrument is used to report measurements, and views are configured to produce
256+ two metrics : one aggregated using ` ExplicitBucketHistogramConfiguration ` and the
257+ other using ` Base2ExponentialBucketHistogramConfiguration ` .
258+
259+ ``` csharp
260+ var histogramWithMultipleAggregations = meter .CreateHistogram <long >(" HistogramWithMultipleAggregations" );
261+
262+ // Configure the Explicit Bucket Histogram aggregation with custom boundaries and new name.
263+ .AddView (instrumentName : " HistogramWithMultipleAggregations" , new ExplicitBucketHistogramConfiguration () { Boundaries = new double [] { 10 , 20 }, Name = " MyHistogramWithExplicitHistogram" })
264+
265+ // Use Base2 Exponential Bucket Histogram aggregation and new name.
266+ .AddView (instrumentName : " HistogramWithMultipleAggregations" , new Base2ExponentialBucketHistogramConfiguration () { Name = " MyHistogramWithBase2ExponentialBucketHistogram" })
267+
268+ // Both views rename the metric to avoid name conflicts. However, in this case,
269+ // renaming one would be sufficient.
270+
271+ // This measurement will be aggregated into two separate metrics.
272+ histogramWithMultipleAggregations .Record (10 , new (" tag1" , " value1" ), new (" tag2" , " value2" ));
273+ ```
274+
275+ When using views that produce multiple metrics from single instrument, it's
276+ crucial to rename the metric to prevent conflicts. In the event of conflict,
277+ OpenTelemetry will emit an internal warning but will still export both metrics.
278+ The impact of this behavior depends on the backend or receiver being used. You
279+ can refer to [ OpenTelemetry's
280+ specification] ( https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#opentelemetry-protocol-data-model-consumer-recommendations )
281+ for more details.
282+
283+ Below example is showing the * BAD* practice. DO NOT FOLLOW it.
284+
285+ ``` csharp
286+ var histogram = meter .CreateHistogram <long >(" MyHistogram" );
287+
288+ // Configure a view to aggregate based only on the "location" tag.
289+ .AddView (instrumentName : " MyHistogram" , metricStreamConfiguration : new MetricStreamConfiguration
290+ {
291+ TagKeys = new string [] { " location" },
292+ })
293+
294+ // Configure another view to aggregate based only on the "status" tag.
295+ .AddView (instrumentName : " MyHistogram" , metricStreamConfiguration : new MetricStreamConfiguration
296+ {
297+ TagKeys = new string [] { " status" },
298+ })
299+
300+ // The measurement below will be aggregated into two metric streams, but both will have the same name.
301+ // OpenTelemetry will issue a warning about this conflict and pass both streams to the exporter.
302+ // However, this may cause issues depending on the backend.
303+ histogram .Record (10 , new (" location" , " seattle" ), new (" status" , " OK" ));
304+ ```
305+
306+ The modified version, avoiding name conflict is shown below:
307+
308+ ``` csharp
309+ var histogram = meter .CreateHistogram <long >(" MyHistogram" );
310+
311+ // Configure a view to aggregate based only on the "location" tag,
312+ // and rename the metric.
313+ .AddView (instrumentName : " MyHistogram" , metricStreamConfiguration : new MetricStreamConfiguration
314+ {
315+ Name = " MyHistogramWithLocation" ,
316+ TagKeys = new string [] { " location" },
317+ })
318+
319+ // Configure a view to aggregate based only on the "status" tag,
320+ // and rename the metric.
321+ .AddView (instrumentName : " MyHistogram" , metricStreamConfiguration : new MetricStreamConfiguration
322+ {
323+ Name = " MyHistogramWithStatus" ,
324+ TagKeys = new string [] { " status" },
325+ })
326+
327+ // The measurement below will be aggregated into two separate metrics, "MyHistogramWithLocation"
328+ // and "MyHistogramWithStatus".
329+ histogram .Record (10 , new (" location" , " seattle" ), new (" status" , " OK" ));
330+ ```
331+
248332> [ !NOTE]
249333> The SDK currently does not support any changes to ` Aggregation ` type
250334 by using Views.
0 commit comments