@serverless/sdk
Advanced tools
Comparing version 0.2.4 to 0.3.0
@@ -1,4 +0,2 @@ | ||
import TraceSpan from './lib/trace-span'; | ||
import ExpressAppInstrument from './instrumentation/express-app'; | ||
import sdk from './../index'; | ||
@@ -17,13 +15,2 @@ export interface TraceSpans {} | ||
instrumentation: Instrumentation; | ||
createTraceSpan( | ||
name: string, | ||
options?: { | ||
startTime?: bigint; | ||
immediateDescendants?: string[]; | ||
tags?: Record<string, boolean | number | string | Date | Array<unknown> | null>; | ||
input?: string; | ||
output?: string; | ||
onCloseByRoot?: Function; | ||
} | ||
): TraceSpan; | ||
captureError( | ||
@@ -53,3 +40,3 @@ error: Error, | ||
} | ||
declare const sdk: Sdk; | ||
export default sdk; |
@@ -13,5 +13,2 @@ import Tags from './tags'; | ||
close(): TraceSpan; | ||
closeContext(): undefined; | ||
destroy(): undefined; | ||
toJSON(): Object; | ||
@@ -18,0 +15,0 @@ } |
@@ -5,2 +5,20 @@ # Changelog | ||
## [0.3.0](https://github.com/serverless/console/compare/@serverless/sdk@0.2.4...@serverless/sdk@0.3.0) (2023-01-12) | ||
### ⚠ BREAKING CHANGES | ||
- `sdk.createTraceSpan` is removed. The functionality of creating custom spans is not considered part of public API at the time being (a new method for that will be introduced in one of the following releases) | ||
### Features | ||
- Make `createTraceSpan` private ([017ef10](https://github.com/serverless/console/commit/017ef109c1f6ad23adb6b020121b41a4ca539d35)) | ||
- Dedicated handling for structured warnings issued by the SDK ([1d38f5f](https://github.com/serverless/console/commit/1d38f5f59e435139ad7fac3c27f30e861cc3fdf2)) | ||
- Relax tag name format restriction ([414108f](https://github.com/serverless/console/commit/414108f57dbeb7189c000fb20bc0ac177e85bbfb)) | ||
- Log error instead of throwing on invalid capturedEvent user input ([b958948](https://github.com/serverless/console/commit/b958948fa7396a3e26a16606b2bcd98c652bb4fa)) | ||
### Maintenance Improvements | ||
- Fixed typings file ([#369](https://github.com/serverless/console/issues/369)) ([2deeb4b](https://github.com/serverless/console/commit/2deeb4b49b196eadde6ca85fc87f7fb9c00f453c)) | ||
- Revert problematic TS addition ([ad88aab](https://github.com/serverless/console/commit/ad88aab5c2ff39429ddbe56804c34192e1b3b4d0)) | ||
### [0.2.4](https://github.com/serverless/console/compare/@serverless/sdk@0.2.3...@serverless/sdk@0.2.4) (2023-01-11) | ||
@@ -7,0 +25,0 @@ |
@@ -15,14 +15,2 @@ # Serverless SDK | ||
### `.createTraceSpan(name[, options])` | ||
Create custom trade span | ||
- `name` - Name of the span | ||
- `options` - Optional setup: | ||
- `startTime` _(bigInt)_ - Externally recorded span _start time_. If not provided, it's resolved automatically on span creation. It cannot be set in a future, and must not be set before `startTime` of the currently ongoing span | ||
- `immediateDescendants` _([...string])_ - If intention is to create sub span descenant at once, names of those spans can be passed with this option. Descendant spans will be created automatically and will share same `startTime` as top sub span | ||
- `onCloseByRoot` _(function)_ - If provided, it'll be invoked if span will be autoclosed when closing the invocation trace. Useful for reporting errors in such scenarios | ||
Returns instance of [TraceSpan](trace-span.md) | ||
### `.captureError(error[, options])` | ||
@@ -34,3 +22,3 @@ | ||
- `options`: | ||
- `tags` _(object)_ - User tags object. Tag names can contain only lowercase alphanumeric tokens separated with dot. Values can be _string_, _boolean_, _number_, Date or Array containing any values of prior listed types | ||
- `tags` _(object)_ - User tags object. Tag names can contain alphanumeric (both lower and upper case), `-`, `_` and `.` characters. Values can be _string_, _boolean_, _number_, Date or Array containing any values of prior listed types | ||
- `fingerprint` _(string)_ - Console UI groups common errors by the _fingerprint_, which by default is derived from the error stack trace. This can be overriden by passing custom `fingeprint` value | ||
@@ -44,4 +32,3 @@ | ||
- `options`: | ||
- `tags` _(object)_ - User tags object. Tag names can contain only lowercase alphanumeric tokens separated with dot. Values can be _string_, _boolean_, _number_, Date or Array containing any values of prior listed types | ||
- `tags` _(object)_ - User tags object. Tag names can contain alphanumeric (both lower and upper case), `-`, `_` and `.` characters. Values can be _string_, _boolean_, _number_, Date or Array containing any values of prior listed types | ||
- `fingerprint` _(string)_ - Console UI groups common warnings by the _fingerprint_, which by default is derived from its message. This can be overriden by passing custom `fingeprint` value |
# TraceSpan | ||
_New trace span can be created via [`serverlessSdk.createTraceSpan`](./sdk.md#serverlesssdkcreatetracespanname-options)._ | ||
## Properties | ||
@@ -53,41 +51,4 @@ | ||
### `close([options])` | ||
Closes span and all sub spans. | ||
Supported options: | ||
- `endTime` - Externally recorded _end time_. If not provided, it's resolved automatically. Cannot be earlier than `startTime` and cannot be set in a future | ||
### `closeContext` | ||
After creating a span, this needs to be called if, after a certain point, we do not want to attribute the following logic to the span context | ||
e.g.: | ||
```javascript | ||
const fooSpan = serverlessSdk.createTraceSpan('foo'); | ||
runFoo(); | ||
// Ensure "bar" trace span is not a sub span of "foo" | ||
fooSpan.closeContext(); | ||
const barSpan = serverlessSdk.createTraceSpan('bar'); | ||
runBar(); | ||
``` | ||
### `destroy()` | ||
Permanently removes the span from the trace. Useful if we created span, but logic it was supposed to describe failed to initialize,, e.g.: | ||
```javascript | ||
const fooSpan = serverlessSdk.createTraceSpan('foo'); | ||
try { | ||
runFoo().finally(() => fooSpan.close()); | ||
} catch (error) { | ||
fooSpan.destroy(); | ||
throw error; | ||
} | ||
``` | ||
#### `toJSON()` | ||
Returns JSON representation of the span |
@@ -34,3 +34,2 @@ 'use strict'; | ||
); | ||
serverlessSdk.createTraceSpan = (name, options = {}) => new TraceSpan(name, options); | ||
serverlessSdk.captureError = (error, options = {}) => { | ||
@@ -94,2 +93,3 @@ createErrorCapturedEvent(error, options); | ||
serverlessSdk._createTraceSpan = (name, options = {}) => new TraceSpan(name, options); | ||
serverlessSdk._isDebugMode = Boolean(process.env.SLS_SDK_DEBUG); | ||
@@ -96,0 +96,0 @@ serverlessSdk._debugLog = (...args) => { |
@@ -17,2 +17,3 @@ 'use strict'; | ||
const TraceSpan = require('./trace-span'); | ||
const ServerlessSdkError = require('./error'); | ||
@@ -43,7 +44,12 @@ class CapturedEvent { | ||
}); | ||
if (customTags) this.customTags.setMany(customTags); | ||
this.customFingerprint = ensureString(options.customFingerprint, { | ||
isOptional: true, | ||
name: 'options.fingerprint', | ||
}); | ||
try { | ||
if (customTags) this.customTags.setMany(customTags); | ||
this.customFingerprint = ensureString(options.customFingerprint, { | ||
isOptional: true, | ||
name: 'options.fingerprint', | ||
Error: ServerlessSdkError, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
if (options._origin) this._origin = options._origin; | ||
@@ -50,0 +56,0 @@ this.traceSpan = TraceSpan.resolveCurrentSpan(); |
@@ -16,5 +16,5 @@ 'use strict'; | ||
customFingerprint: options.fingerprint, | ||
tags: { 'warning.message': message }, | ||
tags: { 'warning.message': message, 'warning.type': options.type || 1 }, | ||
_origin: options._origin, | ||
}); | ||
}; |
@@ -20,3 +20,3 @@ 'use strict'; | ||
if (!expressSpansMap.has(req)) { | ||
const expressSpan = serverlessSdk.createTraceSpan('express'); | ||
const expressSpan = serverlessSdk._createTraceSpan('express'); | ||
const openedSpans = new Set(); | ||
@@ -55,3 +55,3 @@ const expressRouteData = { expressSpan, openedSpans }; | ||
})(); | ||
const middlewareSpan = serverlessSdk.createTraceSpan(middlewareSpanName); | ||
const middlewareSpan = serverlessSdk._createTraceSpan(middlewareSpanName); | ||
openedSpans.add(middlewareSpan); | ||
@@ -82,3 +82,3 @@ if (this.path && (!expressRouteData.path || expressRouteData.path.length < this.path.length)) { | ||
const { openedSpans } = expressSpansMap.get(req); | ||
const middlewareSpan = serverlessSdk.createTraceSpan( | ||
const middlewareSpan = serverlessSdk._createTraceSpan( | ||
`express.middleware.error.${generateMiddlewareName(this.name) || 'unknown'}` | ||
@@ -85,0 +85,0 @@ ); |
@@ -157,3 +157,3 @@ 'use strict'; | ||
const traceSpan = serverlessSdk.createTraceSpan(`node.${protocol}.request`, { | ||
const traceSpan = serverlessSdk._createTraceSpan(`node.${protocol}.request`, { | ||
startTime, | ||
@@ -160,0 +160,0 @@ onCloseByRoot: () => { |
'use strict'; | ||
const isError = require('type/error/is'); | ||
const isPlainObject = require('type/plain-object/is'); | ||
const util = require('util'); | ||
@@ -40,3 +41,7 @@ const createErrorCapturedEvent = require('../create-error-captured-event'); | ||
original.warn.apply(this, args); | ||
createWarningCapturedEvent(resolveWarnMesssage(args), { _origin: 'nodeConsole' }); | ||
if (isPlainObject(args[0]) && args[0].source === 'serverlessSdk') { | ||
createWarningCapturedEvent(args[0].message, { _origin: 'nodeConsole', type: 2 }); | ||
} else { | ||
createWarningCapturedEvent(resolveWarnMesssage(args), { _origin: 'nodeConsole' }); | ||
} | ||
}; | ||
@@ -43,0 +48,0 @@ |
@@ -10,6 +10,5 @@ 'use strict'; | ||
const capitalize = require('ext/string_/capitalize'); | ||
const ServerlessSdkError = require('./error'); | ||
const isValidTagName = RegExp.prototype.test.bind( | ||
/^[a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*(?:\.[a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*)*$/ | ||
); | ||
const isValidTagName = RegExp.prototype.test.bind(/^[a-zA-Z0-9_.-]+$/); | ||
@@ -22,2 +21,3 @@ const ensureTagName = (() => { | ||
errorMessage: `Invalid trace span tag ${propertyName}: Expected string, received "%v"`, | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -27,7 +27,6 @@ if (isValidTagName(value)) return value; | ||
errorCode, | ||
errorMessage: | ||
`Invalid trace span tag ${propertyName}: ${capitalize.call( | ||
propertyName | ||
)} should contain dot separated tokens that follow ` + | ||
'"[a-z][a-z0-9_]*" pattern. Received "%v"', | ||
errorMessage: `Invalid trace span tag ${propertyName}: ${capitalize.call( | ||
propertyName | ||
)}. Only alphanumeric, '-', '_' and '.' characters are allowed. Received "%v"`, | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -47,2 +46,3 @@ }; | ||
'Number must be finite, received "%v"', | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -66,2 +66,3 @@ } | ||
'Number must be finite, received "%v"', | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -78,2 +79,3 @@ } | ||
'Unrecognized value type in array:"%v"', | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -86,2 +88,3 @@ } | ||
'Array cannot have mixed type values:"%v"', | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -95,2 +98,3 @@ }); | ||
'Value must be either boolean, number, string, date or array of same values. Received "%v"', | ||
Error: ServerlessSdkError, | ||
}); | ||
@@ -118,5 +122,8 @@ }; | ||
} | ||
throw Object.assign(new Error(`Cannot set tag: Tag "${inputName}" is already set`), { | ||
code: 'DUPLICATE_TRACE_SPAN_TAG_NAME', | ||
}); | ||
throw Object.assign( | ||
new ServerlessSdkError(`Cannot set tag: Tag "${inputName}" is already set`), | ||
{ | ||
code: 'DUPLICATE_TRACE_SPAN_TAG_NAME', | ||
} | ||
); | ||
} | ||
@@ -126,11 +133,24 @@ return super.set(name, value); | ||
setMany(tags, options = {}) { | ||
ensurePlainObject(tags, { name: 'tags' }); | ||
ensurePlainObject(tags, { name: 'tags', Error: ServerlessSdkError }); | ||
if (!isObject(options)) options = {}; | ||
const prefix = ensureString(options.prefix, { isOptional: true, name: 'options.prefix' }); | ||
const prefix = ensureString(options.prefix, { | ||
isOptional: true, | ||
name: 'options.prefix', | ||
Error: ServerlessSdkError, | ||
}); | ||
if (prefix) ensureTagName(prefix, 'prefix'); | ||
const errors = []; | ||
for (const [name, value] of Object.entries(tags)) { | ||
if (value == null) continue; | ||
this.set(`${prefix ? `${prefix}.` : ''}${name}`, value); | ||
try { | ||
this.set(`${prefix ? `${prefix}.` : ''}${name}`, value); | ||
} catch (error) { | ||
errors.push(error); | ||
} | ||
} | ||
return this; | ||
if (!errors.length) return this; | ||
if (errors.length === 1) throw errors[0]; | ||
throw new ServerlessSdkError( | ||
`Cannot set Tags:\n\t- ${errors.map(({ message }) => message).join('\n\t- ')}` | ||
); | ||
} | ||
@@ -137,0 +157,0 @@ toJSON() { |
{ | ||
"name": "@serverless/sdk", | ||
"repository": "serverless/console", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"author": "Serverless, Inc.", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
@@ -29,5 +29,3 @@ # @serverless/sdk | ||
// ... | ||
const customTraceSpan = serverlessSdk.createTraceSpan('custom.tracespan'); | ||
// ... | ||
customTraceSpan.close(); | ||
serverlessSdk.captureError(new Error('Unexpected')); | ||
``` | ||
@@ -41,5 +39,3 @@ | ||
// ... | ||
const customTraceSpan = serverlessSdk.createTraceSpan('custom.tracespan'); | ||
// ... | ||
customTraceSpan.close(); | ||
serverlessSdk.captureError(new Error('Unexpected')); | ||
``` | ||
@@ -46,0 +42,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1111
55469
86