Skip to content

Commit

Permalink
demo(cnl): updated samples for livestream
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanrahic committed Sep 12, 2023
1 parent ac4405b commit 9a05ade
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 56 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ services:
interval: 1s
timeout: 3s
retries: 60
environment:
TRACETEST_DEV: ${TRACETEST_DEV}

tracetest-postgres:
image: postgres:14
Expand Down
35 changes: 18 additions & 17 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ module.exports.charge = request => {
* Create a context and pass the parent span as a param when creating a new span
*/
// const parent = trace.getActiveSpan()
// const ctx = trace.setSpan(
// context.active(),
// parent,
// )
// const ctx = trace.setSpan(context.active(), parent)
// const span = tracer.startSpan('charge', undefined, ctx)

/**
* 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 @@ -51,12 +48,12 @@ module.exports.charge = request => {
const { card_type: cardType, valid } = card.getCardDetails()

/**
* 4. Demo: Add span attributes and events for custom test specs
* 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 @@ -71,14 +68,18 @@ module.exports.charge = request => {
}

// 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") {
span.setAttribute('app.payment.charged', false)
} else {
span.setAttribute('app.payment.charged', true)
}
// const baggage = propagation.getBaggage(context.active())
// 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()
/**
* 1. Demo: Use the active span from context.
* End the span.
*/
// span.end()

const { units, nanos, currencyCode } = request.amount
logger.info({transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }}, "Transaction complete.")
Expand Down
80 changes: 43 additions & 37 deletions src/paymentservice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const grpc = require('@grpc/grpc-js')
const protoLoader = require('@grpc/proto-loader')
const health = require('grpc-js-health-check')
const { trace, context, SpanStatusCode } = require('@opentelemetry/api')

const tracer = trace.getTracer('paymentservice')

const charge = require('./charge')
Expand All @@ -31,42 +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

/**
* 4. 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)

span.setStatus({ code: SpanStatusCode.OK })
span.end()
callback(null, response)

} catch (err) {
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
1 change: 0 additions & 1 deletion src/paymentservice/opentelemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const sdk = new opentelemetry.NodeSDK({
gcpDetector
],
})

sdk.start()
// Auto End

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
method: oteldemo.CartService.GetCart
request: |-
{
"userId": "1234"
"userId": "1234"
}
specs:
- name: It retrieved the cart items correctly
Expand Down

0 comments on commit 9a05ade

Please sign in to comment.