Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(logs): copy log link that opens the viewer tab (#61827)

Adam Leith75508fd9c18c
diff --git a/products/logs/frontend/components/LogsViewer/logsViewerLogic.test.ts b/products/logs/frontend/components/LogsViewer/logsViewerLogic.test.ts
index 1a6cf2111c78..26abb2402a43 100644
--- a/products/logs/frontend/components/LogsViewer/logsViewerLogic.test.ts
+++ b/products/logs/frontend/components/LogsViewer/logsViewerLogic.test.ts
@@ -1,5 +1,7 @@
import { expectLogic } from 'kea-test-utils'
+import { copyToClipboard } from 'lib/utils/copyToClipboard'
+
import { useMocks } from '~/mocks/jest'
import { LogMessage } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
@@ -11,6 +13,10 @@ import { logsViewerDataLogic } from './data/logsViewerDataLogic'
import { logsViewerFiltersLogic } from './Filters/logsViewerFiltersLogic'
import { logsViewerLogic } from './logsViewerLogic'
+jest.mock('lib/utils/copyToClipboard', () => ({
+ copyToClipboard: jest.fn().mockResolvedValue(true),
+}))
+
const createMockRawLog = (uuid: string): LogMessage => ({
uuid,
trace_id: 'trace-1',
@@ -722,6 +728,24 @@ describe('logsViewerLogic', () => {
})
})
+ describe('copyLinkToLog', () => {
+ beforeEach(() => {
+ ;({ logic } = mountWithLogs())
+ jest.mocked(copyToClipboard).mockClear()
+ })
+
+ it('copies a link pointing at the viewer tab even when copied from another tab', async () => {
+ window.history.replaceState({}, '', '/project/1/logs?activeTab=services')
+
+ logic.actions.copyLinkToLog('log-2')
+ await expectLogic(logic).toFinishAllListeners()
+
+ const copiedUrl = new URL(jest.mocked(copyToClipboard).mock.calls[0][0])
+ expect(copiedUrl.searchParams.get('activeTab')).toEqual('viewer')
+ expect(copiedUrl.searchParams.get('linkToLogId')).toEqual('log-2')
+ })
+ })
+
describe('per-row prettification', () => {
beforeEach(() => {
;({ logic } = mountWithLogs())
diff --git a/products/logs/frontend/components/LogsViewer/logsViewerLogic.ts b/products/logs/frontend/components/LogsViewer/logsViewerLogic.ts
index d86f99c3e6d6..052fb9cf8f54 100644
--- a/products/logs/frontend/components/LogsViewer/logsViewerLogic.ts
+++ b/products/logs/frontend/components/LogsViewer/logsViewerLogic.ts
@@ -517,6 +517,9 @@ export const logsViewerLogic = kea<logsViewerLogicType>([
copyLinkToLog: ({ logId }) => {
posthog.capture('logs link copied')
const url = new URL(window.location.href)
+ // Linked logs only open in the Viewer tab, so always point the shared link there
+ // regardless of which tab the link was copied from.
+ url.searchParams.set('activeTab', 'viewer')
url.searchParams.set('linkToLogId', logId)
if (values.visibleLogsTimeRange) {
url.searchParams.set(