@echoteam/signoz-react 1.0.3 → 1.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2024 CODR Team
3
+ Copyright (c) 2024 echoteam
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -30,6 +30,7 @@ REACT_APP_SIGNOZ_SERVICE_VERSION=1.0.0
30
30
  REACT_APP_SIGNOZ_ENV=production
31
31
  REACT_APP_SIGNOZ_SERVICE_NAMESPACE=frontend
32
32
  REACT_APP_SIGNOZ_URL=https://your-signoz-instance.com/v1/traces
33
+ REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE=1.0
33
34
  ```
34
35
 
35
36
  ### Inisialisasi
@@ -49,7 +50,8 @@ initializeSignOzTracing({
49
50
  url: 'https://your-signoz-instance.com/v1/traces',
50
51
  headers: {
51
52
  'Authorization': 'Bearer your-token'
52
- }
53
+ },
54
+ traceSampleRate: 0.5 // Sampling 50% trace
53
55
  });
54
56
  ```
55
57
 
@@ -199,6 +201,8 @@ Inisialisasi SignOz tracing untuk aplikasi React.
199
201
  - `serviceNamespace`: Namespace layanan
200
202
  - `url`: URL endpoint SignOz
201
203
  - `headers`: Headers tambahan untuk request
204
+ - `traceSampleRate`: Sampling rate tracing (0.0 - 1.0, default: 1.0 jika tidak diisi)
205
+ - Bisa juga diatur lewat environment variable `REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE`
202
206
 
203
207
  ### `ErrorBoundary`
204
208
 
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ interface SignOzConfig {
6
6
  serviceNamespace: string;
7
7
  url: string;
8
8
  headers?: Record<string, string>;
9
+ traceSampleRate?: number;
9
10
  }
