next_core/
next_telemetry.rs

1use turbo_rcstr::RcStr;
2use turbo_tasks::{Vc, fxindexmap};
3use turbopack_core::diagnostics::{Diagnostic, DiagnosticPayload};
4
5/// A structure that keeps track of whether a particular Next.js feature is
6/// enabled for the telemetry.
7///
8/// The original implementation code can be found
9/// [here](https://github.com/vercel/next.js/blob/9da305fe320b89ee2f8c3cfb7ecbf48856368913/packages/next/src/build/webpack-config.ts#L2516).
10#[turbo_tasks::value(shared)]
11pub struct NextFeatureTelemetry {
12    pub event_name: RcStr,
13    pub feature_name: RcStr,
14    pub enabled: bool,
15}
16
17impl NextFeatureTelemetry {
18    pub fn new(feature_name: RcStr, enabled: bool) -> Self {
19        NextFeatureTelemetry {
20            event_name: "EVENT_BUILD_FEATURE_USAGE".into(),
21            feature_name,
22            enabled,
23        }
24    }
25}
26
27#[turbo_tasks::value_impl]
28impl Diagnostic for NextFeatureTelemetry {
29    #[turbo_tasks::function]
30    fn category(&self) -> Vc<RcStr> {
31        Vc::cell("NextFeatureTelemetry_category_tbd".into())
32    }
33
34    #[turbo_tasks::function]
35    fn name(&self) -> Vc<RcStr> {
36        Vc::cell(self.event_name.clone())
37    }
38
39    #[turbo_tasks::function]
40    fn payload(&self) -> Vc<DiagnosticPayload> {
41        Vc::cell(fxindexmap! {
42            self.feature_name.clone() =>
43            self.enabled.to_string().into(),
44        })
45    }
46}
47
48/// A struct represent telemetry event for the feature usage,
49/// referred as `importing` a certain module. (i.e importing @next/image)
50#[turbo_tasks::value(shared)]
51pub struct ModuleFeatureTelemetry {
52    pub event_name: RcStr,
53    pub feature_name: RcStr,
54    pub invocation_count: usize,
55}
56
57impl ModuleFeatureTelemetry {
58    pub fn new(feature_name: RcStr, invocation_count: usize) -> Self {
59        ModuleFeatureTelemetry {
60            event_name: "EVENT_BUILD_FEATURE_USAGE".into(),
61            feature_name,
62            invocation_count,
63        }
64    }
65}
66
67#[turbo_tasks::value_impl]
68impl Diagnostic for ModuleFeatureTelemetry {
69    #[turbo_tasks::function]
70    fn category(&self) -> Vc<RcStr> {
71        Vc::cell("ModuleFeatureTelemetry_category_tbd".into())
72    }
73
74    #[turbo_tasks::function]
75    fn name(&self) -> Vc<RcStr> {
76        Vc::cell(self.event_name.clone())
77    }
78
79    #[turbo_tasks::function]
80    fn payload(&self) -> Vc<DiagnosticPayload> {
81        Vc::cell(fxindexmap! {
82            self.feature_name.clone() =>
83            self.invocation_count.to_string().into(),
84        })
85    }
86}