@netlify/opentelemetry-utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2020 Netlify <team@netlify.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Opentelemetry Utils
2
+
3
+ This package extracts utility methods to interact with open telemetry across our JS packages. It's a thin wrapper around
4
+ the OTEL API and is built strictly so that no other `@opentelemetry` dependency is **required besides
5
+ `@opentelemetry/api`.**
package/lib/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { Context } from '@opentelemetry/api';
2
+ /**
3
+ * Sets attributes to be propagated across child spans under the current active context
4
+ */
5
+ export declare const setMultiSpanAttributes: (attributes: {
6
+ [key: string]: string;
7
+ }) => Context;
8
+ /**
9
+ * Add error information to the current active span (if any). Optionally sets the provided attributes on the span too.
10
+ */
11
+ export declare const addErrorToActiveSpan: (error: Error, attributes?: {
12
+ [key: string]: string;
13
+ } | undefined) => void;
14
+ /**
15
+ * Creates a specific event to the current active span (if any)
16
+ */
17
+ export declare const addEventToActiveSpan: (eventName: string, attributes?: {
18
+ [key: string]: string;
19
+ } | undefined) => void;
20
+ /**
21
+ * Sets global context to be used when initialising our root span
22
+ */
23
+ export declare const setGlobalContext: (ctx: Context) => void;
24
+ /**
25
+ * Gets the global context to be used when initialising our root span
26
+ */
27
+ export declare const getGlobalContext: () => Context;
package/lib/index.js ADDED
@@ -0,0 +1,53 @@
1
+ import { context, propagation, trace, SpanStatusCode } from '@opentelemetry/api';
2
+ /**
3
+ * Sets attributes to be propagated across child spans under the current active context
4
+ */
5
+ export const setMultiSpanAttributes = function (attributes) {
6
+ const currentBaggage = propagation.getBaggage(context.active());
7
+ // Create a baggage if there's none
8
+ let baggage = currentBaggage === undefined ? propagation.createBaggage() : currentBaggage;
9
+ Object.entries(attributes).forEach(([key, value]) => {
10
+ baggage = baggage.setEntry(key, { value });
11
+ });
12
+ return propagation.setBaggage(context.active(), baggage);
13
+ };
14
+ /**
15
+ * Add error information to the current active span (if any). Optionally sets the provided attributes on the span too.
16
+ */
17
+ export const addErrorToActiveSpan = function (error, attributes) {
18
+ const span = trace.getActiveSpan();
19
+ if (!span)
20
+ return;
21
+ if (attributes !== undefined) {
22
+ span.setAttributes(attributes);
23
+ }
24
+ span.recordException(error);
25
+ span.setStatus({
26
+ code: SpanStatusCode.ERROR,
27
+ message: error.message,
28
+ });
29
+ };
30
+ /**
31
+ * Creates a specific event to the current active span (if any)
32
+ */
33
+ export const addEventToActiveSpan = function (eventName, attributes) {
34
+ const span = trace.getActiveSpan();
35
+ if (!span)
36
+ return;
37
+ span.addEvent(eventName, attributes);
38
+ };
39
+ /**
40
+ * Sets global context to be used when initialising our root span
41
+ */
42
+ export const setGlobalContext = function (ctx) {
43
+ global['NETLIFY_GLOBAL_CONTEXT'] = ctx;
44
+ };
45
+ /**
46
+ * Gets the global context to be used when initialising our root span
47
+ */
48
+ export const getGlobalContext = function () {
49
+ if (global['NETLIFY_GLOBAL_CONTEXT'] === undefined) {
50
+ return context.active();
51
+ }
52
+ return global['NETLIFY_GLOBAL_CONTEXT'];
53
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@netlify/opentelemetry-utils",
3
+ "version": "1.0.0",
4
+ "description": "Opentelemetry utility methods",
5
+ "type": "module",
6
+ "exports": "./lib/index.js",
7
+ "main": "./lib/index.js",
8
+ "types": "./lib/index.d.ts",
9
+ "files": [
10
+ "lib/**/*"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "build:logos": "vite build",
15
+ "test": "vitest run",
16
+ "test:dev": "vitest --ui",
17
+ "test:ci": "vitest run --reporter=default"
18
+ },
19
+ "keywords": [],
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/netlify/build.git",
24
+ "directory": "packages/opentelemetry-utils"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/netlify/build/issues"
28
+ },
29
+ "author": "Netlify Inc.",
30
+ "dependencies": {
31
+ "@opentelemetry/api": "~1.6.0"
32
+ },
33
+ "devDependencies": {
34
+ "@opentelemetry/sdk-trace-base": "^1.18.1",
35
+ "@opentelemetry/sdk-trace-node": "^1.18.1",
36
+ "@types/node": "^14.18.53",
37
+ "@vitest/coverage-c8": "^0.30.1",
38
+ "@vitest/ui": "^0.30.1",
39
+ "typescript": "^5.0.0",
40
+ "vite": "^4.0.4",
41
+ "vitest": "^0.30.1"
42
+ },
43
+ "engines": {
44
+ "node": ">=18.0.0"
45
+ },
46
+ "gitHead": "8ebf95ca5a1590da72fdffb67e966e02efd4d379"
47
+ }