Skip to content

Commit

Permalink
[paymentservice] add paymentServiceFailure & paymentServiceUnreachabl…
Browse files Browse the repository at this point in the history
…e featureflags
  • Loading branch information
EislM0203 committed Feb 15, 2024
1 parent 371b36f commit 283dca6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ the release.
([#1377](https://github.com/open-telemetry/opentelemetry-demo/pull/1377))
* [cartservice] update .NET to .NET 8.0.2
([#1380](https://github.com/open-telemetry/opentelemetry-demo/pull/1380))
* [paymentservice] Add paymentServiceFailure & paymentServiceUnreachable featureflags
([](https://github.com/open-telemetry/opentelemetry-demo/pull/))

## 1.7.2

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ services:
ports:
- "${PAYMENT_SERVICE_PORT}"
environment:
- FEATURE_FLAG_GRPC_SERVICE_ADDR
- PAYMENT_SERVICE_PORT
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE
Expand Down
2 changes: 2 additions & 0 deletions kubernetes/opentelemetry-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10093,6 +10093,8 @@ spec:
- containerPort: 8080
name: service
env:
- name: FEATURE_FLAG_GRPC_SERVICE_ADDR
value: 'opentelemetry-demo-featureflagservice:50053'
- name: OTEL_SERVICE_NAME
valueFrom:
fieldRef:
Expand Down
4 changes: 3 additions & 1 deletion src/ffspostgres/init-scripts/20-ffs_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ VALUES
('productCatalogFailure', 'Fail product catalog service on a specific product', 0),
('recommendationCache', 'Cache recommendations', 0),
('adServiceFailure', 'Fail ad service requests', 0),
('cartServiceFailure', 'Fail cart service requests', 0);
('cartServiceFailure', 'Fail cart service requests', 0),
('paymentServiceFailure', 'Fail payment service charge requests', 0),
('paymentServiceUnreachable', 'Payment service is unavailable', 0);
10 changes: 8 additions & 2 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ const tracer = trace.getTracer('paymentservice');
const meter = metrics.getMeter('paymentservice');
const transactionsCounter = meter.createCounter('app.payment.transactions')

module.exports.charge = request => {
const { getFeatureFlagEnabled } = require('./grpc-ff-client')

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

if (await getFeatureFlagEnabled("paymentServiceFailure")) {
throw new Error("PaymentService Fail Feature Flag Enabled");
}

const {
creditCardNumber: number,
creditCardExpirationYear: year,
Expand Down Expand Up @@ -56,4 +62,4 @@ module.exports.charge = request => {
logger.info({transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }}, "Transaction complete.");
transactionsCounter.add(1, {"app.payment.currency": currencyCode})
return { transactionId }
}
}
21 changes: 21 additions & 0 deletions src/paymentservice/grpc-ff-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let grpc = require('@grpc/grpc-js');
let protoLoader = require('@grpc/proto-loader');
let protoDescriptor = grpc.loadPackageDefinition(protoLoader.loadSync('demo.proto'));
let client = new protoDescriptor.oteldemo.FeatureFlagService (
process.env.FEATURE_FLAG_GRPC_SERVICE_ADDR,
grpc.credentials.createInsecure()
);

module.exports.getFeatureFlagEnabled = async featureFlag => {
try {
const response = await new Promise((resolve, reject) => {
client.getFlag({ name: featureFlag }, (error, response) => {
error ? reject(error) : resolve(response);
});
});

return response.flag?.enabled || false;
} catch (error) {
logger.error(`Error getting FeatureFlag: ${error}`);
}
}
12 changes: 9 additions & 3 deletions src/paymentservice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ const opentelemetry = require('@opentelemetry/api')
const charge = require('./charge')
const logger = require('./logger')

function chargeServiceHandler(call, callback) {
const { getFeatureFlagEnabled } = require('./grpc-ff-client')

async function chargeServiceHandler(call, callback) {
const span = opentelemetry.trace.getActiveSpan();

try {
if (await getFeatureFlagEnabled("paymentServiceUnreachable")) {
throw new Error("PaymentService Unavailable Feature Flag Enabled")
}

const amount = call.request.amount
span.setAttributes({
'app.payment.amount': parseFloat(`${amount.units}.${amount.nanos}`)
})
logger.info({ request: call.request }, "Charge request received.")

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

} catch (err) {
Expand Down Expand Up @@ -56,4 +62,4 @@ server.bindAsync(`0.0.0.0:${process.env['PAYMENT_SERVICE_PORT']}`, grpc.ServerCr
)

process.once('SIGINT', closeGracefully)
process.once('SIGTERM', closeGracefully)
process.once('SIGTERM', closeGracefully)

0 comments on commit 283dca6

Please sign in to comment.