Skip to content

Commit 65e9b8d

Browse files
committed
chore: update environment configuration and dependencies
- Updated .env.sample with new logging and tracing configurations, including Grafana Loki and OpenTelemetry settings. - Bumped dependencies for @opentelemetry packages to the latest versions, enhancing monitoring capabilities. - Modified logger configuration to support basic authentication for Loki. - Updated README to reflect new features and setup instructions for Grafana Cloud integration. - Removed deprecated preview features from Prisma schema. This commit improves the overall observability and configuration management of the NestJS Prisma API project.
1 parent fe92042 commit 65e9b8d

File tree

9 files changed

+824
-1051
lines changed

9 files changed

+824
-1051
lines changed

.env.sample

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@
33

44
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
55
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
6-
7-
POSTGRES_USER=postgres
8-
POSTGRES_PASSWORD=postgres
9-
POSTGRES_DB=nestjs_api
10-
POSTGRES_HOST="127.0.0.1"
11-
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB}?schema=public
12-
# DATABASE_URL="postgresql://[email protected]:5432/nestjs_api?schema=public"
6+
DATABASE_URL="postgresql://postgres:[email protected]:5432/nestjs-api?schema=public"
137

148
# HTTP
159
NODE_ENV=development
1610
HTTP_PORT=4000
1711

1812
# Log
19-
LOG_LOKI="http://192.168.1.4:3100"
2013
LOG_LEVEL=debug
21-
LOG_FILE=./logs/combined.log
14+
LOG_FILE="./logs/combined.log"
15+
LOG_LOKI_HOST="https://logs-prod-020.grafana.net"
16+
LOG_LOKI_USERNAME="you id"
17+
LOG_LOKI_PASSWORD="token"
2218

2319
# OTLP
24-
OTLP_SERVICE_NAME="prisma-api"
20+
OTLP_SERVICE_NAME='prisma-api'
2521
OTLP_PROM_PORT=8081
26-
OTLP_TRACES_ENDPOINT="http://192.168.1.4:4318/v1/traces"
22+
OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-gateway-prod-ap-southeast-1.grafana.net/otlp"
23+
OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
24+
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic you key"
2725

2826
# JWT
2927
JWT_SECRET_KEY=jwt_secret
30-
JWT_EXPIRE_IN="7d"
28+
JWT_EXPIRE_IN='7d'
3129

3230
# Redis
33-
REDIS_URL="redis://192.168.1.4:6379"
31+
REDIS_URL="redis://127.0.0.1:6379"
3432

3533
# Google auth
3634
GOOGLE_CLIENT_ID=

README.md

Lines changed: 145 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,210 @@
1-
# Nestjs Prisma Template
1+
# NestJS Prisma API Template
22

3-
This is an opinionated template for Nestjs prisma restful projects.
3+
A modern RESTful API template built with NestJS and Prisma, featuring comprehensive monitoring, logging, and deployment solutions.
44

5-
## Features
5+
## Features
66

