Skip to content

Commit

Permalink
docs: align docs and examples with changes
Browse files Browse the repository at this point in the history
docs: align docs and examples with changes
  • Loading branch information
pichlermarc committed Nov 12, 2024
1 parent 89a6af6 commit e2a36a9
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 105 deletions.
52 changes: 27 additions & 25 deletions doc/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,20 @@ 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]),
const histogramView: ViewOptions = {
aggregation: {
type: AggregationType.EXPLICIT_BUCKET_HISTOGRAM,
boundaries: [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({
const meterProvider = createMeterProvider({
views: [
histogramView
]
Expand Down Expand Up @@ -441,20 +444,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(),
const dropView: ViewOptions = {
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(),
const dropView: ViewOptions = {
aggregation: { type: AggregationType.DROP },
instrumentName: 'http*',
});
};
```

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

```typescript
new View({
const attributeLimitingView: ViewOptions = {
// only export the attribute 'environment'
attributeKeys: ['environment'],
attributeProcesssors: [createAllowListAttributesProcessor(['environment'])],
// apply the view to all instruments
instrumentName: '*',
})
};
```

## Exporting measurements
Expand All @@ -497,22 +500,24 @@ to use the Prometheus exporter `PrometheusExporter` which is included in the

```typescript
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { createMetricReader } = require('@opentelemetry/sdk-metrics');

// Add your port to the Prometheus options
const options = { port: 9464 };
const options = {port: 9464};
const exporter = new PrometheusExporter(options);

// Creates MeterProvider and installs the exporter as a MetricReader
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const meterProvider = createMeterProvider({
readers: [exporter]
});

const meter = meterProvider.getMeter('example-prometheus');

// Now, start recording data
const counter = meter.createCounter('metric_name', {
description: 'Example of a counter'
});
counter.add(10, { pid: process.pid });
counter.add(10, {pid: process.pid});
```

In the above example the instantiated `PrometheusExporter` is configured to expose
Expand All @@ -534,19 +539,16 @@ The example below shows how you can configure OpenTelemetry JavaScript to use
the OTLP exporter using http/protobuf.

```typescript
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
const collectorOptions = {
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
concurrencyLimit: 1, // an optional limit on pending requests
};
const exporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = createMeterProvider({
readers: [createPeriodicExportingMetricReader({ exporter: metricExporter, exportIntervalMillis: 1000 })],
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
15 changes: 8 additions & 7 deletions examples/opentelemetry-web/examples/metrics/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { DiagConsoleLogger, DiagLogLevel, diag, metrics } = require('@opentelemetry/api');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider, createPeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');

// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
Expand All @@ -18,13 +18,14 @@ function stopMetrics() {
function startMetrics() {
console.log('STARTING METRICS');

const meterProvider = new MeterProvider();
metrics.setGlobalMeterProvider(meterProvider);
const meterProvider = createMeterProvider({
readers: [createPeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 1000
})]
});

meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 1000
}));
metrics.setGlobalMeterProvider(meterProvider);

meter = meterProvider.getMeter('example-exporter-collector')

Expand Down
39 changes: 17 additions & 22 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
// const { ConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
// const { createConsoleMetricExporter } = require('@opentelemetry/sdk-metrics');
const {
ExponentialHistogramAggregation,
MeterProvider,
PeriodicExportingMetricReader,
View,
createMeterProvider,
createPeriodicExportingMetricReader,
AggregationType,
} = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const {
Expand All @@ -25,30 +24,26 @@ 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({
const meterProvider = createMeterProvider({
readers: [createPeriodicExportingMetricReader({
exporter: metricExporter,
// exporter: createConsoleMetricExporter(),
exportIntervalMillis: 1000,
})],
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',
}],
});

meterProvider.addMetricReader(
createPeriodicExportingMetricReader({
exporter: metricExporter,
// exporter: new ConsoleMetricExporter(),
exportIntervalMillis: 1000,
})
);

const meter = meterProvider.getMeter('example-exporter-collector');

const requestCounter = meter.createCounter('requests', {
Expand Down
27 changes: 15 additions & 12 deletions experimental/examples/opencensus-shim/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
NodeTracerProvider,
BatchSpanProcessor,
} = require('@opentelemetry/sdk-trace-node');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider } = require('@opentelemetry/sdk-metrics');
const {
OTLPTraceExporter,
} = require('@opentelemetry/exporter-trace-otlp-grpc');
Expand Down Expand Up @@ -56,17 +56,20 @@ module.exports = function setup(serviceName) {
);
tracerProvider.register();

const meterProvider = new MeterProvider({ resource });
meterProvider.addMetricReader(
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
})
);
const meterProvider = createMeterProvider({
readers: [
new PrometheusExporter({
metricProducers: [
new OpenCensusMetricProducer({
openCensusMetricProducerManager:
oc.Metrics.getMetricProducerManager(),
}),
],
})
],
resource
});

metrics.setGlobalMeterProvider(meterProvider);

// Start OpenCensus tracing
Expand Down
7 changes: 4 additions & 3 deletions experimental/examples/prometheus/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { MeterProvider, createMeterProvider} = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

// Optional and only needed to see the internal diagnostic logging (during development)
Expand All @@ -16,8 +16,9 @@ const exporter = new PrometheusExporter({}, () => {
});

// Creates MeterProvider and installs the exporter as a MetricReader
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const meterProvider = createMeterProvider({
readers: [exporter]
});
const meter = meterProvider.getMeter('example-prometheus');

// Creates metric instruments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The OTLPMetricsExporter in Node expects the URL to only be the hostname. It will
options that work with trace also work with metrics.

```js
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createPeriodicExportingMetricReader, createMeterProvider } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
const collectorOptions = {
// url is optional and can be omitted - default is http://localhost:4317
Expand All @@ -36,12 +36,14 @@ const collectorOptions = {
};

const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
})
]
});

['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => meterProvider.shutdown().catch(console.error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ the [Collector Trace Exporter for web and node][trace-exporter-url].
The OTLPMetricExporter in Web expects the endpoint to end in `/v1/metrics`.

```js
import { MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { createMeterProvider, createPeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';

const collectorOptions = {
Expand All @@ -35,12 +35,14 @@ const collectorOptions = {
concurrencyLimit: 1, // an optional limit on pending requests
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
})
]
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand All @@ -51,19 +53,21 @@ counter.add(10, { 'key': 'value' });
## Metrics in Node

```js
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createMeterProvider, createPeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
const collectorOptions = {
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
concurrencyLimit: 1, // an optional limit on pending requests
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
})
]
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ To see sample code and documentation for the traces exporter, visit the [Collect
## Metrics in Node - PROTO over http

```js
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { createPeriodicExportingMetricReader, createMeterProvider } = require('@opentelemetry/sdk-metrics');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
const collectorOptions = {
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:4318/v1/metrics
};
const metricExporter = new OTLPMetricExporter(collectorOptions);
const meterProvider = new MeterProvider({});

meterProvider.addMetricReader(createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
}));
const meterProvider = createMeterProvider({
readers: [
createPeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 1000,
})
]
});

// Now, start recording data
const meter = meterProvider.getMeter('example-meter');
Expand Down
Loading

0 comments on commit e2a36a9

Please sign in to comment.