Skip to content

fix(examples): fix errors in logger and metrics examples #509

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 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| AWS Lambda Powertools for TypeScript is currently released as a beta developer preview and is intended strictly for feedback purposes only. <br/>This version is not stable, and significant breaking changes might incur as part of the upcoming [production-ready release](https://github.com/awslabs/aws-lambda-powertools-typescript/milestone/2). |_


A suite of TypeScript utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, and more. (AWS Lambda Powertools [Python](https://github.com/awslabs/aws-lambda-powertools-python) and [Java](https://github.com/awslabs/aws-lambda-powertools-java) are also available).

**[📜 Documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/)** | **[NPM](https://www.npmjs.com/org/aws-lambda-powertools)** | **[Roadmap](https://github.com/awslabs/aws-lambda-powertools-roadmap/projects/1)** | **[Examples](https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/examples/cdk)**
Expand Down Expand Up @@ -35,7 +34,7 @@ Each TypeScript utility is installed as standalone NPM package.
### Examples

* [CDK](https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/examples/cdk)
* [Tracer](https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/examples/cdk/lib)
* [Tracer](https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/examples/cdk/src)
* [Logger](https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/packages/logger/examples)
* [Metrics](https://github.com/awslabs/aws-lambda-powertools-typescript/tree/main/packages/metrics/examples)

Expand Down
2 changes: 1 addition & 1 deletion examples/cdk/src/example-function.Tracer.PatchAWSSDKv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const tracer = new Tracer({ serviceName: 'tracerPatchAWSSDKv2Fn' });
// To patch a specific AWS SDK v2 Client, we pass it to the Tracer that will return an instrumented version of it
const sts = tracer.captureAWSClient(new STS());

// Here we are showing an example with manual instrumentation but you can do the same also with the captureLambdaHandler Middy Middleware and Class decorator
// Here we are showing an example with manual instrumentation, but you can do the same also with the captureLambdaHandler Middy Middleware and Class decorator
// See: https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/#lambda-handler
export const handler = async (_event: typeof Events.Custom.CustomEvent, _context: Context): Promise<unknown> => {
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdk/src/example-function.Tracer.PatchAWSSDKv3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const tracer = new Tracer({ serviceName: 'tracerManualFn' });
// To patch a specific AWS SDK v3 Client, we need to pass it to the Tracer that will return an instrumented version of it
const sts = tracer.captureAWSv3Client(new STSClient({}));

// Here we are showing an example with manual instrumentation but you can do the same also with the captureLambdaHandler Middy Middleware and Class decorator
// Here we are showing an example with manual instrumentation, but you can do the same also with the captureLambdaHandler Middy Middleware and Class decorator
// See: https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/#lambda-handler
export const handler = async (_event: typeof Events.Custom.CustomEvent, _context: Context): Promise<unknown> => {
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
Expand Down
2 changes: 1 addition & 1 deletion examples/cdk/src/example-function.Tracer.PatchAllAWSSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const AWS = tracer.captureAWS(require('aws-sdk'));
// Then we can use the AWS SDK as usual
const sts = new AWS.STS();

// Here we are showing an example with manual instrumentation but you can do the same also with the captureLambdaHandler Middy Middleware and Class decorator
// Here we are showing an example with manual instrumentation, but you can do the same also with the captureLambdaHandler Middy Middleware and Class decorator
// See: https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/tracer/#lambda-handler
export const handler = async (_event: typeof Events.Custom.CustomEvent, _context: Context): Promise<unknown> => {
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/examples/ephemeral-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const lambdaHandler: Handler = async () => {
// You can also pass multiple parameters
logger.info('This is a log with 2 extra variables',
{ data: myImportantVariable },
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' }}
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } }
);

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/examples/inject-context-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../types/LambdaInterface';
import { LambdaInterface } from './utils/lambda';
import { Logger } from '../src';
import { Callback, Context } from 'aws-lambda/handler';

Expand Down
3 changes: 1 addition & 2 deletions packages/logger/examples/inject-context-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ require('./../tests/helpers/populateEnvironmentVariables');
process.env.LOG_LEVEL = 'INFO';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';

// @ts-ignore
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { Handler } from 'aws-lambda';
import { Logger } from '../src';

import { injectLambdaContext } from '../src/middleware/middy';
import { injectLambdaContext } from '../src';
import middy from '@middy/core';

const logger = new Logger();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogFormatter } from '../../../src/formatter';
import { LogFormatter } from '../../../src';
import { LogAttributes, UnformattedAttributes } from '../../../src/types';

type MyCompanyLog = LogAttributes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogFormatter } from '../../../src/formatter';
import { LogFormatter } from '../../../src';
import { LogAttributes, UnformattedAttributes } from '../../../src/types';

type MyCompanyLog = LogAttributes;
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/cold-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
4 changes: 2 additions & 2 deletions packages/metrics/examples/constructor-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand All @@ -12,7 +12,7 @@ process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

const metrics = new Metrics({
namespace: 'hello-world-constructor',
service: 'hello-world-service-constructor'
serviceName: 'hello-world-service-constructor'
});

const lambdaHandler = async (): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/decorator/cold-start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './../utils/lambda/LambdaInterface';
import { LambdaInterface } from '../utils/lambda';
import { populateEnvironmentVariables } from '../../tests/helpers';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics, MetricUnits } from '../../src';
Expand Down
4 changes: 2 additions & 2 deletions packages/metrics/examples/decorator/constructor-options.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { Metrics, MetricUnits } from '../../src';
import { LambdaInterface } from './../utils/lambda';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';

const metrics = new Metrics({
namespace: 'hello-world-constructor',
service: 'hello-world-service-constructor'
serviceName: 'hello-world-service-constructor'
});

class Lambda implements LambdaInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ populateEnvironmentVariables();
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world-constructor';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service-constructor';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics, MetricUnits } from '../src';
import { Metrics, MetricUnits } from '../../src';

const metrics = new Metrics({ defaultDimensions:{ 'application': 'hello-world' } });

Expand Down
8 changes: 4 additions & 4 deletions packages/metrics/examples/decorator/default-dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ populateEnvironmentVariables();
// Additional runtime variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics, MetricUnits } from '../src';
import { Metrics, MetricUnits } from '../../src';

const metrics = new Metrics();

Expand Down
8 changes: 4 additions & 4 deletions packages/metrics/examples/decorator/dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ populateEnvironmentVariables();
// Additional runtime variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics, MetricUnits } from '../src';
import { Metrics, MetricUnits } from '../../src';

const metrics = new Metrics();

Expand Down
8 changes: 4 additions & 4 deletions packages/metrics/examples/decorator/empty-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ populateEnvironmentVariables();
// Additional runtime variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics } from '../src';
import { Metrics } from '../../src';

const metrics = new Metrics();

Expand Down
8 changes: 4 additions & 4 deletions packages/metrics/examples/decorator/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ populateEnvironmentVariables();
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics, MetricUnits } from '../src';
import { Metrics, MetricUnits } from '../../src';

const metrics = new Metrics();

Expand Down
6 changes: 3 additions & 3 deletions packages/metrics/examples/decorator/manual-flushing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ populateEnvironmentVariables();
// Additional runtime variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { Handler } from 'aws-lambda';
import { Metrics, MetricUnits } from '../src';
import { Metrics, MetricUnits } from '../../src';

const metrics = new Metrics();

Expand Down
6 changes: 3 additions & 3 deletions packages/metrics/examples/decorator/manual-metrics-print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ populateEnvironmentVariables();
// Additional runtime variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { Handler } from 'aws-lambda';
import { Metrics, MetricUnits } from '../src';
import { Metrics, MetricUnits } from '../../src';

const metrics = new Metrics();

Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/decorator/single-metric.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { populateEnvironmentVariables } from '../../tests/helpers';
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
import { LambdaInterface } from './../utils/lambda/LambdaInterface';
import { LambdaInterface } from '../utils/lambda';
import { Callback, Context } from 'aws-lambda/handler';
import { Metrics, MetricUnits } from '../../src';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/default-dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/empty-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
4 changes: 2 additions & 2 deletions packages/metrics/examples/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand All @@ -17,6 +17,6 @@ const lambdaHandler = async (): Promise<void> => {
};

const handlerWithMiddleware = middy(lambdaHandler)
.use(logMetrics(metrics}));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an actual compile error in TS

.use(logMetrics(metrics));

handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
2 changes: 1 addition & 1 deletion packages/metrics/examples/manual-flushing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/manual-metrics-print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/examples/single-metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { context as dummyContext } from '../../../tests/resources/contexts/hello
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src/middleware/middy';
import { logMetrics } from '../src';

// Populate runtime
populateEnvironmentVariables();
Expand Down