B2B-88: add starter kit structure and elements

This commit is contained in:
devmc-ee
2025-06-08 16:18:30 +03:00
parent 657a36a298
commit e7b25600cb
1280 changed files with 77893 additions and 5688 deletions

View File

@@ -0,0 +1 @@
export * from './hooks/use-baselime';

View File

@@ -0,0 +1,50 @@
import { useRef } from 'react';
import { BaselimeRum } from '@baselime/react-rum';
import { MonitoringContext } from '@kit/monitoring-core';
import { useBaselime } from '../hooks/use-baselime';
export function BaselimeProvider({
children,
apiKey,
enableWebVitals,
ErrorPage,
}: React.PropsWithChildren<{
apiKey?: string;
enableWebVitals?: boolean;
ErrorPage?: React.ReactElement;
}>) {
const key = apiKey ?? process.env.NEXT_PUBLIC_BASELIME_KEY ?? '';
if (!key) {
console.warn(
'You configured Baselime as monitoring provider but did not provide a key. ' +
'Please provide a key to enable monitoring with Baselime using the variable NEXT_PUBLIC_BASELIME_KEY.',
);
return children;
}
return (
<BaselimeRum
apiKey={key}
enableWebVitals={enableWebVitals}
fallback={ErrorPage ?? null}
>
<MonitoringProvider>{children}</MonitoringProvider>
</BaselimeRum>
);
}
function MonitoringProvider(props: React.PropsWithChildren) {
const service = useBaselime();
const provider = useRef(service);
return (
<MonitoringContext.Provider value={provider.current}>
{props.children}
</MonitoringContext.Provider>
);
}

View File

@@ -0,0 +1,30 @@
import { useMemo } from 'react';
import { useBaselimeRum } from '@baselime/react-rum';
import { MonitoringService } from '@kit/monitoring-core';
/**
* @name useBaselime
* @description Get the Baselime monitoring service for the browser.
*/
export function useBaselime(): MonitoringService {
const { captureException, setUser, sendEvent } = useBaselimeRum();
return useMemo(() => {
return {
captureException(error: Error, extra?: React.ErrorInfo) {
void captureException(error, extra);
},
identifyUser(params) {
setUser(params.id);
},
captureEvent<Extra extends object>(event: string, extra?: Extra) {
return sendEvent(event, extra);
},
ready() {
return Promise.resolve();
},
} satisfies MonitoringService;
}, [captureException, sendEvent, setUser]);
}

View File

@@ -0,0 +1,38 @@
/**
* @name registerInstrumentation
* @description This file is used to register Baselime instrumentation for your Next.js application.
*
* Please set the MONITORING_PROVIDER environment variable to 'baselime' to register Baselime instrumentation.
*/
export async function registerInstrumentation() {
if (process.env.ENABLE_MONITORING_INSTRUMENTATION !== 'true') {
return;
}
const serviceName = process.env.INSTRUMENTATION_SERVICE_NAME;
if (!serviceName) {
throw new Error(`
You have set the Baselime instrumentation provider, but have not set the INSTRUMENTATION_SERVICE_NAME environment variable.
Please set the INSTRUMENTATION_SERVICE_NAME environment variable.
`);
}
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { BaselimeSDK, BetterHttpInstrumentation, VercelPlugin } =
await import('@baselime/node-opentelemetry');
const sdk = new BaselimeSDK({
serverless: true,
service: serviceName,
baselimeKey: process.env.NEXT_PUBLIC_BASELIME_KEY,
instrumentations: [
new BetterHttpInstrumentation({
plugins: [new VercelPlugin()],
}),
],
});
sdk.start();
}
}

View File

@@ -0,0 +1 @@
export * from './services/baselime-server-monitoring.service';

View File

@@ -0,0 +1,115 @@
import { z } from 'zod';
import { MonitoringService } from '@kit/monitoring-core';
const apiKey = z
.string({
required_error: 'NEXT_PUBLIC_BASELIME_KEY is required',
description: 'The Baseline API key',
})
.parse(process.env.NEXT_PUBLIC_BASELIME_KEY);
export class BaselimeServerMonitoringService implements MonitoringService {
userId: string | null = null;
async captureException(
error: Error | null,
extra?: {
requestId?: string;
sessionId?: string;
namespace?: string;
service?: string;
},
) {
const formattedError = error ? getFormattedError(error) : {};
const event = {
level: 'error',
data: { error },
error: {
...formattedError,
},
message: error ? `${error.name}: ${error.message}` : `Unknown error`,
};
const response = await fetch(`https://events.baselime.io/v1/logs`, {
method: 'POST',
headers: {
contentType: 'application/json',
'x-api-key': apiKey,
'x-service': extra?.service ?? '',
'x-namespace': extra?.namespace ?? '',
},
body: JSON.stringify([
{
userId: this.userId,
sessionId: extra?.sessionId,
namespace: extra?.namespace,
...event,
},
]),
});
if (!response.ok) {
console.error(
{
response,
event,
},
'Failed to send event to Baselime',
);
}
}
async captureEvent<
Extra extends {
sessionId?: string;
namespace?: string;
service?: string;
},
>(event: string, extra?: Extra) {
const response = await fetch(`https://events.baselime.io/v1/logs`, {
method: 'POST',
headers: {
contentType: 'application/json',
'x-api-key': apiKey,
'x-service': extra?.service ?? '',
'x-namespace': extra?.namespace ?? '',
},
body: JSON.stringify([
{
userId: this.userId,
sessionId: extra?.sessionId,
namespace: extra?.namespace,
message: event,
},
]),
});
if (!response.ok) {
console.error(
{
response,
event,
},
'Failed to send event to Baselime',
);
}
}
identifyUser<Info extends { id: string }>(info: Info) {
this.userId = info.id;
}
ready() {
return Promise.resolve();
}
}
function getFormattedError(error: Error) {
return {
name: error.name,
message: error.message,
stack: error.stack,
};
}