From 283dca65fecccc4d20cc1b8a8ee19f38e2550cc0 Mon Sep 17 00:00:00 2001 From: "markus.eisl" Date: Thu, 15 Feb 2024 11:15:21 +0100 Subject: [PATCH] [paymentservice] add paymentServiceFailure & paymentServiceUnreachable featureflags --- CHANGELOG.md | 2 ++ docker-compose.yml | 1 + kubernetes/opentelemetry-demo.yaml | 2 ++ src/ffspostgres/init-scripts/20-ffs_data.sql | 4 +++- src/paymentservice/charge.js | 10 ++++++++-- src/paymentservice/grpc-ff-client.js | 21 ++++++++++++++++++++ src/paymentservice/index.js | 12 ++++++++--- 7 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 src/paymentservice/grpc-ff-client.js diff --git a/CHANGELOG.md b/CHANGELOG.md index c4a00c9836..45116bfb02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 4eaabc52bd..275db4c106 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/kubernetes/opentelemetry-demo.yaml b/kubernetes/opentelemetry-demo.yaml index 54827c14bb..5ab9a2fb43 100644 --- a/kubernetes/opentelemetry-demo.yaml +++ b/kubernetes/opentelemetry-demo.yaml @@ -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: diff --git a/src/ffspostgres/init-scripts/20-ffs_data.sql b/src/ffspostgres/init-scripts/20-ffs_data.sql index 700e993236..e23f91bb3c 100644 --- a/src/ffspostgres/init-scripts/20-ffs_data.sql +++ b/src/ffspostgres/init-scripts/20-ffs_data.sql @@ -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); diff --git a/src/paymentservice/charge.js b/src/paymentservice/charge.js index 77e3c0990d..b476c3a380 100644 --- a/src/paymentservice/charge.js +++ b/src/paymentservice/charge.js @@ -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, @@ -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 } -} +} \ No newline at end of file diff --git a/src/paymentservice/grpc-ff-client.js b/src/paymentservice/grpc-ff-client.js new file mode 100644 index 0000000000..ffd384d978 --- /dev/null +++ b/src/paymentservice/grpc-ff-client.js @@ -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}`); + } +} \ No newline at end of file diff --git a/src/paymentservice/index.js b/src/paymentservice/index.js index 8f95e61fce..f392a2b51d 100644 --- a/src/paymentservice/index.js +++ b/src/paymentservice/index.js @@ -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) { @@ -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) \ No newline at end of file