Skip to content

Commit

Permalink
adds logs enrichment section
Browse files Browse the repository at this point in the history
  • Loading branch information
boristane committed Oct 25, 2023
1 parent 159fb02 commit 6789d81
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sending-data/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ Baselime supports a variety of data sources, including logs, metrics, traces, an
[!ref icon="../assets/images/logos/vector.png"](./vector.md)
[!ref icon="../assets/images/logos/logo_kubernetes_icon.png"](./kubernetes.md)


### Other Sources
[!ref icon="../assets/images/logos/http_[email protected]"](./events-api.md)
[!ref icon="../assets/images/logos/s3[email protected]"](./s3-rehydration.md)

---

## Best Practices

[!ref Enriching Logs for faster debugging](./logs-enrichment.md)
[!ref Data Validation](./validation/index.md)
108 changes: 108 additions & 0 deletions sending-data/logs-enrichment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
label: Enriching Logs
order: -10
---

# Enriching Logs in Baselime

---

Baselime enables you to enrich your logs with special fields to enable deeper insights into your application's performance and traceability.

The available fields are:
- `requestId`
- `duration`
- `traceId`

You can add those fields to your logs to enable the [Requests](../analysing-data/overview.md) view in Baselime.

!!!
Baselime automatically adds those fields to logs coming from cloud services with deep integrations such as [AWS Lambda](./aws/lambda-logs.md) and [Vercel](./vercel.md).
!!!

---

## Grouping your logs by request

Baselime enables you to group your logs by request by adding a `requestId` field to all the logs and events within the same request.

In an Express.js server:

```javascript #
const express = require('express');
const crypto = require('crypto');
const app = express();

app.use((req, res, next) => {
// Generate a unique request ID
const requestId = crypto.randomUUID();

// Attach the requestId to the request object
req.requestId = requestId;

// Continue to the next middleware or route
next();
});

app.get('/example', (req, res) => {
// Access the requestId from the request object
const requestId = req.requestId;

// Add the requestId to the log
console.log(JSON.stringify({ message: `Hello, World!`, requestId }));

// Continue your route logic
});
```

!!!
You can leverage your favourite logger to automatically add the `requestId` field to all the logs within a single request.
!!!

!!!
In addition to HTTP requests, you can use the `requestId` field to group logs for any task, such as background jobs or build processes.
!!!

---

## Measuring Request Duration

Add the `duration` field to at least one log or event from a request to measure its duration and enable analytics on request durations in Baselime.

For example, in an Express.js server:

```javascript #
const express = require('express');
const crypto = require('crypto');
const app = express();

app.use((req, res, next) => {
const requestId = crypto.randomUUID();
req.requestId = requestId;
next();
});

app.get('/example', (req, res) => {
const startTime = Date.now();

// Your route logic here

const requestId = req.requestId;
const duration = Date.now() - startTime;
console.log(JSON.stringify({ message: `End of the request`, requestId, duration }));

});
```
---

## Correlating logs and traces

Refer to the [Correlate Logs with Traces](./opentelemetry/logs-correlation.md) section.

---

## Usage

Once you have enriched your logs with the additional fields, your data will show in Baselime in the [Requests](../analysing-data/overview.md) view.

![Enriched logs in Baselime](../../assets/images/illustrations/sending-data/enriching-logs.png)

0 comments on commit 6789d81

Please sign in to comment.