@echoteam/signoz-react 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 CODR Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,285 @@
1
+ # @codr/signoz-react
2
+
3
+ Library React untuk monitoring dan tracing aplikasi menggunakan OpenTelemetry dan SignOz.
4
+
5
+ ## 🚀 Fitur
6
+
7
+ - **Tracing Otomatis**: Integrasi OpenTelemetry untuk tracing HTTP requests, fetch, dan XMLHttpRequest
8
+ - **Error Boundary**: Komponen React untuk menangkap dan melaporkan error dengan tracing
9
+ - **Error Page**: Halaman error untuk React Router dengan integrasi tracing
10
+ - **TypeScript Support**: Dukungan penuh untuk TypeScript
11
+ - **Zero Configuration**: Mudah digunakan dengan environment variables
12
+ - **Customizable**: ErrorPage dan ErrorBoundary dapat dikustomisasi sesuai kebutuhan
13
+
14
+ ## 📦 Instalasi
15
+
16
+ ```bash
17
+ yarn add @codr/signoz-react
18
+ ```
19
+
20
+ ## 🔧 Konfigurasi
21
+
22
+ ### Environment Variables
23
+
24
+ Tambahkan environment variables berikut ke file `.env`:
25
+
26
+ ```env
27
+ REACT_APP_SIGNOZ_SERVICE_NAME=my-react-app
28
+ REACT_APP_SIGNOZ_SERVICE_VERSION=1.0.0
29
+ REACT_APP_SIGNOZ_ENV=production
30
+ REACT_APP_SIGNOZ_SERVICE_NAMESPACE=frontend
31
+ REACT_APP_SIGNOZ_URL=https://your-signoz-instance.com/v1/traces
32
+ ```
33
+
34
+ ### Inisialisasi
35
+
36
+ ```typescript
37
+ import { initializeSignOzTracing } from '@codr/signoz-react';
38
+
39
+ // Inisialisasi dengan environment variables
40
+ initializeSignOzTracing();
41
+
42
+ // Atau dengan konfigurasi manual
43
+ initializeSignOzTracing({
44
+ serviceName: 'my-react-app',
45
+ serviceVersion: '1.0.0',
46
+ environment: 'production',
47
+ serviceNamespace: 'frontend',
48
+ url: 'https://your-signoz-instance.com/v1/traces',
49
+ headers: {
50
+ 'Authorization': 'Bearer your-token'
51
+ }
52
+ });
53
+ ```
54
+
55
+ ## 🎯 Penggunaan
56
+
57
+ ### Error Boundary
58
+
59
+ #### Penggunaan Dasar
60
+ ```tsx
61
+ import { ErrorBoundary } from '@codr/signoz-react';
62
+
63
+ function App() {
64
+ return (
65
+ <ErrorBoundary>
66
+ <YourApp />
67
+ </ErrorBoundary>
68
+ );
69
+ }
70
+ ```
71
+
72
+ #### Error Boundary dengan Kustomisasi
73
+ ```tsx
74
+ import { ErrorBoundary, ErrorBoundaryProps } from '@codr/signoz-react';
75
+
76
+ // Custom fallback component
77
+ const CustomErrorFallback: React.FC<{ error: Error; resetErrorBoundary: () => void }> = ({
78
+ error,
79
+ resetErrorBoundary
80
+ }) => (
81
+ <div className="custom-error">
82
+ <h2>Terjadi Kesalahan!</h2>
83
+ <p>{error.message}</p>
84
+ <button onClick={resetErrorBoundary}>Coba Lagi</button>
85
+ </div>
86
+ );
87
+
88
+ function App() {
89
+ const handleError = (error: Error, errorInfo: React.ErrorInfo) => {
90
+ console.log('Error caught:', error);
91
+ // Kirim ke service lain jika diperlukan
92
+ };
93
+
94
+ return (
95
+ <ErrorBoundary
96
+ fallback={CustomErrorFallback}
97
+ onError={handleError}
98
+ onReset={() => console.log('Error boundary reset')}
99
+ fallbackClassName="my-error-boundary"
100
+ >
101
+ <YourApp />
102
+ </ErrorBoundary>
103
+ );
104
+ }
105
+ ```
106
+
107
+ ### Error Page untuk React Router
108
+
109
+ #### Penggunaan Dasar
110
+ ```tsx
111
+ import { ErrorPage } from '@codr/signoz-react';
112
+ import { createBrowserRouter } from 'react-router-dom';
113
+
114
+ const router = createBrowserRouter([
115
+ {
116
+ path: "/",
117
+ element: <Root />,
118
+ errorElement: <ErrorPage />,
119
+ },
120
+ ]);
121
+ ```
122
+
123
+ #### Error Page dengan Kustomisasi
124
+ ```tsx
125
+ import { ErrorPage, ErrorPageProps } from '@codr/signoz-react';
126
+
127
+ // Custom error page
128
+ const CustomErrorPage: React.FC<ErrorPageProps> = (props) => (
129
+ <ErrorPage
130
+ title="Halaman Tidak Ditemukan"
131
+ message="Maaf, halaman yang Anda cari tidak tersedia."
132
+ showStackTrace={process.env.NODE_ENV === 'development'}
133
+ actionButton={
134
+ <button onClick={() => window.history.back()}>
135
+ Kembali ke Halaman Sebelumnya
136
+ </button>
137
+ }
138
+ className="custom-error-page"
139
+ />
140
+ );
141
+
142
+ // Atau dengan custom render function
143
+ const CustomErrorPageWithRender: React.FC<ErrorPageProps> = () => (
144
+ <ErrorPage
145
+ renderError={(error) => (
146
+ <div className="my-custom-error">
147
+ <h1>🚨 Error!</h1>
148
+ <p>Kode Error: {error.name}</p>
149
+ <p>Pesan: {error.message}</p>
150
+ <button onClick={() => window.location.href = '/'}>
151
+ Kembali ke Beranda
152
+ </button>
153
+ </div>
154
+ )}
155
+ />
156
+ );
157
+
158
+ const router = createBrowserRouter([
159
+ {
160
+ path: "/",
161
+ element: <Root />,
162
+ errorElement: <CustomErrorPage />,
163
+ },
164
+ ]);
165
+ ```
166
+
167
+ ### Tracing Manual
168
+
169
+ ```typescript
170
+ import { trace } from '@opentelemetry/api';
171
+
172
+ const tracer = trace.getTracer('my-service');
173
+ const span = tracer.startSpan('my-operation');
174
+
175
+ try {
176
+ // Lakukan operasi
177
+ span.setAttribute('operation.type', 'database-query');
178
+ span.setAttribute('operation.result', 'success');
179
+ } catch (error) {
180
+ span.recordException(error);
181
+ span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
182
+ } finally {
183
+ span.end();
184
+ }
185
+ ```
186
+
187
+ ## 📋 API Reference
188
+
189
+ ### `initializeSignOzTracing(config?)`
190
+
191
+ Inisialisasi SignOz tracing untuk aplikasi React.
192
+
193
+ **Parameter:**
194
+ - `config` (opsional): Konfigurasi SignOz
195
+ - `serviceName`: Nama layanan
196
+ - `serviceVersion`: Versi layanan
197
+ - `environment`: Environment (production, staging, development)
198
+ - `serviceNamespace`: Namespace layanan
199
+ - `url`: URL endpoint SignOz
200
+ - `headers`: Headers tambahan untuk request
201
+
202
+ ### `ErrorBoundary`
203
+
204
+ Komponen React untuk menangkap error dan melaporkan ke SignOz.
205
+
206
+ **Props:**
207
+ - `children`: React children
208
+ - `fallback`: Custom fallback component
209
+ - `onError`: Custom error handler function
210
+ - `onReset`: Custom reset handler
211
+ - `showResetButton`: Apakah menampilkan reset button (default: true)
212
+ - `resetButtonText`: Custom reset button text
213
+ - `fallbackClassName`: Custom CSS class untuk fallback
214
+
215
+ ### `ErrorPage`
216
+
217
+ Komponen halaman error untuk React Router.
218
+
219
+ **Props:**
220
+ - `title`: Judul halaman error (default: "Oops!")
221
+ - `message`: Pesan error utama (default: "Terjadi kesalahan yang tidak terduga.")
222
+ - `renderError`: Custom render function untuk menampilkan error
223
+ - `className`: Custom CSS class untuk container
224
+ - `showStackTrace`: Apakah menampilkan stack trace (default: false)
225
+ - `actionButton`: Custom action button
226
+
227
+ ## 🔍 Monitoring
228
+
229
+ Setelah setup, Anda dapat melihat traces di dashboard SignOz:
230
+
231
+ 1. **Service Overview**: Melihat performa layanan secara keseluruhan
232
+ 2. **Traces**: Melihat detail traces untuk setiap request
233
+ 3. **Errors**: Melihat error yang terjadi dengan stack trace
234
+ 4. **Metrics**: Melihat metrics performa aplikasi
235
+
236
+ ## 🛠️ Development
237
+
238
+ ### Setup Development
239
+
240
+ ```bash
241
+ # Clone repository
242
+ git clone https://gitlab.echoteam.tech/codr/package/signoz-react-js.git
243
+ cd signoz-react-js
244
+
245
+ # Install dependencies
246
+ yarn install
247
+
248
+ # Build package
249
+ yarn build
250
+
251
+ # Development mode
252
+ yarn dev
253
+ ```
254
+
255
+ ### Scripts
256
+
257
+ - `yarn build`: Build package untuk production
258
+ - `yarn dev`: Build dalam mode development dengan watch
259
+ - `yarn test`: Jalankan tests
260
+ - `yarn lint`: Lint kode
261
+ - `yarn format`: Format kode dengan Prettier
262
+
263
+ ## 📄 License
264
+
265
+ MIT License - lihat file [LICENSE](LICENSE) untuk detail.
266
+
267
+ ## 🤝 Contributing
268
+
269
+ 1. Fork repository
270
+ 2. Buat feature branch (`git checkout -b feature/amazing-feature`)
271
+ 3. Commit perubahan (`git commit -m 'Add amazing feature'`)
272
+ 4. Push ke branch (`git push origin feature/amazing-feature`)
273
+ 5. Buat Pull Request
274
+
275
+ ## 📞 Support
276
+
277
+ - **Issues**: [GitLab Issues](https://gitlab.echoteam.tech/codr/package/signoz-react-js/-/issues)
278
+ - **Email**: dev@codr.id
279
+ - **Documentation**: [GitLab Wiki](https://gitlab.echoteam.tech/codr/package/signoz-react-js/-/wikis)
280
+
281
+ ## 🔗 Links
282
+
283
+ - [SignOz Documentation](https://signoz.io/docs/)
284
+ - [OpenTelemetry Documentation](https://opentelemetry.io/docs/)
285
+ - [React Error Boundary](https://reactjs.org/docs/error-boundaries.html)
@@ -0,0 +1,65 @@
1
+ import React from 'react';
2
+
3
+ interface SignOzConfig {
4
+ serviceName: string;
5
+ serviceVersion: string;
6
+ environment: string;
7
+ serviceNamespace: string;
8
+ url: string;
9
+ headers?: Record<string, string>;
10
+ }
11
+ declare global {
12
+ interface Window {
13
+ REACT_APP_SIGNOZ_SERVICE_NAME: string;
14
+ REACT_APP_SIGNOZ_SERVICE_VERSION: string;
15
+ REACT_APP_SIGNOZ_ENV: string;
16
+ REACT_APP_SIGNOZ_SERVICE_NAMESPACE: string;
17
+ REACT_APP_SIGNOZ_URL: string;
18
+ }
19
+ }
20
+ /**
21
+ * Inisialisasi SignOz tracing untuk aplikasi React
22
+ * @param config - Konfigurasi SignOz (opsional, akan menggunakan environment variables jika tidak disediakan)
23
+ */
24
+ declare function initializeSignOzTracing(config?: SignOzConfig): void;
25
+
26
+ interface ErrorFallbackProps {
27
+ error: Error;
28
+ resetErrorBoundary: () => void;
29
+ }
30
+ interface ErrorBoundaryProps {
31
+ /** React children */
32
+ children: React.ReactNode;
33
+ /** Custom fallback component */
34
+ fallback?: React.ComponentType<ErrorFallbackProps>;
35
+ /** Custom error handler function */
36
+ onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
37
+ /** Custom reset handler */
38
+ onReset?: () => void;
39
+ /** Apakah menampilkan reset button (default: true) */
40
+ showResetButton?: boolean;
41
+ /** Custom reset button text */
42
+ resetButtonText?: string;
43
+ /** Custom CSS class untuk fallback */
44
+ fallbackClassName?: string;
45
+ }
46
+ declare const ErrorBoundary: React.FC<ErrorBoundaryProps>;
47
+
48
+ interface ErrorPageProps {
49
+ /** Judul halaman error (default: "Oops!") */
50
+ title?: string;
51
+ /** Pesan error utama (default: "Terjadi kesalahan yang tidak terduga.") */
52
+ message?: string;
53
+ /** Custom render function untuk menampilkan error */
54
+ renderError?: (error: Error) => React.ReactNode;
55
+ /** Custom CSS class untuk container */
56
+ className?: string;
57
+ /** Apakah menampilkan stack trace (default: false) */
58
+ showStackTrace?: boolean;
59
+ /** Custom action button */
60
+ actionButton?: React.ReactNode;
61
+ }
62
+ declare const ErrorPage: React.FC<ErrorPageProps>;
63
+
64
+ export { ErrorBoundary, ErrorPage, initializeSignOzTracing };
65
+ export type { ErrorBoundaryProps, ErrorPageProps, SignOzConfig };