@echoteam/signoz-react 1.0.4 → 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/dist/index.js CHANGED
@@ -16065,64 +16065,149 @@ function getWebAutoInstrumentations(inputConfigs) {
16065
16065
  return instrumentations;
16066
16066
  }
16067
16067
 
16068
+ // Error class khusus untuk masalah konfigurasi
16069
+ class SignOzConfigurationError extends Error {
16070
+ constructor(message, missingFields) {
16071
+ super(message);
16072
+ this.missingFields = missingFields;
16073
+ this.name = 'SignOzConfigurationError';
16074
+ }
16075
+ }
16076
+ /**
16077
+ * Mendapatkan nilai environment variable dari process.env atau window
16078
+ * @param key - Nama environment variable
16079
+ * @returns Nilai environment variable atau undefined
16080
+ */
16081
+ function getEnvVariable(key) {
16082
+ // Cek process.env terlebih dahulu
16083
+ if (typeof process !== 'undefined' && process.env) {
16084
+ const value = process.env[key];
16085
+ if (value)
16086
+ return value;
16087
+ }
16088
+ // Fallback ke window jika process.env tidak tersedia atau tidak memiliki nilai
16089
+ if (typeof window !== 'undefined') {
16090
+ return window[key];
16091
+ }
16092
+ return undefined;
16093
+ }
16094
+ /**
16095
+ * Memvalidasi konfigurasi SignOz
16096
+ * @param config - Konfigurasi yang akan divalidasi
16097
+ * @throws {SignOzConfigurationError} Jika konfigurasi tidak valid
16098
+ */
16099
+ function validateConfig(config) {
16100
+ const requiredFields = [
16101
+ 'serviceName',
16102
+ 'serviceVersion',
16103
+ 'environment',
16104
+ 'serviceNamespace',
16105
+ 'url'
16106
+ ];
16107
+ const missingFields = requiredFields.filter(field => !config[field]);
16108
+ if (missingFields.length > 0) {
16109
+ const missingFieldsStr = missingFields.join(', ');
16110
+ throw new SignOzConfigurationError(`Konfigurasi SignOz tidak lengkap. Field yang diperlukan: ${missingFieldsStr}`, missingFields);
16111
+ }
16112
+ // Validasi traceSampleRate jika ada
16113
+ if (config.traceSampleRate !== undefined) {
16114
+ const rate = Number(config.traceSampleRate);
16115
+ if (isNaN(rate) || rate < 0 || rate > 1) {
16116
+ throw new SignOzConfigurationError('traceSampleRate harus berupa angka antara 0 dan 1');
16117
+ }
16118
+ }
16119
+ // Validasi URL
16120
+ try {
16121
+ new URL(config.url);
16122
+ }
16123
+ catch (e) {
16124
+ throw new SignOzConfigurationError(`URL tidak valid: ${config.url}`);
16125
+ }
16126
+ }
16068
16127
  /**
16069
16128
  * Inisialisasi SignOz tracing untuk aplikasi React
16070
16129
  * @param config - Konfigurasi SignOz (opsional, akan menggunakan environment variables jika tidak disediakan)
16130
+ * @throws {SignOzConfigurationError} Jika konfigurasi tidak valid
16071
16131
  */
