Skip to content

Commit aaeb631

Browse files
authored
feat: Optionally use a DataDog logger (#56)
If the environment variable NEXT_PUBLIC_DATADOG_CLIENT_TOKEN is set then we use a client side DataDog logger.
1 parent 5658db8 commit aaeb631

File tree

6 files changed

+98
-13
lines changed

6 files changed

+98
-13
lines changed

package-lock.json

+43-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"dependencies": {
1515
"@cloudflare/workers-types": "^3.3.1",
1616
"@rocicorp/reflect": "^0.6.0",
17+
"@datadog/browser-logs": "^4.11.3",
18+
"@rocicorp/logger": "^2.2.0",
1719
"@rocicorp/reflect-server": "^0.11.0",
1820
"@types/node": "^17.0.16",
1921
"@types/react": "^17.0.39",

src/frontend/collaborator.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Rect } from "./rect";
44
import type { M } from "../datamodel/mutators";
55
import { useClientInfo } from "../datamodel/subscriptions";
66
import type { Reflect } from "@rocicorp/reflect";
7+
import type { OptionalLogger } from "@rocicorp/logger";
78

89
const hideCollaboratorDelay = 5000;
910

@@ -18,9 +19,11 @@ interface Position {
1819
export function Collaborator({
1920
reflect,
2021
clientID,
22+
logger,
2123
}: {
2224
reflect: Reflect<M>;
2325
clientID: string;
26+
logger: OptionalLogger;
2427
}) {
2528
const clientInfo = useClientInfo(reflect, clientID);
2629
const [lastPos, setLastPos] = useState<Position | null>(null);
@@ -40,11 +43,11 @@ export function Collaborator({
4043

4144
if (curPos) {
4245
if (!lastPos) {
43-
console.debug(`Cursor ${clientID} - got initial position`, curPos);
46+
logger.debug?.(`Cursor ${clientID} - got initial position`, curPos);
4447
setLastPos({ pos: curPos, ts: Date.now() });
4548
} else {
4649
if (lastPos.pos.x != curPos.x || lastPos.pos.y != curPos.y) {
47-
console.debug(`Cursor ${clientID} - got change to`, curPos);
50+
logger.debug?.(`Cursor ${clientID} - got change to`, curPos);
4851
setLastPos({ pos: curPos, ts: Date.now() });
4952
setGotFirstChange(true);
5053
}
@@ -58,14 +61,14 @@ export function Collaborator({
5861

5962
useEffect(() => {
6063
if (remaining > 0) {
61-
console.debug(`Cursor ${clientID} - setting timer for ${remaining}ms`);
64+
logger.debug?.(`Cursor ${clientID} - setting timer for ${remaining}ms`);
6265
const timerID = setTimeout(() => setPoke({}), remaining);
6366
return () => clearTimeout(timerID);
6467
}
6568
return;
6669
});
6770

68-
console.debug(
71+
logger.debug?.(
6972
`Cursor ${clientID} - elapsed ${elapsed}, remaining: ${remaining}, visible: ${visible}`
7073
);
7174
if (!clientInfo || !curPos || !userInfo) {
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { LogLevel, LogSink } from "@rocicorp/logger";
2+
import { datadogLogs } from "@datadog/browser-logs";
3+
4+
function throwError(message: string): never {
5+
throw new Error(message);
6+
}
7+
8+
export class DataDogBrowserLogSink implements LogSink {
9+
constructor() {
10+
datadogLogs.init({
11+
clientToken:
12+
process.env.NEXT_PUBLIC_DATADOG_CLIENT_TOKEN ??
13+
throwError("Missing env var NEXT_PUBLIC_DATADOG_CLIENT_TOKEN"),
14+
forwardErrorsToLogs: false,
15+
sampleRate: 100,
16+
silentMultipleInit: true,
17+
});
18+
}
19+
20+
log(level: LogLevel, ...args: unknown[]): void {
21+
datadogLogs.logger.log(args.join(", "), {}, level);
22+
}
23+
}

src/frontend/designer.tsx

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Reflect } from "@rocicorp/reflect";
2+
import type { OptionalLogger } from "@rocicorp/logger";
23
import React, { useRef, useState } from "react";
34
import { HotKeys } from "react-hotkeys";
45
import { DraggableCore } from "react-draggable";
@@ -15,7 +16,13 @@ import {
1516
} from "../datamodel/subscriptions";
1617
import type { M } from "../datamodel/mutators";
1718

18-
export function Designer({ reflect }: { reflect: Reflect<M> }) {
19+
export function Designer({
20+
reflect,
21+
logger,
22+
}: {
23+
reflect: Reflect<M>;
24+
logger: OptionalLogger;
25+
}) {
1926
const ids = useShapeIDs(reflect);
2027
const overID = useOverShapeID(reflect);
2128
const selectedID = useSelectedShapeID(reflect);
@@ -126,11 +133,10 @@ export function Designer({ reflect }: { reflect: Reflect<M> }) {
126133
// rectangles as their own independent svg content. Le. Sigh.
127134
collaboratorIDs.map((id) => (
128135
<Collaborator
129-
{...{
130-
key: `key-${id}`,
131-
reflect,
132-
clientID: id,
133-
}}
136+
key={`key-${id}`}
137+
reflect={reflect}
138+
clientID={id}
139+
logger={logger}
134140
/>
135141
))
136142
}

src/pages/d/[id].tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ import { Nav } from "../../frontend/nav";
55
import { M, clientMutators } from "../../datamodel/mutators";
66
import { randUserInfo } from "../../datamodel/client-state";
77
import { nanoid } from "nanoid";
8+
import { consoleLogSink, OptionalLoggerImpl } from "@rocicorp/logger";
9+
import { DataDogBrowserLogSink } from "../../frontend/data-dog-browser-log-sink";
810

911
export default function Home() {
1012
const [reflect, setReflectClient] = useState<Reflect<M> | null>(null);
1113
const [online, setOnline] = useState(false);
14+
15+
const logSink = process.env.NEXT_PUBLIC_DATADOG_CLIENT_TOKEN
16+
? new DataDogBrowserLogSink()
17+
: consoleLogSink;
18+
const logger = new OptionalLoggerImpl(logSink);
19+
1220
useEffect(() => {
1321
const [, , roomID] = location.pathname.split("/");
1422

1523
(async () => {
1624
const workerOrigin =
1725
process.env.NEXT_PUBLIC_WORKER_HOST ??
1826
"wss://replidraw.replicache.workers.dev";
19-
console.info(`Connecting to worker at ${workerOrigin}`);
27+
logger.info?.(`Connecting to worker at ${workerOrigin}`);
2028
const userID = nanoid();
2129
const r = new Reflect<M>({
2230
socketOrigin: workerOrigin,
@@ -27,6 +35,7 @@ export default function Home() {
2735
userID,
2836
roomID,
2937
}),
38+
logSinks: [logSink],
3039
mutators: clientMutators,
3140
});
3241

@@ -59,7 +68,7 @@ export default function Home() {
5968
}}
6069
>
6170
<Nav reflect={reflect} online={online} />
62-
<Designer {...{ reflect }} />
71+
<Designer reflect={reflect} logger={logger} />
6372
</div>
6473
);
6574
}

0 commit comments

Comments
 (0)