Skip to content

Commit

Permalink
[payment] - add manual instrumentation (open-telemetry#157)
Browse files Browse the repository at this point in the history
* add manual instrumentation

* use service name for tracer
  • Loading branch information
puckpuck authored Jun 23, 2022
1 parent fbc0778 commit 1778b08
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
const cardValidator = require('simple-card-validator');
const { v4: uuidv4 } = require('uuid');
const pino = require('pino');
const opentelemetry = require('@opentelemetry/api');
const tracer = opentelemetry.trace.getTracer("paymentservice");

const logger = pino({
name: 'paymentservice-charge',
Expand Down Expand Up @@ -56,13 +58,20 @@ class ExpiredCreditCard extends CreditCardError {
* @return transaction_id - a random uuid v4.
*/
module.exports = function charge (request) {
// create and start span
const span = tracer.startSpan("charge")

const { amount, credit_card: creditCard } = request;
const cardNumber = creditCard.credit_card_number;
const cardInfo = cardValidator(cardNumber);
const {
card_type: cardType,
valid
} = cardInfo.getCardDetails();
span.setAttributes({
"app.payment.charge.cardType": cardType,
"app.payment.charge.valid": valid
})

if (!valid) { throw new InvalidCreditCard(); }

Expand All @@ -79,5 +88,9 @@ module.exports = function charge (request) {
logger.info(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);

span.setAttribute("app.payment.charged", true);
// a manually created span needs to be ended
span.end();

return { transaction_id: uuidv4() };
};
13 changes: 13 additions & 0 deletions src/paymentservice/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const path = require('path');
const grpc = require('@grpc/grpc-js');
const pino = require('pino');
const protoLoader = require('@grpc/proto-loader');
const opentelemetry = require('@opentelemetry/api');

const charge = require('./charge');

Expand Down Expand Up @@ -45,11 +46,23 @@ class HipsterShopServer {
* @param {*} callback fn(err, ChargeResponse)
*/
static ChargeServiceHandler(call, callback) {
// get the current auto-instrumented span in context
const span = opentelemetry.trace.getSpan(opentelemetry.context.active())
try {
const amount = call.request.amount;
span.setAttributes({
"app.payment.currency": amount.currency_code,
"app.payment.cost": parseFloat(amount.units + "." + amount.nanos)
});
logger.info(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`);

const response = charge(call.request);
callback(null, response);

} catch (err) {
// record exception in span (will create a span event)
span.recordException(err);
span.setStatus({code: opentelemetry.SpanStatusCode.ERROR})
console.warn(err);
callback(err);
}
Expand Down

0 comments on commit 1778b08

Please sign in to comment.