Skip to content
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

[payment] fork friendly hooks #2111

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 35 additions & 0 deletions src/payment/charge-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// This file is meant to be implemented by forks of the OpenTelemetry Demo repository
// to showcase custom observability functionality implemented by the fork.
// The OpenTelemetry Demo repository will not modify this file, allowing forks to
// demonstrate their own unique functionality without merge conflicts.

class chargeHooks {
constructor(request) {
this.request = request;
}

// The preHook function is called before the charge span is started
preHook() {

}

// The startHook function is called after the charge span is started
startHook() {

}

// The endHook function is called before the charge span is ended
endHook() {

}

// The postHook function is called after the charge span is ended
postHook() {

}
}

module.exports = chargeHooks;
15 changes: 15 additions & 0 deletions src/payment/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { FlagdProvider } = require('@openfeature/flagd-provider');
const flagProvider = new FlagdProvider();

const logger = require('./logger');
const chargeHooks = require("./charge-hooks");
const tracer = trace.getTracer('payment');
const meter = metrics.getMeter('payment');
const transactionsCounter = meter.createCounter('app.payment.transactions');
Expand All @@ -22,8 +23,15 @@ function random(arr) {
}

module.exports.charge = async request => {
const ch = new chargeHooks(request);
// call any fork hooks before starting span
ch.preHook();

const span = tracer.startSpan('charge');

// call any fork hooks after starting span
ch.startHook();

await OpenFeature.setProviderAndWait(flagProvider);

const numberVariant = await OpenFeature.getClient().getNumberValue("paymentFailure", 0);
Expand Down Expand Up @@ -82,7 +90,14 @@ module.exports.charge = async request => {
const { units, nanos, currencyCode } = request.amount;
logger.info({ transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }, loyalty_level }, 'Transaction complete.');
transactionsCounter.add(1, { 'app.payment.currency': currencyCode });

// call any fork hooks before ending span
ch.endHook();

span.end();

// call any fork hooks after ending span
ch.postHook();

return { transactionId };
};