Skip to content

Data Collection Masking

If you only need to perform global modifications or masking based on field names, it is recommended to use setDataModifier as it has lower performance overhead. Use setLineDataModifier only when you need to combine multiple fields within a single data record for judgment or replacement.

Do not perform complex, time-consuming, or asynchronous operations inside the callback, as this will affect the SDK's data writing performance.

DataModifier processes tags and fields from original Logs, native RUM, and WebView RUM. Returning null indicates that the original value is retained.

LineDataModifier is executed after the SDK tags and fields are merged. The callback receives the complete Map of the data record. The return value is a patch containing only the fields to be replaced: only fields that already exist in the original data will be replaced; returning null, an empty Map, a field value of null, or a non-existent field will not modify the data.

import {
  DataModifier,
  DataModifierValue,
  FTSDK,
  FTSDKConfig,
  LineDataModifier
} from '@guancecloud/ft_sdk/Index';

class FieldMasker implements DataModifier {
  /**
   * Modify a single tag or field.
   * Return null to indicate that the original value is not modified.
   */
  modify(key: string, _value: DataModifierValue): DataModifierValue | null {
    if (key === 'device_uuid' || key === 'authorization') {
      return '[masked]';
    }
    return null;
  }
}

class RecordMasker implements LineDataModifier {
  /**
   * Modify a single data record.
   * measurement is the measurement type, e.g., view, action, resource, longtask, error, or df_rum_harmonyos_log.
   * Return a Map containing only the fields to be modified; return null or an empty Map to not modify the data.
   */
  modify(measurement: string, data: Map<string, DataModifierValue>): Map<string, DataModifierValue> | null {
    const patch = new Map<string, DataModifierValue>();

    if (measurement === 'view' && data.has('view_url')) {
      patch.set('view_url', '[masked]');
    }
    if (data.get('message') === 'sensitive message') {
      patch.set('message', '[masked]');
    }

    return patch.size > 0 ? patch : null;
  }
}

const sdkConfig = FTSDKConfig.builder(datawayUrl, clientToken)
  .setDataModifier(new FieldMasker())
  .setLineDataModifier(new RecordMasker());

FTSDK.install(sdkConfig, this.context);

When both data modification and blacklist filtering are configured, the execution order is: DataModifierLineDataModifier → Blacklist Filtering → Write to Local Cache. Both local and remote blacklist rules will be evaluated based on the modified data.

DataModifierValue supports string, number, boolean, and object; objects will be serialized to strings before being written to the local cache. Neither type of callback can add or delete fields, nor can it move data between tags and fields.

Feedback

Is this page helpful? ×