Skip to content

Commit

Permalink
demo(cnl): edit payment service
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanrahic committed Sep 13, 2023
1 parent 908f112 commit b288376
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 82 deletions.
12 changes: 6 additions & 6 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports.charge = request => {
/**
* 3. Demo: Create a new active span without the need to pass a parent span
*/
// const span = tracer.startSpan('charge')
const span = tracer.startSpan('charge')

const {
creditCardNumber: number,
Expand All @@ -50,10 +50,10 @@ module.exports.charge = request => {
/**
* 1. Demo: Add span attributes and events for custom test specs
*/
// span.setAttributes({
// 'app.payment.card_type': cardType,
// 'app.payment.card_valid': valid
// })
span.setAttributes({
'app.payment.card_type': cardType,
'app.payment.card_valid': valid
})

if (!valid) {
throw new Error('Credit card info is invalid.')
Expand All @@ -79,7 +79,7 @@ module.exports.charge = request => {
* 1. Demo: Use the active span from context.
* End the span.
*/
// span.end()
span.end()

const { units, nanos, currencyCode } = request.amount
logger.info({transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }}, "Transaction complete.")
Expand Down
86 changes: 43 additions & 43 deletions src/paymentservice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,49 @@ function chargeServiceHandler(call, callback) {
/**
* 3. Demo: Create a new active span without the need to pass a parent span
*/
// return tracer.startActiveSpan('chargeServiceHandler', span => {...})

try {
const amount = call.request.amount

/**
* 1. Demo: Add span attributes and events for custom test specs
*/
// span.setAttributes({
// 'app.payment.amount': parseFloat(`${amount.units}.${amount.nanos}`)
// })
// span.addEvent('Charge request received.', {
// 'log.severity': 'info',
// 'log.message': 'Charge request received.',
// 'request': call.request,
// })

const response = charge.charge(call.request)

/**
* 1. Demo: Add span attributes and events for custom test specs
*/
// span.setStatus({ code: SpanStatusCode.OK })
// span.end()

callback(null, response)

} catch (err) {

/**
* 1. Demo: Add span attributes and events for custom test specs
*/
// span.addEvent('Charge request error.', {
// 'log.severity': 'warn',
// 'log.message': 'Charge request error.',
// 'error': err,
// })
// span.recordException(err)
// span.setStatus({ code: SpanStatusCode.ERROR })
// span.end()

callback(err)
}
return tracer.startActiveSpan('chargeServiceHandler', span => {
try {
const amount = call.request.amount

/**
* 1. Demo: Add span attributes and events for custom test specs
*/
span.setAttributes({
'app.payment.amount': parseFloat(`${amount.units}.${amount.nanos}`)
})
span.addEvent('Charge request received.', {
'log.severity': 'info',
'log.message': 'Charge request received.',
'request': call.request,
})

const response = charge.charge(call.request)

/**
* 1. Demo: Add span attributes and events for custom test specs
*/
span.setStatus({ code: SpanStatusCode.OK })
span.end()

callback(null, response)

} catch (err) {

/**
* 1. Demo: Add span attributes and events for custom test specs
*/
span.addEvent('Charge request error.', {
'log.severity': 'warn',
'log.message': 'Charge request error.',
'error': err,
})
span.recordException(err)
span.setStatus({ code: SpanStatusCode.ERROR })
span.end()

callback(err)
}
})
}

async function closeGracefully(signal) {
Expand Down
62 changes: 31 additions & 31 deletions src/paymentservice/opentelemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@
// SPDX-License-Identifier: Apache-2.0

// Auto Start
// const opentelemetry = require("@opentelemetry/sdk-node")
// const {getNodeAutoInstrumentations} = require("@opentelemetry/auto-instrumentations-node")
// const {OTLPTraceExporter} = require('@opentelemetry/exporter-trace-otlp-grpc')
// const {OTLPMetricExporter} = require('@opentelemetry/exporter-metrics-otlp-grpc')
// const {PeriodicExportingMetricReader} = require('@opentelemetry/sdk-metrics')
// const {alibabaCloudEcsDetector} = require('@opentelemetry/resource-detector-alibaba-cloud')
// const {awsEc2Detector, awsEksDetector} = require('@opentelemetry/resource-detector-aws')
// const {containerDetector} = require('@opentelemetry/resource-detector-container')
// const {gcpDetector} = require('@opentelemetry/resource-detector-gcp')
// const {envDetector, hostDetector, osDetector, processDetector} = require('@opentelemetry/resources')
const opentelemetry = require("@opentelemetry/sdk-node")
const {getNodeAutoInstrumentations} = require("@opentelemetry/auto-instrumentations-node")
const {OTLPTraceExporter} = require('@opentelemetry/exporter-trace-otlp-grpc')
const {OTLPMetricExporter} = require('@opentelemetry/exporter-metrics-otlp-grpc')
const {PeriodicExportingMetricReader} = require('@opentelemetry/sdk-metrics')
const {alibabaCloudEcsDetector} = require('@opentelemetry/resource-detector-alibaba-cloud')
const {awsEc2Detector, awsEksDetector} = require('@opentelemetry/resource-detector-aws')
const {containerDetector} = require('@opentelemetry/resource-detector-container')
const {gcpDetector} = require('@opentelemetry/resource-detector-gcp')
const {envDetector, hostDetector, osDetector, processDetector} = require('@opentelemetry/resources')

// const sdk = new opentelemetry.NodeSDK({
// traceExporter: new OTLPTraceExporter(),
// instrumentations: [
// getNodeAutoInstrumentations()
// ],
// metricReader: new PeriodicExportingMetricReader({
// exporter: new OTLPMetricExporter(),
// }),
// resourceDetectors: [
// containerDetector,
// envDetector,
// hostDetector,
// osDetector,
// processDetector,
// alibabaCloudEcsDetector,
// awsEksDetector,
// awsEc2Detector,
// gcpDetector
// ],
// })
// sdk.start()
const sdk = new opentelemetry.NodeSDK({
traceExporter: new OTLPTraceExporter(),
instrumentations: [
getNodeAutoInstrumentations()
],
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
}),
resourceDetectors: [
containerDetector,
envDetector,
hostDetector,
osDetector,
processDetector,
alibabaCloudEcsDetector,
awsEksDetector,
awsEc2Detector,
gcpDetector
],
})
sdk.start()
// Auto End

// Manual Start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ spec:
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: paymentservice:50051 # ${env:PAYMENT_SERVICE_ADDR}
protobufFile: ../../../../pb/demo.proto
address: paymentservice:50051
method: oteldemo.PaymentService.Charge
request: |-
{
Expand Down
4 changes: 4 additions & 0 deletions test/tracetesting/payment-service/valid-credit-card.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ spec:
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
assertions:
- attr:tracetest.response.body | json_path '$.transactionId' != ""
- name: It should return a valid credit card
selector: span[tracetest.span.type="general" name="charge"]
assertions:

Check failure on line 42 in test/tracetesting/payment-service/valid-credit-card.yaml

View workflow job for this annotation

GitHub Actions / yamllint

42:18 [trailing-spaces] trailing spaces
- attr:app.payment.card_valid = "true"

0 comments on commit b288376

Please sign in to comment.