function normalizeLabelKey()

in packages/opentelemetry-cloud-monitoring-exporter/src/transform.ts [304:316]


function normalizeLabelKey(key: string): string {
  // Replace characters which are not Letter or Decimal_Number unicode category with "_", see
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes
  //
  // Reimplementation of reference impl in Go:
  // https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/e955c204f4f2bfdc92ff0ad52786232b975efcc2/exporter/metric/metric.go#L595-L604
  let sanitized = key.replace(/[^\p{Letter}\p{Decimal_Number}_]/gu, '_');

  if (sanitized[0].match(/\p{Decimal_Number}/u)) {
    sanitized = 'key_' + sanitized;
  }
  return sanitized;
}