-
Notifications
You must be signed in to change notification settings - Fork 154
docs(all): make docs more coherent #387
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1e17d07
chore(test): test
flochaz 62d6c9e
docs(metrics): remove serializeMetrics and fix cloudwatch excerpt
flochaz b1bc7f9
fix: tracer construct initializer
dreamorosi c63fdd6
Conformed tracer & logger
dreamorosi 80ae556
Merge branch 'docs/fixMetrics' of github.com:awslabs/aws-lambda-power…
dreamorosi a93e905
Added changes from #376
dreamorosi a0e8615
Fixed merge conflicts
dreamorosi 8d7a93e
Added lambdaInterface to logger examples
dreamorosi 479820f
docs: update docs/core/logger.md
dreamorosi 6b473bd
docs: updated return type for all async handlers
dreamorosi 1714eb6
docs: update docs/core/metrics.md
dreamorosi f07c545
docs: update docs/core/metrics.md
dreamorosi d0d0c29
docs: update docs/core/metrics.md
dreamorosi 482b6d8
docs: updated all references to publishStoredMetrics
dreamorosi ac7587a
docs: replaced extends w/ implements keyword in classes
dreamorosi 2539fbb
docs: switched to serviceName according to #401
dreamorosi 6e06130
docs: switched to serviceName according to #401
dreamorosi a5dbd92
docs: update docs/core/tracer.md
dreamorosi faee189
docs: update docs/core/metrics.md
dreamorosi d05e421
docs: update docs/core/tracer.md
dreamorosi 2145f90
docs: update docs/core/logger.md
dreamorosi bfe774a
docs: update note on patching all modules
dreamorosi fffc291
docs: updated text in captureMethod section
dreamorosi d982318
docs: update docs/core/metrics.md
dreamorosi 7853925
docs: update docs/core/metrics.md
dreamorosi ce9e3d8
docs: updated other references to serverlessAirline
dreamorosi 6b7a800
Merge branch 'docs/general_improv' of github.com:awslabs/aws-lambda-p…
dreamorosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ For a **complete list** of supported environment variables, refer to [this secti | |
// You can also pass the parameters in the constructor | ||
// const logger = new Logger({ | ||
// logLevel: "WARN", | ||
// serviceName: "shopping-cart-api" | ||
// serviceName: "serverlessAirline" | ||
// }); | ||
``` | ||
|
||
|
@@ -74,7 +74,7 @@ For a **complete list** of supported environment variables, refer to [this secti | |
Environment: | ||
Variables: | ||
LOG_LEVEL: WARN | ||
POWERTOOLS_SERVICE_NAME: shopping-cart-api | ||
POWERTOOLS_SERVICE_NAME: serverlessAirline | ||
``` | ||
|
||
### Standard structured keys | ||
|
@@ -86,7 +86,7 @@ Key | Example | Note | |
**level**: `string` | `INFO` | Logging level set for the Lambda function"s invocation | ||
**message**: `string` | `Query performed to DynamoDB` | A descriptive, human-readable representation of this log item | ||
**sampling_rate**: `float` | `0.1` | When enabled, it prints all the logs of a percentage of invocations, e.g. 10% | ||
**service**: `string` | `shopping-cart-api` | A unique name identifier of the service this Lambda function belongs to, by default `service_undefined` | ||
**service**: `string` | `serverlessAirline` | A unique name identifier of the service this Lambda function belongs to, by default `service_undefined` | ||
**timestamp**: `string` | `2011-10-05T14:48:00.000Z` | Timestamp string in simplified extended ISO format (ISO 8601) | ||
**xray_trace_id**: `string` | `1-5759e988-bd862e3fe1be46a994272793` | When [tracing is enabled](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"}, it shows X-Ray Trace ID | ||
**error**: `Object` | `{ name: "Error", location: "/my-project/handler.ts:18", message: "Unexpected error #1", stack: "[stacktrace]"}` | Optional - An object containing information about the Error passed to the logger | ||
|
@@ -105,7 +105,23 @@ Key | Example | |
**function_arn**: `string` | `arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1` | ||
**function_request_id**: `string` | `c6af9ac6-7b61-11e6-9a41-93e812345678` | ||
|
||
=== "Middleware" | ||
=== "Manual" | ||
|
||
```typescript hl_lines="7" | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
|
||
const logger = new Logger(); | ||
|
||
export const handler = async (_event, context) => { | ||
|
||
logger.addContext(context); | ||
|
||
logger.info("This is an INFO log with some context"); | ||
|
||
}; | ||
``` | ||
|
||
=== "Middy Middleware" | ||
|
||
!!! note | ||
Middy comes bundled with Logger, so you can just import it when using the middleware. | ||
|
@@ -119,45 +135,33 @@ Key | Example | |
|
||
const logger = new Logger(); | ||
|
||
const lambdaHandler = async () => { | ||
const lambdaHandler = async (_event: any, _context: any) => { | ||
logger.info("This is an INFO log with some context"); | ||
}; | ||
|
||
const handler = middy(lambdaHandler) | ||
export const handler = middy(lambdaHandler) | ||
.use(injectLambdaContext(logger)); | ||
``` | ||
|
||
=== "Manual" | ||
|
||
```typescript hl_lines="7" | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
|
||
const logger = new Logger(); | ||
|
||
const lambdaHandler = async (_event, context) => { | ||
|
||
logger.addContext(context); | ||
|
||
logger.info("This is an INFO log with some context"); | ||
|
||
}; | ||
``` | ||
|
||
=== "Decorator" | ||
|
||
```typescript hl_lines="7" | ||
```typescript hl_lines="8" | ||
import { Logger } from "@aws-lambda-powertools/logger"; | ||
import { LambdaInterface } from '@aws-lambda-powertools/commons'; | ||
|
||
const logger = new Logger(); | ||
|
||
class Lambda { | ||
|
||
class Lambda implements LambdaInterface { | ||
// Decorate your handler class method | ||
@logger.injectLambdaContext() | ||
public handler() { | ||
public async handler(_event: any, _context: any): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. |
||
logger.info("This is an INFO log with some context"); | ||
} | ||
|
||
} | ||
|
||
export const myFunction = new Lambda(); | ||
export const handler = myFunction.handler; | ||
``` | ||
|
||
In each case, the printed log will look like this: | ||
|
@@ -173,7 +177,7 @@ In each case, the printed log will look like this: | |
"function_name": "shopping-cart-api-lambda-prod-eu-central-1", | ||
"level": "INFO", | ||
"message": "This is an INFO log with some context", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T21:21:08.921Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
|
@@ -213,7 +217,7 @@ You can append additional persistent keys and values in the logs generated durin | |
// } | ||
// }); | ||
|
||
const lambdaHandler: Handler = async () => { | ||
export const handler = async (_event: any, _context: any) => { | ||
|
||
// This info log will print all extra custom attributes added above | ||
// Extra attributes: logger object with name and version of the logger library, awsAccountId, awsRegion | ||
|
@@ -232,7 +236,7 @@ You can append additional persistent keys and values in the logs generated durin | |
{ | ||
"level": "INFO", | ||
"message": "This is an INFO log", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T21:49:58.084Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456", | ||
"aws_account_id": "123456789012", | ||
|
@@ -245,7 +249,7 @@ You can append additional persistent keys and values in the logs generated durin | |
{ | ||
"level": "INFO", | ||
"message": "This is another INFO log", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T21:49:58.088Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456", | ||
"aws_account_id": "123456789012", | ||
|
@@ -271,7 +275,7 @@ You can append additional keys and values in a single log item passing them as p | |
|
||
const logger = new Logger(); | ||
|
||
const lambdaHandler = async () => { | ||
export const handler = async (_event: any, _context: any) => { | ||
|
||
const myImportantVariable = { | ||
foo: "bar" | ||
|
@@ -300,15 +304,15 @@ You can append additional keys and values in a single log item passing them as p | |
{ | ||
"level": "INFO", | ||
"message": "This is a log with an extra variable", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:06:17.463Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456", | ||
"data": { foo: "bar" } | ||
} | ||
{ | ||
"level": "INFO", | ||
"message": "This is a log with 2 extra variables", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:06:17.466Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456", | ||
"data": { "foo": "bar" }, | ||
|
@@ -328,7 +332,7 @@ The error will be logged with default key name `error`, but you can also pass yo | |
|
||
const logger = new Logger(); | ||
|
||
const lambdaHandler = async () => { | ||
export const handler = async (_event: any, _context: any) => { | ||
|
||
try { | ||
throw new Error("Unexpected error #1"); | ||
|
@@ -353,7 +357,7 @@ The error will be logged with default key name `error`, but you can also pass yo | |
{ | ||
"level": "ERROR", | ||
"message": "This is an ERROR log #1", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:12:39.345Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456", | ||
"error": { | ||
|
@@ -366,7 +370,7 @@ The error will be logged with default key name `error`, but you can also pass yo | |
{ | ||
"level": "ERROR", | ||
"message": "This is an ERROR log #2", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:12:39.377Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456", | ||
"myCustomErrorKey": { | ||
|
@@ -400,7 +404,7 @@ This can be useful for example if you want to enable multiple Loggers with diffe | |
logLevel: "ERROR" | ||
}); | ||
|
||
const lambdaHandler: Handler = async () => { | ||
export const handler = async (_event: any, _context: any) => { | ||
|
||
logger.info("This is an INFO log, from the parent logger"); | ||
logger.error("This is an ERROR log, from the parent logger"); | ||
|
@@ -417,21 +421,21 @@ This can be useful for example if you want to enable multiple Loggers with diffe | |
{ | ||
"level": "INFO", | ||
"message": "This is an INFO log, from the parent logger", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:32:54.667Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "ERROR", | ||
"message": "This is an ERROR log, from the parent logger", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:32:54.670Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "ERROR", | ||
"message": "This is an ERROR log, from the child logger", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:32:54.670Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
|
@@ -465,7 +469,7 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i | |
sampleRateValue: 0.5 | ||
}); | ||
|
||
const lambdaHandler = async () => { | ||
export const handler = async (_event: any, _context: any) => { | ||
|
||
// This log item (equal to log level 'ERROR') will be printed to standard output | ||
// in all Lambda invocations | ||
|
@@ -490,31 +494,31 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i | |
"level": "ERROR", | ||
"message": "This is an ERROR log", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.334Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "DEBUG", | ||
"message": "This is a DEBUG log that has 50% chance of being printed", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.337Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "INFO", | ||
"message": "This is an INFO log that has 50% chance of being printed", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.338Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "WARN", | ||
"message": "This is a WARN log that has 50% chance of being printed", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.338Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
|
@@ -527,7 +531,7 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i | |
"level": "ERROR", | ||
"message": "This is an ERROR log", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.334Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
|
@@ -540,31 +544,31 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i | |
"level": "ERROR", | ||
"message": "This is an ERROR log", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.334Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "DEBUG", | ||
"message": "This is a DEBUG log that has 50% chance of being printed", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.337Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "INFO", | ||
"message": "This is an INFO log that has 50% chance of being printed", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.338Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
{ | ||
"level": "WARN", | ||
"message": "This is a WARN log that has 50% chance of being printed", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.338Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
|
@@ -577,15 +581,15 @@ For example, by setting the "sample rate" to `0.5`, roughly 50% of your lambda i | |
"level": "ERROR", | ||
"message": "This is an ERROR log", | ||
"sampling_rate": "0.5", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"timestamp": "2021-12-12T22:59:06.334Z", | ||
"xray_trace_id": "abcdef123456abcdef123456abcdef123456" | ||
} | ||
``` | ||
|
||
### Custom Log formatter (Bring Your Own Formatter) | ||
|
||
You can customize the structure (keys and values) of your log items by passing a custom log formatter, an object that extends the `LogFormatter` abstract class. | ||
You can customize the structure (keys and values) of your log items by passing a custom log formatter, an object that implements the `LogFormatter` abstract class. | ||
|
||
=== "handler.ts" | ||
|
||
|
@@ -596,7 +600,7 @@ You can customize the structure (keys and values) of your log items by passing a | |
const logger = new Logger({ | ||
logFormatter: new MyCompanyLogFormatter(), | ||
logLevel: "DEBUG", | ||
serviceName: "shopping-cart-api", | ||
serviceName: "serverlessAirline", | ||
sampleRateValue: 0.5, | ||
persistentLogAttributes: { | ||
awsAccountId: process.env.AWS_ACCOUNT_ID, | ||
|
@@ -607,10 +611,12 @@ You can customize the structure (keys and values) of your log items by passing a | |
}, | ||
}); | ||
|
||
const lambdaHandler: Handler = async (event, context) => { | ||
export const handler = async (event, _context) => { | ||
|
||
logger.addContext(context); | ||
|
||
logger.info("This is an INFO log", { correlationIds: { myCustomCorrelationId: "foo-bar-baz" } }); | ||
|
||
}; | ||
``` | ||
|
||
|
@@ -625,7 +631,7 @@ This is how the `MyCompanyLogFormatter` (dummy name) would look like: | |
// Replace this line with your own type | ||
type MyCompanyLog = LogAttributes; | ||
|
||
class MyCompanyLogFormatter extends LogFormatter { | ||
class MyCompanyLogFormatter implements LogFormatter { | ||
|
||
public formatAttributes(attributes: UnformattedAttributes): MyCompanyLog { | ||
return { | ||
|
@@ -666,7 +672,7 @@ This is how the printed log would look: | |
```json | ||
{ | ||
"message": "This is an INFO log", | ||
"service": "shopping-cart-api", | ||
"service": "serverlessAirline", | ||
"awsRegion": "eu-central-1", | ||
"correlationIds": { | ||
"awsRequestId": "c6af9ac6-7b61-11e6-9a41-93e812345678", | ||
|
@@ -722,7 +728,7 @@ describe('MyUnitTest', () => { | |
|
||
test('Lambda invoked successfully', async () => { | ||
|
||
const testEvent = { test: 'test' }; | ||
const testEvent = { test: 'test' }; | ||
await handler(testEvent, dummyContext); | ||
|
||
}); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(minor) There are a few more "shopping-cart-api" left in the doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I've missed those. I've now removed all of the ones in the
serviceName
but left the ones in the function name