10
11
  declare global {
11
12
  interface Window {
@@ -14,11 +15,13 @@ declare global {
14
15
  REACT_APP_SIGNOZ_ENV: string;
15
16
  REACT_APP_SIGNOZ_SERVICE_NAMESPACE: string;
16
17
  REACT_APP_SIGNOZ_URL: string;
18
+ REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE: string;
17
19
  }
18
20
  }
19
21
  /**
20
22
  * Inisialisasi SignOz tracing untuk aplikasi React
21
23
  * @param config - Konfigurasi SignOz (opsional, akan menggunakan environment variables jika tidak disediakan)
24
+ * @throws {SignOzConfigurationError} Jika konfigurasi tidak valid
22
25
  */
23
26
  declare function initializeSignOzTracing(config?: SignOzConfig): void;
24
27
 
package/dist/index.esm.js CHANGED
@@ -16045,64 +16045,149 @@ function getWebAutoInstrumentations(inputConfigs) {
16045
16045
  return instrumentations;
16046
16046
  }
16047
16047
 
16048
+ // Error class khusus untuk masalah konfigurasi
16049
+ class SignOzConfigurationError extends Error {
16050
+ constructor(message, missingFields) {
16051
+ super(message);
16052
+ this.missingFields = missingFields;
16053
+ this.name = 'SignOzConfigurationError';
16054
+ }
16055
+ }
16056
+ /**
16057
+ * Mendapatkan nilai environment variable dari process.env atau window
16058
+ * @param key - Nama environment variable
16059
+ * @returns Nilai environment variable atau undefined
16060
+ */
16061
+ function getEnvVariable(key) {
16062
+ // Cek process.env terlebih dahulu
16063
+ if (typeof process !== 'undefined' && process.env) {
16064
+ const value = process.env[key];
16065
+ if (value)
16066
+ return value;
16067
+ }
16068
+ // Fallback ke window jika process.env tidak tersedia atau tidak memiliki nilai
16069
+ if (typeof window !== 'undefined') {
16070
+ return window[key];
16071
+ }
16072
+ return undefined;
16073
+ }
16074
+ /**
16075
+ * Memvalidasi konfigurasi SignOz
16076
+ * @param config - Konfigurasi yang akan divalidasi
16077
+ * @throws {SignOzConfigurationError} Jika konfigurasi tidak valid
16078
+ */
16079
+ function validateConfig(config) {
16080
+ const requiredFields = [
16081
+ 'serviceName',
16082
+ 'serviceVersion',
16083
+ 'environment',
16084
+ 'serviceNamespace',
16085
+ 'url'
16086
+ ];
16087
+ const missingFields = requiredFields.filter(field => !config[field]);
16088
+ if (missingFields.length > 0) {
16089
+ const missingFieldsStr = missingFields.join(', ');
16090
+ throw new SignOzConfigurationError(`Konfigurasi SignOz tidak lengkap. Field yang diperlukan: ${missingFieldsStr}`, missingFields);
16091
+ }
16092
+ // Validasi traceSampleRate jika ada
16093
+ if (config.traceSampleRate !== undefined) {
16094
+ const rate = Number(config.traceSampleRate);
16095
+ if (isNaN(rate) || rate < 0 || rate > 1) {
16096
+ throw new SignOzConfigurationError('traceSampleRate harus berupa angka antara 0 dan 1');
16097
+ }
16098
+ }
16099
+ // Validasi URL
16100
+ try {
16101
+ new URL(config.url);
16102
+ }
16103
+ catch (e) {
16104
+ throw new SignOzConfigurationError(`URL tidak valid: ${config.url}`);
16105
+ }
16106
+ }
16048
16107
  /**
16049
16108
  * Inisialisasi SignOz tracing untuk aplikasi React
16050
16109
  * @param config - Konfigurasi SignOz (opsional, akan menggunakan environment variables jika tidak disediakan)
16110
+ * @throws {SignOzConfigurationError} Jika konfigurasi tidak valid
16051
16111
  */
16052
16112
  function initializeSignOzTracing(config) {
16053
- // Gunakan config yang disediakan atau fallback ke environment variables
16054
- const serviceName = (config === null || config === void 0 ? void 0 : config.serviceName) || window.REACT_APP_SIGNOZ_SERVICE_NAME;
16055
- const serviceVersion = (config === null || config === void 0 ? void 0 : config.serviceVersion) || window.REACT_APP_SIGNOZ_SERVICE_VERSION;
16056
- const environment = (config === null || config === void 0 ? void 0 : config.environment) || window.REACT_APP_SIGNOZ_ENV;
16057
- const serviceNamespace = (config === null || config === void 0 ? void 0 : config.serviceNamespace) || window.REACT_APP_SIGNOZ_SERVICE_NAMESPACE;
16058
- const url = (config === null || config === void 0 ? void 0 : config.url) || window.REACT_APP_SIGNOZ_URL;
16059
- const headers = (config === null || config === void 0 ? void 0 : config.headers) || {};
16060
- // Validasi konfigurasi yang diperlukan
16061
- if (!serviceName || !serviceVersion || !environment || !serviceNamespace || !url) {
16062
- console.error('SignOz: Konfigurasi tidak lengkap. Pastikan semua environment variables atau config disediakan.');
16063
- return;
16113
+ try {
16114
+ // Gabungkan config dengan environment variables
16115
+ const effectiveConfig = {
16116
+ serviceName: (config === null || config === void 0 ? void 0 : config.serviceName) || getEnvVariable('REACT_APP_SIGNOZ_SERVICE_NAME'),
16117
+ serviceVersion: (config === null || config === void 0 ? void 0 : config.serviceVersion) || getEnvVariable('REACT_APP_SIGNOZ_SERVICE_VERSION'),
16118
+ environment: (config === null || config === void 0 ? void 0 : config.environment) || getEnvVariable('REACT_APP_SIGNOZ_ENV'),
16119
+ serviceNamespace: (config === null || config === void 0 ? void 0 : config.serviceNamespace) || getEnvVariable('REACT_APP_SIGNOZ_SERVICE_NAMESPACE'),
16120
+ url: (config === null || config === void 0 ? void 0 : config.url) || getEnvVariable('REACT_APP_SIGNOZ_URL'),
16121
+ headers: (config === null || config === void 0 ? void 0 : config.headers) || {},
16122
+ traceSampleRate: (config === null || config === void 0 ? void 0 : config.traceSampleRate) !== undefined
16123
+ ? config.traceSampleRate
16124
+ : getEnvVariable('REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE')
16125
+ ? parseFloat(getEnvVariable('REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE'))
16126
+ : 1.0
16127
+ };
16128
+ // Validasi konfigurasi
16129
+ validateConfig(effectiveConfig);
16130
+ // Define resource and service attributes
16131
+ const resource = new Resource({
16132
+ 'service.name': effectiveConfig.serviceName,
16133
+ 'service.version': effectiveConfig.serviceVersion,
16134
+ 'deployment.environment': effectiveConfig.environment,
16135
+ 'service.namespace': effectiveConfig.serviceNamespace,
16136
+ });
16137
+ // Set up the OTLP trace exporter
16138
+ const exporter = new OTLPTraceExporter({
16139
+ url: effectiveConfig.url,
16140
+ headers: effectiveConfig.headers,
16141
+ });
16142
+ // Set up the span processor
16143
+ const processor = new BatchSpanProcessor(exporter);
16144
+ // Create and configure the WebTracerProvider
16145
+ const provider = new WebTracerProvider({
16146
+ resource: resource,
16147
+ spanProcessors: [processor],
16148
+ sampler: new ParentBasedSampler({
16149
+ root: new TraceIdRatioBasedSampler(effectiveConfig.traceSampleRate)
16150
+ })
16151
+ });
16152
+ // Register the tracer provider with the context manager
16153
+ provider.register({
16154
+ contextManager: new ZoneContextManager(),
16155
+ });
16156
+ // Set up automatic instrumentation for web APIs
16157
+ registerInstrumentations({
16158
+ instrumentations: [
16159
+ getWebAutoInstrumentations({
16160
+ '@opentelemetry/instrumentation-xml-http-request': {
16161
+ propagateTraceHeaderCorsUrls: [
16162
+ /.+/g, // Regex to match your backend URLs
16163
+ ],
16164
+ },
16165
+ '@opentelemetry/instrumentation-fetch': {
16166
+ propagateTraceHeaderCorsUrls: [
16167
+ /.+/g, // Regex to match your backend URLs
16168
+ ],
16169
+ },
16170
+ }),
16171
+ ],
16172
+ });
16173
+ console.log('SignOz tracing berhasil diinisialisasi');
16174
+ }
16175
+ catch (error) {
16176
+ if (error instanceof SignOzConfigurationError) {
16177
+ console.error(`[SignOz Error] ${error.message}`);
16178
+ if (error.missingFields) {
16179
+ console.error('Field yang hilang:');
16180
+ error.missingFields.forEach(field => {
16181
+ const envVar = `REACT_APP_SIGNOZ_${field.toUpperCase()}`;
16182
+ console.error(`- ${field} (Environment Variable: ${envVar})`);
16183
+ });
16184
+ }
16185
+ }
16186
+ else {
16187
+ console.error('[SignOz Error] Terjadi kesalahan saat menginisialisasi tracing:', error);
16188
+ }
16189
+ throw error;
16064
16190
  }
16065
- // Define resource and service attributes
16066
- const resource = new Resource({
16067
- 'service.name': serviceName,
16068
- 'service.version': serviceVersion,
16069
- 'deployment.environment': environment,
16070
- 'service.namespace': serviceNamespace,
16071
- });
16072
- // Set up the OTLP trace exporter
16073
- const exporter = new OTLPTraceExporter({
16074
- url: url,
16075
- headers: headers,
16076
- });
16077
- // Set up the span processor
16078
- const processor = new BatchSpanProcessor(exporter);
16079
- // Create and configure the WebTracerProvider
16080
- const provider = new WebTracerProvider({
16081
- resource: resource,
16082
- spanProcessors: [processor],
16083
- });
16084
- // Register the tracer provider with the context manager
16085
- provider.register({
16086
- contextManager: new ZoneContextManager(),
16087
- });
16088
- // Set up automatic instrumentation for web APIs
16089
- registerInstrumentations({
16090
- instrumentations: [
16091
- getWebAutoInstrumentations({
16092
- '@opentelemetry/instrumentation-xml-http-request': {
16093
- propagateTraceHeaderCorsUrls: [
16094
- /.+/g, // Regex to match your backend URLs
16095
- ],
16096
- },
16097
- '@opentelemetry/instrumentation-fetch': {
16098
- propagateTraceHeaderCorsUrls: [
16099
- /.+/g, // Regex to match your backend URLs
16100
- ],
16101
- },
16102
- }),
16103
- ],
16104
- });
16105
- console.log('SignOz tracing berhasil diinisialisasi');
16106
16191
  }
16107
16192
 
16108
16193
  const ErrorBoundaryContext = createContext(null);