Skip to content
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

Add Pino transport #1134

Merged
merged 1 commit into from
Oct 15, 2024
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
18 changes: 18 additions & 0 deletions .changesets/add-pino-transport.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
bump: patch
type: add
---

A Pino transport is now available. If Pino is your main logger, you can now use the AppSignal pino transport to send those logs to AppSignal.

```js
import pino from "pino"
import { Appsignal, AppsignalPinoTransport } from "@appsignal/nodejs"

const logger = pino(
AppsignalPinoTransport({
client: Appsignal.client,
group: "application",
})
)
```
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@prisma/instrumentation": ">= 5.11.0 < 5.14.0",
"node-addon-api": "^3.1.0",
"node-gyp": "^10.0.0",
"pino-abstract-transport": "^2.0.0",
"tslib": "^2.0.3",
"winston": "^3.6.0"
},
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ export { WinstonTransport } from "./winston_transport"
export * from "./helpers"
export * as checkIn from "./check_in"
export { Heartbeat, heartbeat } from "./heartbeat"

import AppsignalPinoTransport from "./pino_transport"
export { AppsignalPinoTransport }
78 changes: 78 additions & 0 deletions src/pino_transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { LOGGER_LEVEL_SEVERITY } from "./logger"
import build from "pino-abstract-transport"
import { Client } from "./client"

type PinoTransportOptions = {
client: Client
group: string
}

const appsignalPinoTransport = ({
client,
group = "app"
}: PinoTransportOptions) => {
return build(async (source: any) => {
for await (const obj of source) {
sendLogs(parseInfo(obj, group), client)
}
})

async function sendLogs(data: Record<string, any>, client: Client) {
client.extension.log(
data.group || "app",
data.severity,
0,
data.msg,
data.attributes
)
}
}

function parseInfo(
obj: Record<string, any>,
group: string
): Record<string, any> {
const { hostname, level, msg, ...attributes } = obj

return {
severity: getSeverity(level),
hostname,
group,
msg,
attributes: flattenAttributes(attributes)
}
}

function flattenAttributes(
attributes: Record<string, any>,
prefix = ""
): Record<string, any> {
let result: Record<string, any> = {}

for (const key in attributes) {
const newKey = prefix ? `${prefix}.${key}` : key

if (
typeof attributes[key] === "object" &&
attributes[key] !== null &&
!Array.isArray(attributes[key])
) {
const flattened = flattenAttributes(attributes[key], newKey)
result = { ...result, ...flattened }
} else {
result[newKey] = attributes[key]
}
}

return result
}

function getSeverity(level: number): number {
if (level >= 50) return LOGGER_LEVEL_SEVERITY.error
if (level >= 40) return LOGGER_LEVEL_SEVERITY.warn
if (level >= 30) return LOGGER_LEVEL_SEVERITY.info
if (level >= 20) return LOGGER_LEVEL_SEVERITY.debug
return LOGGER_LEVEL_SEVERITY.trace
}

export = appsignalPinoTransport
Loading