Files
RSSHub/lib/utils/otel/metric.ts
T
dependabot[bot] edce496ba0 chore(deps): bump @opentelemetry/exporter-prometheus from 0.53.0 to 0.54.0 (#17286)
* chore(deps): bump @opentelemetry/exporter-prometheus

Bumps [@opentelemetry/exporter-prometheus](https://github.com/open-telemetry/opentelemetry-js) from 0.53.0 to 0.54.0.
- [Release notes](https://github.com/open-telemetry/opentelemetry-js/releases)
- [Changelog](https://github.com/open-telemetry/opentelemetry-js/blob/main/CHANGELOG.md)
- [Commits](https://github.com/open-telemetry/opentelemetry-js/compare/experimental/v0.53.0...experimental/v0.54.0)

---
updated-dependencies:
- dependency-name: "@opentelemetry/exporter-prometheus"
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore: fix pnpm install

* fix: use ATTR_SERVICE_NAME

SEMRESATTRS_SERVICE_NAME is deprecated

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-10-24 22:28:25 +08:00

68 lines
2.3 KiB
TypeScript

import { Resource } from '@opentelemetry/resources';
import { PrometheusExporter, PrometheusSerializer } from '@opentelemetry/exporter-prometheus';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { MeterProvider } from '@opentelemetry/sdk-metrics';
import { Attributes } from '@opentelemetry/api';
import { config } from '@/config';
interface IMetricAttributes extends Attributes {
method: string;
path: string;
status: number;
}
interface IHistogramAttributes extends IMetricAttributes {
unit: string;
}
const METRIC_PREFIX = 'rsshub';
const exporter = new PrometheusExporter({});
const provider = new MeterProvider({
resource: new Resource({
[ATTR_SERVICE_NAME]: 'rsshub',
}),
readers: [exporter],
});
const serializer = new PrometheusSerializer();
const meter = provider.getMeter('rsshub');
const requestTotal = meter.createCounter<IMetricAttributes>(`${METRIC_PREFIX}_request_total`);
const requestErrorTotal = meter.createCounter<IMetricAttributes>(`${METRIC_PREFIX}_request_error_total`);
const requestDurationSecondsBucket = meter.createHistogram<IHistogramAttributes>(`${METRIC_PREFIX}_request_duration_seconds_bucket`, {
advice: {
explicitBucketBoundaries: config.otel.seconds_bucket?.split(',').map(Number),
},
});
const request_duration_milliseconds_bucket = meter.createHistogram<IHistogramAttributes>(`${METRIC_PREFIX}_request_duration_milliseconds_bucket`, {
advice: {
explicitBucketBoundaries: config.otel.milliseconds_bucket?.split(',').map(Number),
},
});
export const requestMetric = {
success: (value: number, attributes: IMetricAttributes) => {
requestTotal.add(1, attributes);
request_duration_milliseconds_bucket.record(value, { unit: 'millisecond', ...attributes });
requestDurationSecondsBucket.record(value / 1000, { unit: 'second', ...attributes });
},
error: (attributes: IMetricAttributes) => {
requestErrorTotal.add(1, attributes);
},
};
export const getContext = () =>
new Promise<string>((resolve, reject) => {
exporter
.collect()
.then((value) => {
resolve(serializer.serialize(value.resourceMetrics));
})
.finally(() => {
reject('');
});
});