Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/804integrate event framework #83

Merged
merged 9 commits into from
Aug 29, 2019
837 changes: 803 additions & 34 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mojaloop/central-services-shared",
"version": "7.4.5",
"version": "7.4.6",
"description": "Shared code for central services",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@mojaloop/central-services-error-handling": "7.4.0",
"@mojaloop/central-services-stream": "6.2.2",
"@mojaloop/event-sdk": "7.4.0",
"axios": "0.19.0",
"catbox": "10.0.6",
"catbox-memory": "4.0.1",
Expand Down
4 changes: 3 additions & 1 deletion src/enums/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ const Http = require('./http')
const Transfers = require('./transfers')
const Events = require('./events')
const Kafka = require('./kafka')
const Tags = require('./tags')

module.exports = {
Accounts,
EndPoints,
Events,
Http,
Transfers,
Kafka
Kafka,
Tags
}
31 changes: 31 additions & 0 deletions src/enums/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Gates Foundation organization for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Gates Foundation
- Name Surname <[email protected]>

* Neal Donnan <[email protected]>
--------------
******/

const RouteTags = {
SAMPLED: 'sampled'
}

module.exports = {
RouteTags
}
4 changes: 3 additions & 1 deletion src/util/hapi/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const HapiRawPayload = require('./plugins/rawPayloadToDataUri')
const HapiEventPlugin = require('./plugins/eventPlugin')

module.exports = {
HapiRawPayload
HapiRawPayload,
HapiEventPlugin
}
78 changes: 78 additions & 0 deletions src/util/hapi/plugins/eventPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*****
License
--------------
Copyright © 2017 Bill & Melinda Gates Foundation
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Gates Foundation organization for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Gates Foundation
* Name Surname <[email protected]>

* Neal Donnan <[email protected]>
--------------
******/
'use strict'

const EventSdk = require('@mojaloop/event-sdk')
const Logger = require('../../../logger')
const Enum = require('../../../enums')

const onPreAuth = (request, reply) => {
if (request && request.route && request.route.settings && request.route.settings.tags && request.route.settings.tags.includes(Enum.Tags.RouteTags.SAMPLED)) {
const context = EventSdk.Tracer.extractContextFromHttpRequest(request)
const spanName = request.route.settings.id
let span
if (context) {
span = EventSdk.Tracer.createChildSpanFromContext(spanName, context)
} else {
Logger.debug(`Starting parent span ${spanName}`)
span = EventSdk.Tracer.createSpan(spanName)
}
reply.request.span = span
}
return reply.continue
}

const onPreResponse = (request, reply) => {
const span = request.span
const response = request.response
if (span) {
if (response instanceof Error || response.isBoom) {
let state
if (response.output.payload.errorInformation && response.output.payload.errorInformation.errorCode) {
state = new EventSdk.EventStateMetadata(EventSdk.EventStatusType.failed, response.output.payload.errorInformation.errorCode, response.output.payload.errorInformation.errorDescription)
} else {
state = new EventSdk.EventStateMetadata(EventSdk.EventStatusType.failed, response.output.statusCode, response.message)
}
span.error(response, state)
span.finish(response.message, state)
} else {
Logger.debug(`Finishing parent span ${span.spanContext.service}`)
span.finish()
}
}
return reply.continue
}

/**
* HAPI plugin to start and stop a parent span on API requests.
* In order to have a span created for a route the 'sampled' tag should be
* applied to the route
*/
module.exports.plugin = {
name: 'eventPlugin',
register: function (server) {
server.ext('onPreAuth', onPreAuth)
server.ext('onPreResponse', onPreResponse)
}
}
8 changes: 7 additions & 1 deletion src/util/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
******/
'use strict'

const EventSdk = require('@mojaloop/event-sdk')
const request = require('axios')
const Logger = require('../logger')
const Headers = require('./headers/transformer')
Expand All @@ -43,10 +44,11 @@ const MISSING_FUNCTION_PARAMETERS = 'Missing parameters for function'
* @param {string} destination id for which callback is being sent
* @param {object} payload the body of the request being sent
* @param {string} responseType the type of the response object
* @param {object} span a span for event logging if this request is within a span
*
*@return {object} The response for the request being sent or error object with response included
*/
const sendRequest = async (url, headers, source, destination, method = enums.Http.RestMethods.GET, payload = undefined, responseType = enums.Http.ResponseTypes.JSON) => {
const sendRequest = async (url, headers, source, destination, method = enums.Http.RestMethods.GET, payload = undefined, responseType = enums.Http.ResponseTypes.JSON, span = undefined) => {
let requestOptions
if (!url || !method || !headers || (method !== enums.Http.RestMethods.GET && !payload) || !source || !destination) {
throw ErrorHandler.Factory.createInternalServerFSPIOPError(MISSING_FUNCTION_PARAMETERS)
Expand All @@ -64,6 +66,10 @@ const sendRequest = async (url, headers, source, destination, method = enums.Htt
data: payload,
responseType
}
if (span) {
requestOptions = span.injectContextToHttpRequest(requestOptions)
span.audit(requestOptions, EventSdk.AuditEventAction.egress)
}
Logger.info(`sendRequest::request ${JSON.stringify(requestOptions)}`)
const response = await request(requestOptions)
Logger.info(`Success: sendRequest::response ${JSON.stringify(response, Object.getOwnPropertyNames(response))}`)
Expand Down
Loading