function createMonitoredResource()

in packages/opentelemetry-resource-util/src/index.ts [298:343]


function createMonitoredResource(
  monitoredResourceType: keyof typeof MAPPINGS,
  resourceAttrs: Attributes
): MonitoredResource {
  const mapping: Mapping = MAPPINGS[monitoredResourceType];
  const labels: {[key: string]: string} = {};

  Object.entries(mapping).map(([mrKey, mapConfig]) => {
    let mrValue: AttributeValue | undefined;
    const test: string | undefined = undefined;
    for (const otelKey of mapConfig.otelKeys) {
      if (
        otelKey in resourceAttrs &&
        !resourceAttrs[otelKey]?.toString()?.startsWith(UNKNOWN_SERVICE_PREFIX)
      ) {
        mrValue = resourceAttrs[otelKey];
        break;
      }
    }

    if (
      mrValue === undefined &&
      mapConfig.otelKeys.includes(SEMRESATTRS_SERVICE_NAME)
    ) {
      // The service name started with unknown_service, was ignored above, and we couldn't find
      // a better value for mrValue.
      mrValue = resourceAttrs[SEMRESATTRS_SERVICE_NAME];
    }
    if (mrValue === undefined) {
      mrValue = mapConfig.fallback ?? '';
    }

    // OTel attribute values can be any of string, boolean, number, or array of any of them.
    // Encode any non-strings as json string
    if (typeof mrValue !== 'string') {
      mrValue = JSON.stringify(mrValue);
    }

    labels[mrKey] = mrValue;
  });

  return {
    type: monitoredResourceType,
    labels,
  };
}