ekzyis 3a7c3f7af2
Add setting to send diagnostics back to SN (#463)
* Add diagnostics settings & endpoint

Stackers can now help us to identify and fix bugs by enabling diagnostics.

This will send anonymized data to us.

For now, this is only used to send events around push notifications.

* Send diagnostics to slack

* Detect OS

* Diagnostics data is only pseudonymous, not anonymous

It's only pseudonymous since with additional knowledge (which stacker uses which fancy name), we could trace the events back to individual stackers.

Data is only anonymous if this is not possible - it must be irreversible.

* Check if window.navigator is defined

* Use Slack SDK

* Catch errors of slack requests

---------

Co-authored-by: ekzyis <ek@stacker.news>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-09-18 18:00:16 -05:00

27 lines
1.1 KiB
JavaScript

import models from '../../../api/models'
import slackClient from '../../../api/slack'
const channelId = process.env.SLACK_CHANNEL_ID
const toKV = (obj) => {
return obj ? Object.entries(obj).reduce((text, [k, v]) => text + ` ${k}=${v}`, '').trimStart() : '-'
}
const slackPostMessage = ({ id, level, name, message, env, context }) => {
if (!slackClient) return
const text = `\`${new Date().toISOString()}\` | \`${id} [${level}] ${name}\` | ${message} | ${toKV(context)} | ${toKV({ os: env.os })}`
return slackClient.chat.postMessage({ channel: channelId, text })
}
export default async (req, res) => {
const { level, name, message, env, context } = req.body
if (!name) return res.status(400).json({ status: 400, message: 'name required' })
if (!message) return res.status(400).json({ status: 400, message: 'message required' })
const { id } = await models.log.create({ data: { level: level.toUpperCase(), name, message, env, context } })
slackPostMessage({ id, ...req.body }).catch(console.error)
return res.status(200).json({ status: 200, message: 'ok' })
}