Skip to content

Commit d004d41

Browse files
authored
docs(sdk-metrics): align documentation with current state (#5485)
1 parent 0cdf9ee commit d004d41

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

doc/metrics.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,15 @@ for the resulting metric. The first step is select to the metrics to whom the Vi
362362
is relevant, the second step is to configure the customizations for the the selected
363363
metrics.
364364

365-
A Metric View is a class that can be instantiated via:
365+
A Metric View can be added to a `MeterProvider` like so
366366

367367
````typescript
368-
const view = new View({
369-
name: 'metric-view', // optionally, give the view a unique name
370-
// select instruments with a specific name
371-
instrumentName: 'http.server.duration',
368+
new MeterProvider({
369+
views: [{
370+
name: 'metric-view', // optionally, give the view a unique name
371+
// select instruments with a specific name
372+
instrumentName: 'http.server.duration',
373+
}]
372374
});
373375
````
374376

@@ -396,20 +398,22 @@ should be used to define the bucket sizes for the Histogram instrument.
396398
Below an example is given how you can define explicit buckets for a histogram.
397399

398400
```typescript
399-
// Define view for the histogram metric
400-
const histogramView = new View({
401-
aggregation: new ExplicitBucketHistogramAggregation([0, 1, 5, 10, 15, 20, 25, 30]),
402-
instrumentName: 'http.server.duration',
403-
instrumentType: InstrumentType.HISTOGRAM,
404-
});
405-
406-
// Note, the instrumentName is the same as the name that has been passed for
407-
// the Meter#createHistogram function
408-
409401
// Create an instance of the metric provider
410402
const meterProvider = new MeterProvider({
411403
views: [
412-
histogramView
404+
// Define view for the histogram metric
405+
{
406+
aggregation: {
407+
type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
408+
options: {
409+
boundaries: [0, 1, 5, 10, 15, 20, 25, 30],
410+
}
411+
},
412+
// Note, the instrumentName is the same as the name that has been passed for
413+
// the Meter#createHistogram function
414+
instrumentName: 'http.server.duration',
415+
instrumentType: InstrumentType.HISTOGRAM,
416+
}
413417
]
414418
});
415419

@@ -441,20 +445,20 @@ instruments with a specific name:
441445
The following view drops all instruments that are associated with a meter named `pubsub`:
442446

443447
```typescript
444-
const dropView = new View({
445-
aggregation: new DropAggregation(),
448+
{
449+
aggregation: { type: AggregationType.DROP },
446450
meterName: 'pubsub',
447-
});
451+
}
448452
```
449453

450454
Alternatively, you can also drop instruments with a specific instrument name,
451455
for example, all instruments of which the name starts with `http`:
452456

453457
```typescript
454-
const dropView = new View({
455-
aggregation: new DropAggregation(),
458+
{
459+
aggregation: { type: AggregationType.DROP },
456460
instrumentName: 'http*',
457-
});
461+
}
458462
```
459463

460464
### Customizing the metric attributes of instrument
@@ -467,12 +471,12 @@ In the example below will drop all attributes except attribute `environment` for
467471
all instruments.
468472

469473
```typescript
470-
new View({
474+
{
471475
// only export the attribute 'environment'
472476
attributeKeys: ['environment'],
473477
// apply the view to all instruments
474478
instrumentName: '*',
475-
})
479+
}
476480
```
477481

478482
## Exporting measurements

examples/otlp-exporter-node/metrics.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-htt
66
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
77
// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
88
const {
9-
ExponentialHistogramAggregation,
109
MeterProvider,
1110
PeriodicExportingMetricReader,
1211
View,
12+
AggregationType,
1313
} = require('@opentelemetry/sdk-metrics');
1414
const { resourceFromAttributes } = require('@opentelemetry/resources');
1515
const {
@@ -25,20 +25,18 @@ const metricExporter = new OTLPMetricExporter({
2525
// },
2626
});
2727

28-
// Define view for the exponential histogram metric
29-
const expHistogramView = new View({
30-
aggregation: new ExponentialHistogramAggregation(),
31-
// Note, the instrumentName is the same as the name that has been passed for
32-
// the Meter#createHistogram function for exponentialHistogram.
33-
instrumentName: 'test_exponential_histogram',
34-
});
35-
3628
// Create an instance of the metric provider
3729
const meterProvider = new MeterProvider({
3830
resource: resourceFromAttributes({
3931
[SEMRESATTRS_SERVICE_NAME]: 'basic-metric-service',
4032
}),
41-
views: [expHistogramView],
33+
// Define view for the exponential histogram metric
34+
views: [{
35+
aggregation: { type: AggregationType.EXPONENTIAL_HISTOGRAM },
36+
// Note, the instrumentName is the same as the name that has been passed for
37+
// the Meter#createHistogram function for exponentialHistogram.
38+
instrumentName: 'test_exponential_histogram',
39+
}],
4240
readers: [
4341
new PeriodicExportingMetricReader({
4442
exporter: metricExporter,

packages/sdk-metrics/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ Views can be registered when instantiating a `MeterProvider`:
6464
const meterProvider = new MeterProvider({
6565
views: [
6666
// override the bucket boundaries on `my.histogram` to [0, 50, 100]
67-
new View({ aggregation: new ExplicitBucketHistogramAggregation([0, 50, 100]), instrumentName: 'my.histogram'}),
67+
{
68+
aggregation: {
69+
type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
70+
options: {
71+
boundaries: [0, 50, 100]
72+
}
73+
},
74+
instrumentName: 'my.histogram'
75+
},
6876
// rename 'my.counter' to 'my.renamed.counter'
69-
new View({ name: 'my.renamed.counter', instrumentName: 'my.counter'})
77+
{ name: 'my.renamed.counter', instrumentName: 'my.counter'}
7078
]
7179
})
7280
```

0 commit comments

Comments
 (0)