16072
16132
  function initializeSignOzTracing(config) {
16073
- // Gunakan config yang disediakan atau fallback ke environment variables
16074
- const serviceName = (config === null || config === void 0 ? void 0 : config.serviceName) || window.REACT_APP_SIGNOZ_SERVICE_NAME;
16075
- const serviceVersion = (config === null || config === void 0 ? void 0 : config.serviceVersion) || window.REACT_APP_SIGNOZ_SERVICE_VERSION;
16076
- const environment = (config === null || config === void 0 ? void 0 : config.environment) || window.REACT_APP_SIGNOZ_ENV;
16077
- const serviceNamespace = (config === null || config === void 0 ? void 0 : config.serviceNamespace) || window.REACT_APP_SIGNOZ_SERVICE_NAMESPACE;
16078
- const url = (config === null || config === void 0 ? void 0 : config.url) || window.REACT_APP_SIGNOZ_URL;
16079
- const headers = (config === null || config === void 0 ? void 0 : config.headers) || {};
16080
- // Validasi konfigurasi yang diperlukan
16081
- if (!serviceName || !serviceVersion || !environment || !serviceNamespace || !url) {
16082
- console.error('SignOz: Konfigurasi tidak lengkap. Pastikan semua environment variables atau config disediakan.');
16083
- return;
16133
+ try {
16134
+ // Gabungkan config dengan environment variables
16135
+ const effectiveConfig = {
16136
+ serviceName: (config === null || config === void 0 ? void 0 : config.serviceName) || getEnvVariable('REACT_APP_SIGNOZ_SERVICE_NAME'),
16137
+ serviceVersion: (config === null || config === void 0 ? void 0 : config.serviceVersion) || getEnvVariable('REACT_APP_SIGNOZ_SERVICE_VERSION'),
16138
+ environment: (config === null || config === void 0 ? void 0 : config.environment) || getEnvVariable('REACT_APP_SIGNOZ_ENV'),
16139
+ serviceNamespace: (config === null || config === void 0 ? void 0 : config.serviceNamespace) || getEnvVariable('REACT_APP_SIGNOZ_SERVICE_NAMESPACE'),
16140
+ url: (config === null || config === void 0 ? void 0 : config.url) || getEnvVariable('REACT_APP_SIGNOZ_URL'),
16141
+ headers: (config === null || config === void 0 ? void 0 : config.headers) || {},
16142
+ traceSampleRate: (config === null || config === void 0 ? void 0 : config.traceSampleRate) !== undefined
16143
+ ? config.traceSampleRate
16144
+ : getEnvVariable('REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE')
16145
+ ? parseFloat(getEnvVariable('REACT_APP_SIGNOZ_TRACE_SAMPLE_RATE'))
16146
+ : 1.0
16147
+ };
16148
+ // Validasi konfigurasi
16149
+ validateConfig(effectiveConfig);
16150
+ // Define resource and service attributes
16151
+ const resource = new Resource({
16152
+ 'service.name': effectiveConfig.serviceName,
16153
+ 'service.version': effectiveConfig.serviceVersion,
16154
+ 'deployment.environment': effectiveConfig.environment,
16155
+ 'service.namespace': effectiveConfig.serviceNamespace,
16156
+ });
16157
+ // Set up the OTLP trace exporter
16158
+ const exporter = new OTLPTraceExporter({
16159
+ url: effectiveConfig.url,
16160
+ headers: effectiveConfig.headers,
16161
+ });
16162
+ // Set up the span processor
16163
+ const processor = new BatchSpanProcessor(exporter);
16164
+ // Create and configure the WebTracerProvider
16165
+ const provider = new WebTracerProvider({
16166
+ resource: resource,
16167
+ spanProcessors: [processor],
16168
+ sampler: new ParentBasedSampler({
16169
+ root: new TraceIdRatioBasedSampler(effectiveConfig.traceSampleRate)
16170
+ })
16171
+ });
16172
+ // Register the tracer provider with the context manager
16173
+ provider.register({
16174
+ contextManager: new ZoneContextManager(),
16175
+ });
16176
+ // Set up automatic instrumentation for web APIs
16177
+ registerInstrumentations({
16178
+ instrumentations: [
16179
+ getWebAutoInstrumentations({
16180
+ '@opentelemetry/instrumentation-xml-http-request': {
16181
+ propagateTraceHeaderCorsUrls: [
16182
+ /.+/g, // Regex to match your backend URLs
16183
+ ],
16184
+ },
16185
+ '@opentelemetry/instrumentation-fetch': {
16186
+ propagateTraceHeaderCorsUrls: [
16187
+ /.+/g, // Regex to match your backend URLs
16188
+ ],
16189
+ },
16190
+ }),
16191
+ ],
16192
+ });
16193
+ console.log('SignOz tracing berhasil diinisialisasi');
16194
+ }
16195
+ catch (error) {
16196
+ if (error instanceof SignOzConfigurationError) {
16197
+ console.error(`[SignOz Error] ${error.message}`);
16198
+ if (error.missingFields) {
16199
+ console.error('Field yang hilang:');
16200
+ error.missingFields.forEach(field => {
16201
+ const envVar = `REACT_APP_SIGNOZ_${field.toUpperCase()}`;
16202
+ console.error(`- ${field} (Environment Variable: ${envVar})`);
16203
+ });
16204
+ }
16205
+ }
16206
+ else {
16207
+ console.error('[SignOz Error] Terjadi kesalahan saat menginisialisasi tracing:', error);
16208
+ }
16209
+ throw error;
16084
16210
  }
16085
- // Define resource and service attributes
16086
- const resource = new Resource({
16087
- 'service.name': serviceName,
16088
- 'service.version': serviceVersion,
16089
- 'deployment.environment': environment,
16090
- 'service.namespace': serviceNamespace,
16091
- });
16092
- // Set up the OTLP trace exporter
16093
- const exporter = new OTLPTraceExporter({
16094
- url: url,
16095
- headers: headers,
16096
- });
16097
- // Set up the span processor
16098
- const processor = new BatchSpanProcessor(exporter);
16099
- // Create and configure the WebTracerProvider
16100
- const provider = new WebTracerProvider({
16101
- resource: resource,
16102
- spanProcessors: [processor],
16103
- });
16104
- // Register the tracer provider with the context manager
16105
- provider.register({
16106
- contextManager: new ZoneContextManager(),
16107
- });
16108
- // Set up automatic instrumentation for web APIs
16109
- registerInstrumentations({
16110
- instrumentations: [
16111
- getWebAutoInstrumentations({
16112
- '@opentelemetry/instrumentation-xml-http-request': {
16113
- propagateTraceHeaderCorsUrls: [
16114
- /.+/g, // Regex to match your backend URLs
16115
- ],
16116
- },
16117
- '@opentelemetry/instrumentation-fetch': {
16118
- propagateTraceHeaderCorsUrls: [
16119
- /.+/g, // Regex to match your backend URLs
16120
- ],
16121
- },
16122
- }),
16123
- ],
16124
- });
16125
- console.log('SignOz tracing berhasil diinisialisasi');
16126
16211
  }
16127
16212
 
16128
16213
  const ErrorBoundaryContext = React2.createContext(null);