Skip to content

docs(sdk-metrics): align documentation with current state #5485

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
52 changes: 28 additions & 24 deletions doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,15 @@ for the resulting metric. The first step is select to the metrics to whom the Vi
is relevant, the second step is to configure the customizations for the the selected
metrics.

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

````typescript
const view = new View({
name: 'metric-view', // optionally, give the view a unique name
// select instruments with a specific name
instrumentName: 'http.server.duration',
new MeterProvider({
views: [{
name: 'metric-view', // optionally, give the view a unique name
// select instruments with a specific name
instrumentName: 'http.server.duration',
}]
});
````

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

```typescript
// Define view for the histogram metric
const histogramView = new View({
aggregation: new ExplicitBucketHistogramAggregation([0, 1, 5, 10, 15, 20, 25, 30]),
instrumentName: 'http.server.duration',
instrumentType: InstrumentType.HISTOGRAM,
});

// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function

// Create an instance of the metric provider
const meterProvider = new MeterProvider({
views: [
histogramView
// Define view for the histogram metric
{
aggregation: {
type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
options: {
boundaries: [0, 1, 5, 10, 15, 20, 25, 30],
}
},
// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function
instrumentName: 'http.server.duration',
instrumentType: InstrumentType.HISTOGRAM,
}
]
});

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

```typescript
const dropView = new View({
aggregation: new DropAggregation(),
{
aggregation: { type: AggregationType.DROP },
meterName: 'pubsub',
});
}
```

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

```typescript
const dropView = new View({
aggregation: new DropAggregation(),
{
aggregation: { type: AggregationType.DROP },
instrumentName: 'http*',
});
}
```

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

```typescript
new View({
{
// only export the attribute 'environment'
attributeKeys: ['environment'],
// apply the view to all instruments
instrumentName: '*',
})
}
```

## Exporting measurements
Expand Down
18 changes: 8 additions & 10 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-htt
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
const {
ExponentialHistogramAggregation,
MeterProvider,
PeriodicExportingMetricReader,
View,
AggregationType,
} = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const {
Expand All @@ -25,20 +25,18 @@ const metricExporter = new OTLPMetricExporter({
// },
});

// Define view for the exponential histogram metric
const expHistogramView = new View({
aggregation: new ExponentialHistogramAggregation(),
// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function for exponentialHistogram.
instrumentName: 'test_exponential_histogram',
});

// Create an instance of the metric provider
const meterProvider = new MeterProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'basic-metric-service',
}),
views: [expHistogramView],
// Define view for the exponential histogram metric
views: [{
aggregation: { type: AggregationType.EXPONENTIAL_HISTOGRAM },
// Note, the instrumentName is the same as the name that has been passed for
// the Meter#createHistogram function for exponentialHistogram.
instrumentName: 'test_exponential_histogram',
}],
readers: [
new PeriodicExportingMetricReader({
exporter: metricExporter,
Expand Down
12 changes: 10 additions & 2 deletions packages/sdk-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ Views can be registered when instantiating a `MeterProvider`:
const meterProvider = new MeterProvider({
views: [
// override the bucket boundaries on `my.histogram` to [0, 50, 100]
new View({ aggregation: new ExplicitBucketHistogramAggregation([0, 50, 100]), instrumentName: 'my.histogram'}),
{
aggregation: {
type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
options: {
boundaries: [0, 50, 100]
}
},
instrumentName: 'my.histogram'
},
// rename 'my.counter' to 'my.renamed.counter'
new View({ name: 'my.renamed.counter', instrumentName: 'my.counter'})
{ name: 'my.renamed.counter', instrumentName: 'my.counter'}
]
})
```
Expand Down
Loading