如何使用 OpenTelemetry 和 SvelteKit 检测函数

问题描述 投票:0回答:1

背景

我正在使用 SvelteKit,并希望使用 OpenTelemetry 来检测它。我尝试过使用 NodeSDK 进行自动检测,并且它有效;我可以在 Grafana Tempo 后端看到一些自动创建的跟踪,但它们不是很有用。

问题

我特别想在我的 +page.svelte 文件或组件(可以在客户端或服务器上运行)中检测一些函数,以便它们清楚地显示为自己的痕迹。

有人能告诉我如何在 +page.svelte 文件中手动检测函数,使其成为跟踪中的第一个跨度吗? (我尝试让它工作的方法是在 +page.svelte 文件中)

代码

这是我当前代码的要点

额外背景

以下是链接要点的摘录,显示了我想要使用的代码。我希望这会在 Grafana Tempo 中显示为名为

getContactsAndUpdateStore
的跟踪,但我没有看到该跟踪,只看到自动生成的跟踪。

    async function getContactsAndUpdateStore(userId: string) {
        const tracer = opentelemetry.trace.getTracer('default');
        console.log(tracer);
        return tracer.startActiveSpan('getContactsAndUpdateStore', async (span) => {
            try {
                span.setAttribute('userId', userId);
                const users = await getContacts(userId);
                contacts.set(users);
                span.addEvent('Contacts fetched successfully');
            } catch (error) {
                console.error('Error fetching contacts:', error);
                if (error instanceof Error) {
                    // Record the exception in the span if it's an Error
                    span.recordException(error);
                    span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
                } else {
                    // If it's not an Error instance, stringify if possible.
                    span.setStatus({ code: SpanStatusCode.ERROR, message: String(error) });
                }
                contacts.set([]); // Fallback in case of error
            } finally {
                console.log(span);
                span.end();
                console.log(span);
            }
        });
    }

console.log(tracer)
产生:

console.log(span)
产生:

我也尝试使用auto-instrumentations-web,但每当我导入它时,我都会遇到问题并且无法使其工作。然而,我怀疑我需要以某种方式根据代码是在客户端还是服务器上运行来以不同的方式处理获取跟踪器,但是我在这里有点超出我的深度,并且真诚地感谢任何帮助!

javascript node.js browser sveltekit open-telemetry
1个回答
0
投票

一个建议可能是尝试一种支持 opentelemetry 跟踪的不同工具,看看您在跨度中寻找的内容是否出现,您可以查看 SigNoz (https://github.com/signoz/signoz)

© www.soinside.com 2019 - 2024. All rights reserved.