@joktec/kafka
Version:
JokTec - Kafka Service
63 lines • 3.32 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KafkaSendMetric = exports.KafkaMetricService = exports.KafkaMetricStatus = exports.TOTAL_RECEIVE_KAFKA_METRIC = exports.TOTAL_SEND_KAFKA_METRIC = void 0;
const core_1 = require("@joktec/core");
exports.TOTAL_SEND_KAFKA_METRIC = 'total_send_kafka_metric';
exports.TOTAL_RECEIVE_KAFKA_METRIC = 'total_receive_kafka_metric';
var KafkaMetricStatus;
(function (KafkaMetricStatus) {
KafkaMetricStatus["SUCCESS"] = "SUCCESS";
KafkaMetricStatus["ERROR"] = "ERROR";
})(KafkaMetricStatus || (exports.KafkaMetricStatus = KafkaMetricStatus = {}));
let KafkaMetricService = class KafkaMetricService {
constructor(totalSend, totalReceive) {
this.totalSend = totalSend;
this.totalReceive = totalReceive;
}
send(type, status, key, conId = core_1.DEFAULT_CON_ID) {
this.totalSend.inc({ type, status, key, conId });
}
receive(type, status, key, conId = core_1.DEFAULT_CON_ID) {
this.totalReceive.inc({ type, status, key, conId });
}
};
exports.KafkaMetricService = KafkaMetricService;
exports.KafkaMetricService = KafkaMetricService = __decorate([
(0, core_1.Injectable)(),
__param(0, (0, core_1.InjectMetric)(exports.TOTAL_SEND_KAFKA_METRIC)),
__param(1, (0, core_1.InjectMetric)(exports.TOTAL_RECEIVE_KAFKA_METRIC)),
__metadata("design:paramtypes", [core_1.Counter,
core_1.Counter])
], KafkaMetricService);
const KafkaSendMetric = () => {
return (0, core_1.BaseMethodDecorator)(async (options) => {
const { args, method, propertyKey, services } = options;
const [record = {}, producerConfig, conId = core_1.DEFAULT_CON_ID] = args;
const key = record.producerKey ?? record.topic ?? undefined;
const type = String(propertyKey);
const kafkaMetric = services.kafkaMetricService;
try {
await method(...args);
kafkaMetric.send(type, key, KafkaMetricStatus.SUCCESS, conId);
services.pinoLogger.debug(record, '`%s` [%s] kafka %s success', conId, key, type);
}
catch (error) {
kafkaMetric.send(type, key, KafkaMetricStatus.ERROR, conId);
services.pinoLogger.error(error, '`%s` [%s] kafka %s error', conId, key, type);
}
}, [KafkaMetricService]);
};
exports.KafkaSendMetric = KafkaSendMetric;
//# sourceMappingURL=kafka.metric.js.map