Skip to content

Commit

Permalink
docs(sdk-metrics): align documentation with current state (open-telem…
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Feb 19, 2025
1 parent 0cdf9ee commit d004d41
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
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 { resourceFromAttributes } = 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: resourceFromAttributes({
[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

0 comments on commit d004d41

Please sign in to comment.