Skip to content

Commit 1ad66a1

Browse files
authored
Add a simple version of the function for basic usage. (#49)
* Add simple version of call to add segment * Update readme
1 parent 58bac88 commit 1ad66a1

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@
33
[![NPM](https://nodei.co/npm/aws-xray-lambda-promise-subsegment.png?compact=true)](https://npmjs.org/package/aws-xray-lambda-promise-subsegment)
44

55
# aws-xray-lambda-promise-subsegment
6+
67
This will wrap an existing promise in a new promise that also creates a new subsegment in aws x-ray. It takes advantage of lambda already having an open trace to add a sub-segment to.
78

89
## Running locally
910

1011
If you are running locally an not inside lambda, you will get a warning:
1112

12-
**WARNING: Skipping adding subsegment because we are not executing inside of aws lambda**
13+
```text
14+
WARNING: Skipping adding subsegment because we are not executing inside of aws lambda
15+
```
1316

14-
This is because there is no open trace to add a segment to. However, this will allow you to test locally without errors occuring.
17+
This is because there is no open trace to add a segment to. However, this will allow you to test locally without errors occurring.
1518

1619
## Usage
1720

1821
## Without a parent segment
22+
23+
You have two options available. For the simplest possible addition use:
24+
25+
```javascript
26+
// javascript
27+
const addSegment = require("aws-xray-lambda-promise-subsegment").addSegment;
28+
29+
const promiseWrappedInSubsegment = addSegment('subSegmentName', promiseOfThingsFactory());
30+
```
31+
32+
However, if you want to add metadata or annotations, then you'll need the more complex version:
33+
1934
```javascript
2035
// javascript
2136
const addPromiseSegment = require("aws-xray-lambda-promise-subsegment").addPromiseSegment;
@@ -34,6 +49,15 @@ const promiseWrappedInSubsegment = addPromiseSegment({
3449
});
3550
```
3651

52+
### Es6 Syntax
53+
54+
```javascript
55+
// es6
56+
const { addSegment } = require("aws-xray-lambda-promise-subsegment");
57+
58+
const promiseWrappedInSubsegment = addSegment('subSegmentName', promiseOfThingsFactory());
59+
```
60+
3761
```javascript
3862
// es6
3963
import { addPromiseSegment } from "aws-xray-lambda-promise-subsegment";

demo/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ console.log('----------------------------------------');
66
console.log('----------------------------------------');
77

88
const AWSXRay = require('aws-xray-sdk-core');
9-
const addPromiseSegment = require('../index.js').addPromiseSegment;
9+
const { addPromiseSegment, addSegment } = require('../index.js');
1010
const Enquirer = require('enquirer');
1111

1212
process.env.LAMBDA_TASK_ROOT = 'The taskiest root of them all';
@@ -76,6 +76,7 @@ async function basicTracing() {
7676
(async function() {
7777
const BASIC_EXAMPLE = 'Basic';
7878
const NESTED_EXAMPLE = 'Nested';
79+
const ADD_SEGMENT = 'addSegment';
7980

8081
enquirer = new Enquirer();
8182
const response = await enquirer.prompt({
@@ -84,14 +85,17 @@ async function basicTracing() {
8485
message: 'Which example?',
8586
choices: [
8687
BASIC_EXAMPLE,
87-
NESTED_EXAMPLE
88+
NESTED_EXAMPLE,
89+
ADD_SEGMENT
8890
]
8991
});
9092

9193
if(response.example === BASIC_EXAMPLE) {
9294
return basicTracing();
9395
} else if(response.example === NESTED_EXAMPLE) {
9496
return nestedTracing();
97+
} else if(response.example === ADD_SEGMENT) {
98+
return runWithTracing(() => addSegment('add-segment-example', Promise.resolve({ all: 'of it works' })));
9599
}
96100
})()
97101
.then(console.log)

index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ export declare function addPromiseSegment<T>({
1717
parentSegment?: object,
1818
promiseFactory: (subsegment) => Promise<T>,
1919
segmentName: string
20-
}): Promise<T>;
20+
}): Promise<T>;
21+
22+
export declare function addSegment<T>(segmentName: string, promise: Promise): Promise<T>;

index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function addAnnotations(subSegment, annotations) {
2222
}
2323
}
2424

25-
module.exports.addPromiseSegment = async function ({
25+
async function addPromiseSegment ({
2626
annotations,
2727
parentSegment,
2828
promiseFactory,
@@ -64,4 +64,16 @@ module.exports.addPromiseSegment = async function ({
6464
reject(err);
6565
}
6666
});
67+
};
68+
69+
async function addSegment (segmentName, promise) {
70+
return addPromiseSegment({
71+
segmentName,
72+
promiseFactory: () => promise
73+
})
74+
}
75+
76+
module.exports = {
77+
addPromiseSegment,
78+
addSegment
6779
};

0 commit comments

Comments
 (0)