Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ the release.

## Unreleased

* [paymentservice] Add verion and tenant level attributes to spans. Added service.name to logs.
([#1815]https://github.com/open-telemetry/opentelemetry-demo/pull/1815)
* [grafana] Update grafana to 11.3.0
([#1764](https://github.com/open-telemetry/opentelemetry-demo/pull/1764))
* [chore] Move build args to .env file
Expand Down
42 changes: 31 additions & 11 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
const {context, propagation, trace, metrics} = require('@opentelemetry/api');
const { context, propagation, trace, metrics } = require('@opentelemetry/api');
const cardValidator = require('simple-card-validator');
const { v4: uuidv4 } = require('uuid');

const { OpenFeature } = require('@openfeature/server-sdk');
const { FlagdProvider} = require('@openfeature/flagd-provider');
const { FlagdProvider } = require('@openfeature/flagd-provider');
const flagProvider = new FlagdProvider();

const logger = require('./logger');
const tracer = trace.getTracer('paymentservice');
const meter = metrics.getMeter('paymentservice');
const transactionsCounter = meter.createCounter('app.payment.transactions')
const transactionsCounter = meter.createCounter('app.payment.transactions');

const TENANT_LEVEL = ['gold', 'silver', 'bronze'];
Comment thread
rcastley marked this conversation as resolved.
Outdated

/** Return random element from given array */
function random(arr) {
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
Comment thread
rcastley marked this conversation as resolved.

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

await OpenFeature.setProviderAndWait(flagProvider);

if (await OpenFeature.getClient().getBooleanValue("paymentServiceFailure", false)) {
throw new Error("PaymentService Fail Feature Flag Enabled");
// 75% chance to fail with version 350.10 span tag
if (Math.random() < 0.75) {
span.setAttributes({ 'app.version': '350.10', 'app.tenant.level': random(TENANT_LEVEL) });
Comment thread
rcastley marked this conversation as resolved.
Outdated
span.end();

throw new Error('Payment request failed. Invalid token. Version: 350.10');
}
}

const {
Expand All @@ -40,29 +55,34 @@ module.exports.charge = async request => {
});

if (!valid) {
span.setAttributes({ 'app.version': '350.09', 'app.tenant.level': random(TENANT_LEVEL) });
throw new Error('Credit card info is invalid.');
}

if (!['visa', 'mastercard'].includes(cardType)) {
span.setAttributes({ 'app.version': '350.09', 'app.tenant.level': random(TENANT_LEVEL) });
throw new Error(`Sorry, we cannot process ${cardType} credit cards. Only VISA or MasterCard is accepted.`);
}

if ((currentYear * 12 + currentMonth) > (year * 12 + month)) {
span.setAttributes({ 'app.version': '350.09', 'app.tenant.level': random(TENANT_LEVEL) });
throw new Error(`The credit card (ending ${lastFourDigits}) expired on ${month}/${year}.`);
}

// check baggage for synthetic_request=true, and add charged attribute accordingly
// 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") {
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);
}

span.end();
span.setAttributes({ 'app.version': '350.09', 'app.tenant.level': random(TENANT_LEVEL) });

const { units, nanos, currencyCode } = request.amount;
logger.info({transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }}, "Transaction complete.");
transactionsCounter.add(1, {"app.payment.currency": currencyCode})
return { transactionId }
}
logger.info({ transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode } }, 'Transaction complete.');
transactionsCounter.add(1, { 'app.payment.currency': currencyCode });
span.end();

return { transactionId };
};
17 changes: 15 additions & 2 deletions src/paymentservice/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

const pino = require('pino');
const pino = require('pino')

module.exports = pino();
const logger = pino({
mixin() {
return {
'service.name': process.env['OTEL_SERVICE_NAME'],
}
},
formatters: {
level: (label) => {
return { 'level': label };
},
},
});

module.exports = logger;