From db73d241bba120848732e063918afd73b34f9269 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Sun, 26 Nov 2023 20:03:29 +0100 Subject: [PATCH] Sample send events --- .changeset/long-hounds-search.md | 6 +++ ...{envelope1.txt => envelope_javascript.txt} | 0 .../overlay/_fixtures/send_to_sidecar.cjs | 53 +++++++++++++++++++ packages/overlay/package.json | 1 + .../sentry/data/sentryDataCache.test.ts | 2 +- .../src/integrations/sentry/index.test.ts | 2 +- .../integrations/sentry/sentry-integration.ts | 5 +- packages/sidecar/package.json | 3 -- packages/sidecar/src/connect.js | 34 ------------ 9 files changed, 66 insertions(+), 40 deletions(-) create mode 100644 .changeset/long-hounds-search.md rename packages/overlay/_fixtures/{envelope1.txt => envelope_javascript.txt} (100%) create mode 100644 packages/overlay/_fixtures/send_to_sidecar.cjs delete mode 100644 packages/sidecar/src/connect.js diff --git a/.changeset/long-hounds-search.md b/.changeset/long-hounds-search.md new file mode 100644 index 00000000..beb8a9e1 --- /dev/null +++ b/.changeset/long-hounds-search.md @@ -0,0 +1,6 @@ +--- +'@spotlightjs/overlay': patch +'@spotlightjs/sidecar': patch +--- + +Remove connect function from sidecar diff --git a/packages/overlay/_fixtures/envelope1.txt b/packages/overlay/_fixtures/envelope_javascript.txt similarity index 100% rename from packages/overlay/_fixtures/envelope1.txt rename to packages/overlay/_fixtures/envelope_javascript.txt diff --git a/packages/overlay/_fixtures/send_to_sidecar.cjs b/packages/overlay/_fixtures/send_to_sidecar.cjs new file mode 100644 index 00000000..61cf66c9 --- /dev/null +++ b/packages/overlay/_fixtures/send_to_sidecar.cjs @@ -0,0 +1,53 @@ +#!/usr/bin/env node +const fs = require('fs'); +const http = require('http'); +const path = require('path'); + +// Directory where the script is running +const directoryPath = path.join(__dirname); + +// Function to read files and send data +function sendData(filePath) { + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error(`Error reading file ${filePath}: ${err}`); + return; + } + + const options = { + hostname: 'localhost', + port: 8969, + path: '/stream', + method: 'POST', + headers: { + 'Content-Type': 'application/x-sentry-envelope', + }, + }; + + const req = http.request(options, res => { + console.log(`Status: ${res.statusCode}`); + }); + + req.on('error', error => { + console.error(`Problem with request: ${error.message}`); + }); + + // Send the data + req.write(data); + req.end(); + }); +} + +// Read all .txt files from the directory +fs.readdir(directoryPath, (err, files) => { + if (err) { + console.log('Unable to scan directory: ' + err); + return; + } + + files.forEach(file => { + if (path.extname(file) === '.txt') { + sendData(path.join(directoryPath, file)); + } + }); +}); diff --git a/packages/overlay/package.json b/packages/overlay/package.json index 31497618..b1d05fa2 100644 --- a/packages/overlay/package.json +++ b/packages/overlay/package.json @@ -11,6 +11,7 @@ "preview": "vite preview", "test": "vitest", "test:ci": "vitest --coverage --reporter=junit --reporter=default --outputFile=junit.xml", + "sample": "node _fixtures/send_to_sidecar.cjs", "yalc:publish": "yalc publish --push --sig --private" }, "files": [ diff --git a/packages/overlay/src/integrations/sentry/data/sentryDataCache.test.ts b/packages/overlay/src/integrations/sentry/data/sentryDataCache.test.ts index f13156d8..ca15f079 100644 --- a/packages/overlay/src/integrations/sentry/data/sentryDataCache.test.ts +++ b/packages/overlay/src/integrations/sentry/data/sentryDataCache.test.ts @@ -7,7 +7,7 @@ import fs from 'fs'; describe('SentryDataCache', () => { // We need to refactor this to make it actually testable test('Process Envelope', () => { - const envelope = fs.readFileSync('./_fixtures/envelope1.txt', 'utf-8'); + const envelope = fs.readFileSync('./_fixtures/envelope_javascript.txt', 'utf-8'); const processedEnvelope = processEnvelope({ data: envelope, contentType: 'test' }); sentryDataCache.pushEnvelope(processedEnvelope.event); expect(true).not.toBe(undefined); diff --git a/packages/overlay/src/integrations/sentry/index.test.ts b/packages/overlay/src/integrations/sentry/index.test.ts index 3c435e70..ae73cccf 100644 --- a/packages/overlay/src/integrations/sentry/index.test.ts +++ b/packages/overlay/src/integrations/sentry/index.test.ts @@ -5,7 +5,7 @@ import fs from 'fs'; describe('Sentry Integration', () => { test('Process Envelope', () => { - const envelope = fs.readFileSync('./_fixtures/envelope1.txt', 'utf-8'); + const envelope = fs.readFileSync('./_fixtures/envelope_javascript.txt', 'utf-8'); expect(processEnvelope({ data: envelope, contentType: 'test' })).not.toBe(undefined); }); diff --git a/packages/overlay/src/integrations/sentry/sentry-integration.ts b/packages/overlay/src/integrations/sentry/sentry-integration.ts index f0304b7b..5656ea44 100644 --- a/packages/overlay/src/integrations/sentry/sentry-integration.ts +++ b/packages/overlay/src/integrations/sentry/sentry-integration.ts @@ -51,7 +51,10 @@ function sendEnvelopesToSidecar(client: Client) { }, mode: 'cors', }).catch(err => { - console.error(err); + console.error( + `Sentry SDK can't connect to Sidcar is it running? See: https://spotlightjs.com/sidecar/npx/`, + err, + ); }); }); } diff --git a/packages/sidecar/package.json b/packages/sidecar/package.json index 78b114f9..9e424ba5 100644 --- a/packages/sidecar/package.json +++ b/packages/sidecar/package.json @@ -26,9 +26,6 @@ "./vite-plugin": { "import": "./src/vite-plugin.js" }, - "./connect": { - "import": "./src/connect.js" - }, "./run": { "import": "./src/run.js" } diff --git a/packages/sidecar/src/connect.js b/packages/sidecar/src/connect.js deleted file mode 100644 index 7a6240c5..00000000 --- a/packages/sidecar/src/connect.js +++ /dev/null @@ -1,34 +0,0 @@ -function serializeEnvelope(envelope) { - const [envHeaders, items] = envelope; - - // Initially we construct our envelope as a string and only convert to binary chunks if we encounter binary data - const parts = []; - parts.push(JSON.stringify(envHeaders)); - - for (const item of items) { - const [itemHeaders, payload] = item; - - parts.push('\\n' + JSON.stringify(itemHeaders) + '\\n'); - - parts.push(JSON.stringify(payload)); - } - - return parts.join(''); -} -export function connect(hub) { - // A very hacky way to hook into Sentry's SDK - // but we love hacks - hub._stack[0].client.setupIntegrations(true); - hub._stack[0].client.on('beforeEnvelope', envelope => { - fetch('http://localhost:8969/stream', { - method: 'POST', - body: serializeEnvelope(envelope), - headers: { - 'Content-Type': 'application/x-sentry-envelope', - }, - mode: 'cors', - }).catch(() => { - console.warn('[Spotlight] Failed to send envelope to Spotlight Sidecar'); - }); - }); -}