Skip to content

Commit

Permalink
Merge pull request #37 from last9/nextjs-integration
Browse files Browse the repository at this point in the history
Nextjs integration
  • Loading branch information
chtushar authored Apr 26, 2024
2 parents 1097b18 + f3f8ce9 commit a5cd8af
Show file tree
Hide file tree
Showing 43 changed files with 2,236 additions and 152 deletions.
7 changes: 4 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module.exports = {
plugins: ['@typescript-eslint'],
root: true,
rules: {
prettier: {
'space-before-function-paren': ['error', 'never']
}
'func-call-spacing': 'off',
'space-before-function-paren': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/ban-ts-comment': 'off'
}
};
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/setup-node@v2
with:
node-version: 16.20.0
node-version: 18.19.0
- uses: shogo82148/[email protected]
with:
mysql-version: '8.0'
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
dist
.DS_Store
.idea
.env
.env
.next
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.0]

### Added

- Optional `enableMetricsServer` option to enable or disable the metrics server.
- Optional `enabled` option to conditionally enable or disable OpenAPM.
- Exposed `getMetrics` function to get the metrics in prometheus exposition format.
- Add support to instrument applications based on `Next.js` framework in Node.js environment.
- Automatically add Prisma metrics if they are available.

## [0.7.0] - 2024-04-01

### Added
Expand Down
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ An APM solution based on metrics and open-source tools such as Prometheus and Gr
1. [Installation](#installation)
2. [Usage](#usage)
3. [Options](#options)
4. [Setup Locally](#setup-locally)
5. [Grafana Dashboard View](#grafana-dashboard-view)
4. [API Reference](#api-reference)
5. [Setup Locally](#setup-locally)
6. [Grafana Dashboard View](#grafana-dashboard-view)

## Installation

Expand Down Expand Up @@ -51,6 +52,7 @@ process.on('SIGTERM', gracefullyShutdown);
1. [Express](#express)
2. [MySQL](#mysql)
3. [NestJS](#nestjs)
4. [Next.js][#nextjs]

### Express

Expand Down Expand Up @@ -83,6 +85,16 @@ OpenAPM currently supports RED Metrics for NestJS v4 and above.
openapm.instrument('nestjs');
```

### Next.js

OpenAPM supports RED metrics for both pages and app router in a Next.js application.

```js
openapm.instrument('nextjs');
```

> Note: You can only use the library if Next.js runs in a Node.js environment. Since OpenAPM relies on prom-client for capturing metrics data, a serverless environment might not be able persist them.
## Options

### Usage
Expand Down Expand Up @@ -147,6 +159,8 @@ const openapm = new OpenAPM({

9. `levitateConfig`: (Optional) Configuration for Levitate TSDB. Adding this configuration will enable the [Change Events](https://docs.last9.io/docs/change-events).

10. `enableMetricsServer`: (Optional) Defaults to `true`. When set to `false` the OpenAPM won't start a metrics server. To get the metrics users can rely on the `.getMetrics()` function.

```js
{
...
Expand All @@ -161,6 +175,48 @@ const openapm = new OpenAPM({
}
```

## API Reference

1. `instrument`: Used to instrument supported technologies. Refer the [usage][#usage] section.

2. `getMetrics`: Returns a Promise of string which contains metrics in Prometheus exposition format. You can use this function to expose a metrics endpoint if `enableMetricsServer` is set to false. For example,

```js
const openapm = new OpenAPM({
enableMetricsServer: false
});

openapm.instrument('express');

const app = express();

app.get('/metrics', async (_, res) => {
const metrics = await openapm.getMetrics();
res.setHeader('Content-Type', 'text/plain; version=0.0.4; charset=utf-8');
res.end(metrics);
});
```

3. `shutdown`: Returns a promise which is resolved after the cleanup in OpenAPM. The cleanup includes closing the metrics server if it has started and cleared the prom-client register.

```js
const gracefullyShutdown = () => {
server.close(() => {
openapm
.shutdown()
.then(() => {
console.log('OpenAPM shutdown successful.');
})
.catch((err) => {
console.log('Error shutting down OpenAPM', err);
});
});
};

process.on('SIGINT', gracefullyShutdown);
process.on('SIGTERM', gracefullyShutdown);
```

## Setup locally

Make sure you are in the express directory.
Expand Down
Loading

0 comments on commit a5cd8af

Please sign in to comment.