Skip to content

Commit

Permalink
[payment] span attributes update (open-telemetry#335)
Browse files Browse the repository at this point in the history
* update span attributes

* update payment span attributes

* set charged attribute based on baggage
  • Loading branch information
puckpuck authored Aug 23, 2022
1 parent 0ca6d9f commit 79cba83
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
10 changes: 6 additions & 4 deletions docs/manual_span_attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ This document contains the list of manual Span Attributes used throughout the de

## PaymentService

| Name | Type | Description |
|------------------------|--------|--------------------|
| `app.payment.cost` | number | Total payment cost |
| `app.payment.currency` | string | Payment currency |
| Name | Type | Description |
|--------------------------|----------|------------------------------------------------------|
| `app.payment.amount` | number | Total payment amount |
| `app.payment.card_type` | string | Type of card used for payment |
| `app.payment.card_valid` | boolean | Was the card used valid |
| `app.payment.charged` | boolean | Was the charge successful (false with loadgenerator) |

## ProductCatalogService

Expand Down
64 changes: 37 additions & 27 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,60 @@
// limitations under the License.

// Npm
const opentelemetry = require('@opentelemetry/api')
const cardValidator = require('simple-card-validator')
const pino = require('pino')
const { v4: uuidv4 } = require('uuid')
const {context, propagation, trace} = require('@opentelemetry/api');
const cardValidator = require('simple-card-validator');
const pino = require('pino');
const { v4: uuidv4 } = require('uuid');

// Setup
const logger = pino()
const tracer = opentelemetry.trace.getTracer('paymentservice')
const logger = pino();
const tracer = trace.getTracer('paymentservice');

// Functions
module.exports.charge = request => {
const span = tracer.startSpan('charge')
const span = tracer.startSpan('charge');

const { creditCardNumber: number,
creditCardExpirationYear: year,
creditCardExpirationMonth: month
} = request.creditCard
const { units, nanos, currencyCode } = request.amount
const currentMonth = new Date().getMonth() + 1
const currentYear = new Date().getFullYear()
const lastFourDigits = number.substr(-4)
const transactionId = uuidv4()
} = request.creditCard;
const { units, nanos, currencyCode } = request.amount;
const currentMonth = new Date().getMonth() + 1;
const currentYear = new Date().getFullYear();
const lastFourDigits = number.substr(-4);
const transactionId = uuidv4();

const card = cardValidator(number)
const {card_type: cardType, valid } = card.getCardDetails()
const card = cardValidator(number);
const {card_type: cardType, valid } = card.getCardDetails();

span.setAttributes({
'app.payment.charge.cardType': cardType,
'app.payment.charge.valid': valid
})
'app.payment.card_type': cardType,
'app.payment.card_valid': valid
});

if (!valid)
throw new Error('Credit card info is invalid.')
if (!valid) {
throw new Error('Credit card info is invalid.');
}

if (!['visa', 'mastercard'].includes(cardType))
throw new Error(`Sorry, we cannot process ${cardType} credit cards. Only VISA or MasterCard is accepted.`)
if (!['visa', 'mastercard'].includes(cardType)) {
throw new Error(`Sorry, we cannot process ${cardType} credit cards. Only VISA or MasterCard is accepted.`);
}

if ((currentYear * 12 + currentMonth) > (year * 12 + month))
throw new Error(`The credit card (ending ${lastFourDigits}) expired on ${month}/${year}.`)
if ((currentYear * 12 + currentMonth) > (year * 12 + month)) {
throw new Error(`The credit card (ending ${lastFourDigits}) expired on ${month}/${year}.`);
}

span.setAttribute('app.payment.charged', true)
span.end()
// check baggage for synthetic_request=true, and add charged attribute accordingly
const baggage = propagation.getBaggage(context.active());
if (baggage && baggage.getEntry("synthetic_request") && baggage.getEntry("synthetic_request").value == "true") {
span.setAttribute('app.payment.charged', false);
} else {
span.setAttribute('app.payment.charged', true);
}

logger.info(`Transaction ${transactionId}: ${cardType} ending ${lastFourDigits} | Amount: ${units}.${nanos} ${currencyCode}`)
span.end();

logger.info(`Transaction ${transactionId}: ${cardType} ending ${lastFourDigits} | Amount: ${units}.${nanos} ${currencyCode}`);

return { transactionId }
}
3 changes: 1 addition & 2 deletions src/paymentservice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ function chargeServiceHandler(call, callback) {
try {
const amount = call.request.amount
span.setAttributes({
'app.payment.currency': amount.currency_code,
'app.payment.cost': parseFloat(`${amount.units}.${amount.nanos}`)
'app.payment.amount': parseFloat(`${amount.units}.${amount.nanos}`)
})
logger.info(`PaymentService#Charge invoked by: ${JSON.stringify(call.request)}`)

Expand Down

0 comments on commit 79cba83

Please sign in to comment.