From 9d0d49ee13bb66cbe543f1b83562ac11f425084c Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Fri, 14 May 2021 13:38:38 -0300 Subject: [PATCH 1/2] add AzureHttpTriggerPlugin --- README.md | 21 ++++ src/core/PluginInstaller.ts | 8 +- src/index.ts | 1 + src/plugins/AzureHttpTriggerPlugin.ts | 139 ++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 src/plugins/AzureHttpTriggerPlugin.ts diff --git a/README.md b/README.md index 8757c90..b63680c 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,27 @@ Library | Underlying Plugin Name | [`request-promise`](https://github.com/request/request-promise) | `http` / `https` | | [`koa`](https://github.com/koajs/koa) | `http` / `https` | +## Experimental Azure Functions Support + +The plugin `AzureHttpTriggerPlugin` provides a wrapper function for an Azure Functions Javascript HttpTrigger endpoint. This is an http server endpoint and it must be instrumented manually currently. So far all other plugins tested work within the HttpTrigger and so a trace can pass through the Function and on to other endpoints called by the function. How much sense it makes to instrument an Azure Function which already lives in the cloud and has robust monitoring incorporated is a good question, but at the least this plugin will allow those endpoints to show up in a Skywalking trace. + +### Usage: + +```javascript +const {default: agent, AzureHttpTriggerPlugin} = require('skywalking-backend-js'); + +agent.start({ ... }); + +module.exports = AzureHttpTriggerPlugin.wrap(async function (context, req) { + + /* contents of http trigger function */ + +}); +``` + +All that needs to be done is the actual trigger function needs to be wrapped with `azureHttpTriggerPlugin.wrap()`, whether that function is a default export or an explicitly named `entryPoint` or `run` or `index`. + + ## Contact Us * Submit [an issue](https://github.com/apache/skywalking/issues/new) by using [Nodejs] as title prefix. * Mail list: **dev@skywalking.apache.org**. Mail to `dev-subscribe@skywalking.apache.org`, follow the reply to subscribe the mail list. diff --git a/src/core/PluginInstaller.ts b/src/core/PluginInstaller.ts index 10d4bc6..7168335 100644 --- a/src/core/PluginInstaller.ts +++ b/src/core/PluginInstaller.ts @@ -49,7 +49,7 @@ export default class PluginInstaller { private checkModuleVersion = (plugin: SwPlugin): { version: string; isSupported: boolean } => { try { - if (this.isBuiltIn(plugin.module)) { + if (plugin.versions === '!' || this.isBuiltIn(plugin.module)) { return { version: '*', isSupported: true, @@ -101,7 +101,11 @@ export default class PluginInstaller { return; } - logger.info(`Installing plugin ${plugin.module} ${plugin.versions}`); + if (plugin.versions === '!') { + logger.info(`Explicit instrumentation plugin ${plugin.module} available`); + } else { + logger.info(`Installing plugin ${plugin.module} ${plugin.versions}`); + } plugin.install(this); } catch (e) { diff --git a/src/index.ts b/src/index.ts index 3f455b8..8207eb1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,3 +53,4 @@ class Agent { export default new Agent(); export { default as ContextManager } from './trace/context/ContextManager'; +export { default as AzureHttpTriggerPlugin } from './plugins/AzureHttpTriggerPlugin'; diff --git a/src/plugins/AzureHttpTriggerPlugin.ts b/src/plugins/AzureHttpTriggerPlugin.ts new file mode 100644 index 0000000..1c0f285 --- /dev/null +++ b/src/plugins/AzureHttpTriggerPlugin.ts @@ -0,0 +1,139 @@ +/*! + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file 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, software + * distributed under the License is 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. + * + */ + +import SwPlugin from '../core/SwPlugin'; +import { URL } from 'url'; +import ContextManager from '../trace/context/ContextManager'; +import { Component } from '../trace/Component'; +import Tag from '../Tag'; +import { SpanLayer } from '../proto/language-agent/Tracing_pb'; +import { ContextCarrier } from '../trace/context/ContextCarrier'; +import DummySpan from '../trace/span/DummySpan'; +import { ignoreHttpMethodCheck } from '../config/AgentConfig'; + +class AzureHttpTriggerPlugin implements SwPlugin { + readonly module = 'azureHttpTrigger'; + readonly versions = '!'; + + install(): void { /* linter kibble */ } + + wrap(func: any) { + return function(this: any, context: any) { + let outRet = true; + let outName: any; + + for (const def of context.bindingDefinitions || []) { + if (def.type === 'http' && def.directioun === 'out') { + outName = def.name; + outRet = outName === '$return' + } + } + + const req = context.req; + const url = new URL(req.url); + const operation = url.pathname.replace(/\?.*$/g, ''); + const carrier = ContextCarrier.from(req.headers); + + const span: any = ignoreHttpMethodCheck(req.method) + ? DummySpan.create() + : ContextManager.current.newEntrySpan(operation, carrier); + + span.layer = SpanLayer.HTTP; + span.component = Component.HTTP_SERVER; + span.peer = (req.headers['x-forwarded-for'] || '???').split(',').shift(); + + span.tag(Tag.httpMethod(req.method)); + span.tag(Tag.httpURL(url.origin + url.pathname)); + + span.start(); + + let stop = (_ret?: any) => { + stop = (v: any) => v; + + const res = outRet ? _ret : context.bindings[outName] || context.res; + const status = span.errored ? 500 : !res ? 0 : res.status || 200; + + if (status) { + span.tag(Tag.httpStatusCode(status)); + + if (status >= 400) + span.errored = true; + } + + span.stop(); + + return _ret; + }; + + const done = context.done; + let did = false; + + context.done = function(err: any, _ret: any) { + if (!did) { + if (err) + span.error(err); + + if (arguments.length >= 2) + arguments[1] = stop(_ret); + else + stop(); + + did = true; + } + + return done.apply(this, arguments); + }; + + let ret; + + try { + ret = func.apply(this, arguments); + + } catch (err) { + span.error(err); + stop(); + + throw err; + } + + if (ret && typeof ret.then === 'function') { // generic Promise check + ret = ret.then( + (_ret: any) => { + + return stop(_ret); + }, + + (err: any) => { + span.error(err); + stop(); + + return Promise.reject(err); + } + ); + } + + span.async(); + + return ret; + }; + } +} + +// noinspection JSUnusedGlobalSymbols +export default new AzureHttpTriggerPlugin(); From a7cd7ea72ebcb1b7248d37e030ec72cc19c59ee3 Mon Sep 17 00:00:00 2001 From: Tomasz Pytel Date: Sat, 15 May 2021 13:17:52 -0300 Subject: [PATCH 2/2] component ID, azure in separate dir --- src/{plugins => azure}/AzureHttpTriggerPlugin.ts | 10 ++-------- src/core/PluginInstaller.ts | 8 ++------ src/index.ts | 2 +- src/trace/Component.ts | 1 + 4 files changed, 6 insertions(+), 15 deletions(-) rename src/{plugins => azure}/AzureHttpTriggerPlugin.ts (93%) diff --git a/src/plugins/AzureHttpTriggerPlugin.ts b/src/azure/AzureHttpTriggerPlugin.ts similarity index 93% rename from src/plugins/AzureHttpTriggerPlugin.ts rename to src/azure/AzureHttpTriggerPlugin.ts index 1c0f285..30dafad 100644 --- a/src/plugins/AzureHttpTriggerPlugin.ts +++ b/src/azure/AzureHttpTriggerPlugin.ts @@ -17,7 +17,6 @@ * */ -import SwPlugin from '../core/SwPlugin'; import { URL } from 'url'; import ContextManager from '../trace/context/ContextManager'; import { Component } from '../trace/Component'; @@ -27,12 +26,7 @@ import { ContextCarrier } from '../trace/context/ContextCarrier'; import DummySpan from '../trace/span/DummySpan'; import { ignoreHttpMethodCheck } from '../config/AgentConfig'; -class AzureHttpTriggerPlugin implements SwPlugin { - readonly module = 'azureHttpTrigger'; - readonly versions = '!'; - - install(): void { /* linter kibble */ } - +class AzureHttpTriggerPlugin { wrap(func: any) { return function(this: any, context: any) { let outRet = true; @@ -55,7 +49,7 @@ class AzureHttpTriggerPlugin implements SwPlugin { : ContextManager.current.newEntrySpan(operation, carrier); span.layer = SpanLayer.HTTP; - span.component = Component.HTTP_SERVER; + span.component = Component.AZURE_HTTPTRIGGER ; span.peer = (req.headers['x-forwarded-for'] || '???').split(',').shift(); span.tag(Tag.httpMethod(req.method)); diff --git a/src/core/PluginInstaller.ts b/src/core/PluginInstaller.ts index 7168335..10d4bc6 100644 --- a/src/core/PluginInstaller.ts +++ b/src/core/PluginInstaller.ts @@ -49,7 +49,7 @@ export default class PluginInstaller { private checkModuleVersion = (plugin: SwPlugin): { version: string; isSupported: boolean } => { try { - if (plugin.versions === '!' || this.isBuiltIn(plugin.module)) { + if (this.isBuiltIn(plugin.module)) { return { version: '*', isSupported: true, @@ -101,11 +101,7 @@ export default class PluginInstaller { return; } - if (plugin.versions === '!') { - logger.info(`Explicit instrumentation plugin ${plugin.module} available`); - } else { - logger.info(`Installing plugin ${plugin.module} ${plugin.versions}`); - } + logger.info(`Installing plugin ${plugin.module} ${plugin.versions}`); plugin.install(this); } catch (e) { diff --git a/src/index.ts b/src/index.ts index 8207eb1..7d9371b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,4 +53,4 @@ class Agent { export default new Agent(); export { default as ContextManager } from './trace/context/ContextManager'; -export { default as AzureHttpTriggerPlugin } from './plugins/AzureHttpTriggerPlugin'; +export { default as AzureHttpTriggerPlugin } from './azure/AzureHttpTriggerPlugin'; diff --git a/src/trace/Component.ts b/src/trace/Component.ts index 64d47bf..f45bf0d 100644 --- a/src/trace/Component.ts +++ b/src/trace/Component.ts @@ -26,6 +26,7 @@ export class Component { static readonly HTTP_SERVER = new Component(49); static readonly RABBITMQ_PRODUCER = new Component(52); static readonly RABBITMQ_CONSUMER = new Component(53); + static readonly AZURE_HTTPTRIGGER = new Component(111); static readonly EXPRESS = new Component(4002); static readonly AXIOS = new Component(4005); static readonly MONGOOSE = new Component(4006);