7-
- [x] Platform: [Express](https://expressjs.com/)
8-
- [x] Database:
9-
- [x] ORM: [Prisma](https://prisma.io)
10-
- [ ] Redis: [Redis](https://github.com/songkeys/nestjs-redis)
11-
- Cache Manager: [cache-manager](https://github.com/nestjs/cache-manager)
12-
- [x] Logger: [Pino](https://github.com/pinojs/pino)
7+
### 🚀 Core Technology Stack
138

14-
- [nestjs-pino](https://github.com/iamolegga/nestjs-pino)
15-
- [rotating-file-stream](https://github.com/iccicci/rotating-file-stream)
16-
- [pino-pretty](https://github.com/pinojs/pino-pretty) in development
17-
- [pino-loki](https://github.com/Julien-R44/pino-loki)
9+
- **Framework**: [NestJS](https://nestjs.com/) + [Express](https://expressjs.com/)
10+
- **Database**: [Prisma](https://prisma.io) ORM
11+
- **Logging**: [Pino](https://github.com/pinojs/pino) + [nestjs-pino](https://github.com/iamolegga/nestjs-pino)
12+
- **Caching**: [cache-manager](https://github.com/nestjs/cache-manager)
1813

19-
- [x] health
20-
- [x] Authentication: [Passport](https://github.com/nestjs/passport)
21-
- JWT: [passport-jwt](https://www.passportjs.org/packages/passport-jwt/)
22-
- Google: [passport-google-oauth20](https://www.passportjs.org/packages/passport-google-oauth20/)
23-
- [x] Swagger: [@nestjs/swagger](https://github.com/nestjs/swagger)
24-
- [x] Tracing: [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel)
25-
- [x] [grafana](https://grafana.com/docs/grafana/latest/)
26-
- [x] [loki](https://grafana.com/docs/loki/latest/)
27-
- [x] [prometheus](https://prometheus.io/)
28-
- [x] [promtail](https://grafana.com/docs/loki/latest/send-data/promtail/)
29-
- [x] [tempo](https://grafana.com/docs/tempo/latest)
30-
- [x] [alertmanager](https://grafana.com/docs/grafana/latest/alerting/set-up/migrating-alerts/legacy-alerting/grafana-cloud-alerting/alertmanager/)
31-
- [x] k8s
14+
### 🔐 Authentication & Authorization
3215

33-
## Guide
16+
- **Auth Strategy**: [Passport](https://github.com/nestjs/passport)
17+
- JWT Authentication: [passport-jwt](https://www.passportjs.org/packages/passport-jwt/)
18+
- Google OAuth: [passport-google-oauth20](https://www.passportjs.org/packages/passport-google-oauth20/)
3419

35-
### Development
20+
### 📊 Monitoring & Observability
3621

37-
Init environment variables:
22+
- **API Documentation**: [Swagger](https://github.com/nestjs/swagger)
23+
- **Distributed Tracing**: [OpenTelemetry](https://github.com/pragmaticivan/nestjs-otel)
24+
- **Metrics**: [Prometheus](https://prometheus.io/)
25+
- **Log Aggregation**: [Grafana Loki](https://grafana.com/docs/loki/latest/)
26+
- **Alerting**: [Alertmanager](https://grafana.com/docs/grafana/latest/alerting/set-up/migrating-alerts/legacy-alerting/grafana-cloud-alerting/alertmanager/)
27+
28+
### 🐳 Deployment & Operations
29+
30+
- **Containerization**: Docker + Docker Compose
31+
- **Orchestration**: Kubernetes configurations
32+
- **Health Checks**: Built-in health check endpoints
33+
34+
## 🚀 Quick Start
35+
36+
### Environment Setup
37+
38+
1. **Clone the project**
3839

3940
```bash
40-
cp .env.example .env
41+
git clone <your-repo-url>
42+
cd nestjs-prisma-api
4143
```
4244

43-
Install dependencies:
45+
2. **Install dependencies**
4446

4547
```bash
4648
pnpm install
4749
```
4850

49-
Generate Prisma client:
51+
3. **Environment configuration**
5052

5153
```bash
52-
pnpm run prisma:migrate
54+
cp .env.example .env
55+
# Edit .env file to configure database connection and other settings
5356
```
5457

55-
Migrate database:
58+
4. **Database initialization**
5659

5760
```bash
61+
# Generate Prisma client
62+
pnpm run prisma:generate
63+
64+
# Run database migrations
5865
pnpm run prisma:migrate:dev
5966
```
6067

61-
Run the app:
68+
5. **Start development server**
6269

6370
```bash
6471
pnpm run start:dev
6572
```
6673

67-
### Deployment with docker
74+
The application will start at `http://localhost:3000`, with Swagger documentation available at `http://localhost:3000/api`.
75+
76+
## ⚙️ Configuration Guide
77+
78+
### Grafana Cloud Configuration
79+
80+
#### 1. Configure Loki Log Collection
81+
82+
Visit [Grafana Cloud Logs]`https://grafana.com/orgs/{your-org-id}/hosted-logs/{your-dashboard-id}#sending-logs` to get configuration information.
83+
84+
Configure in your `.env` file:
85+
86+
```bash
87+
LOG_LOKI_HOST="your-loki-url"
88+
LOG_LOKI_USERNAME="your-username"
89+
LOG_LOKI_PASSWORD="Basic <Your Grafana.com API Token>"
90+
```
91+
92+
#### 2. Configure OpenTelemetry Tracing
93+
94+
Visit [Grafana Cloud OTLP]`https://grafana.com/orgs/{your-org-id}/stacks/{your-stack-id}/otlp-info` to get configuration information.
95+
96+
Configure in your `.env` file:
97+
98+
```bash
99+
OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
100+
OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-gateway-prod-ap-southeast-1.grafana.net/otlp"
101+
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic {your-api-key}"
102+
```
103+
104+
![Grafana Cloud](./docs/cloud.png)
105+
106+
### Grafana Docker Environment
107+
108+
Use [dockotlp](https://github.com/iamnivekx/dockotlp) to quickly set up a local monitoring environment:
109+
110+
```bash
111+
git clone https://github.com/iamnivekx/dockotlp.git
112+
cd dockotlp
113+
docker compose up -d
114+
```
115+
116+
## 🐳 Deployment
117+
118+
### Docker Deployment
68119

69-
Docker:
120+
1. **Build image**
70121

71122
```bash
72123
docker-compose build
73124
```
74125

75-
### start docker-compose
126+
2. **Start services**
76127

77128
```bash
78129
docker-compose up -d
79130
```
80131

81-
In Node.js Environment:
132+
### Production Deployment
133+
134+
1. **Install dependencies and build**
82135

83136
```bash
84137
pnpm install
85138
pnpm run build
86-
pnpm run start:prod
87139
```
88140

89-
### Migrate database
90-
91-
Development:
141+
2. **Start production service**
92142

93143
```bash
94-
pnpm run prisma:migrate:dev
144+
pnpm run start:prod
95145
```
96146

97-
Production:
147+
### Database Migrations
98148

99-
```bash
100-
pnpm run prisma:migrate:deploy
101-
```
149+
- **Development**: `pnpm run prisma:migrate:dev`
150+
- **Production**: `pnpm run prisma:migrate:deploy`
151+
152+
## 📸 Feature Preview
153+
154+
### API Documentation (Swagger)
155+
156+
![Swagger API Documentation](./docs/swagger.jpg)
157+
158+
### OpenTelemetry Distributed Tracing
159+
160+
![OpenTelemetry Tracing](./docs/opentelemetry.jpg)
102161

103-
## Overview
162+
### Prometheus Metrics Monitoring
104163

105-
swagger
164+
![Prometheus Monitoring](./docs/prom.jpg)
106165

107-
![swagger](./docs/swagger.jpg)
166+
### Alert Management
108167

109-
`opentelemetry`
168+
![Alert Management](./docs/alert.jpg)
110169

111-
![opentelemetry](./docs/opentelemetry.jpg)
170+
### Kubernetes Deployment
112171

113-
`prometheus`
172+
![Kubernetes](./docs/k8s.jpg)
173+
174+
### Ingress Configuration
175+
176+
![Ingress](./docs/ingress.jpg)
177+
178+
## 📁 Project Structure
179+
180+
```
181+
src/
182+
├── api/ # API modules
183+
│ ├── auth/ # Authentication module
184+
│ ├── health/ # Health checks
185+
│ └── users/ # User management
186+
├── common/ # Common modules
187+
│ ├── constants/ # Constant definitions
188+
│ ├── decorators/ # Custom decorators
189+
│ └── guards/ # Guards
190+
├── config/ # Configuration module
191+
├── entities/ # Data entities
192+
├── prisma/ # Prisma configuration
193+
└── services/ # Business services
194+
```
114195

115-
![prom](./docs/prom.jpg)
196+
## 🤝 Contributing
116197

117-
`alerting`
118-
![alert](./docs/alert.jpg)
198+
Issues and Pull Requests are welcome!
119199

120-
`k8s`
121-
![k8s](./docs/k8s.jpg)
200+
## 📄 License
122201

123-
`ingress`
124-
![ingress](./docs/ingress.jpg)
202+
This project is licensed under the MIT License.
125203

126-
## Acknowledgements
204+
## 🙏 Acknowledgments
127205

128-
- [nestjs-prisma](https://github.com/notiz-dev/nestjs-prisma)
129-
- [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel)
130-
- [nestjs-otel-prom-grafana-tempo](https://github.com/pragmaticivan/nestjs-otel-prom-grafana-tempo)
206+
- [grafana](https://grafana.com) - Grafana
207+
- [dockotlp](https://github.com/iamnivekx/dockotlp) - Docker OpenTelemetry Prometheus Loki Grafana environment
208+
- [nestjs-prisma](https://github.com/notiz-dev/nestjs-prisma) - NestJS Prisma integration
209+
- [nestjs-otel](https://github.com/pragmaticivan/nestjs-otel) - OpenTelemetry integration
210+
- [nestjs-otel-prom-grafana-tempo](https://github.com/pragmaticivan/nestjs-otel-prom-grafana-tempo) - Monitoring stack integration examples

docs/cloud.png

609 KB
Loading

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"prisma:generate": "pnpm dlx prisma generate",
3232
"prisma:studio": "pnpm dlx prisma studio",
3333
"prisma:seed": "pnpm dlx prisma db seed",
34-
"prepare": "husky install"
34+
"prepare": "husky"
3535
},
3636
"dependencies": {
3737
"@nestjs/cache-manager": "^2.3.0",
@@ -46,20 +46,20 @@
4646
"@nestjs/swagger": "^8.1.1",
4747
"@nestjs/terminus": "^10.3.0",
4848
"@opentelemetry/api": "^1.9.0",
49-
"@opentelemetry/auto-instrumentations-node": "^0.56.1",
50-
"@opentelemetry/context-async-hooks": "1.30.1",
51-
"@opentelemetry/core": "1.30.1",
52-
"@opentelemetry/exporter-prometheus": "^0.200.0",
53-
"@opentelemetry/exporter-trace-otlp-http": "^0.200.0",
54-
"@opentelemetry/propagator-b3": "1.30.1",
55-
"@opentelemetry/propagator-jaeger": "1.30.1",
56-
"@opentelemetry/resources": "1.30.1",
57-
"@opentelemetry/sdk-metrics": "1.30.1",
58-
"@opentelemetry/sdk-node": "^0.200.0",
59-
"@opentelemetry/sdk-trace-base": "1.30.1",
60-
"@opentelemetry/semantic-conventions": "1.30.0",
61-
"@prisma/client": "^5.22.0",
62-
"@prisma/instrumentation": "^6.5.0",
49+
"@opentelemetry/auto-instrumentations-node": "0.62.1",
50+
"@opentelemetry/context-async-hooks": "2.0.1",
51+
"@opentelemetry/core": "2.0.1",
52+
"@opentelemetry/exporter-prometheus": "0.203.0",
53+
"@opentelemetry/exporter-trace-otlp-http": "0.203.0",
54+
"@opentelemetry/propagator-b3": "2.0.0",
55+
"@opentelemetry/propagator-jaeger": "2.0.1",
56+
"@opentelemetry/resources": "2.0.1",
57+
"@opentelemetry/sdk-metrics": "2.0.1",
58+
"@opentelemetry/sdk-node": "0.203.0",
59+
"@opentelemetry/sdk-trace-base": "2.0.1",
60+
"@opentelemetry/semantic-conventions": "1.36.0",
61+
"@prisma/client": "^6.15.0",
62+
"@prisma/instrumentation": "^6.15.0",
6363
"cache-manager": "^5.7.6",
6464
"cache-manager-ioredis-yet": "^2.1.2",
6565
"class-transformer": "^0.5.1",

0 commit comments

Comments
 (0)