@VisibleForTesting
  static boolean matchMetric(MetricName metricName, @Nullable Set<String> allowedMetricUrns) {
    if (allowedMetricUrns == null) {
      return true;
    }
    if (metricName instanceof MonitoringInfoMetricName) {
      return allowedMetricUrns.contains(((MonitoringInfoMetricName) metricName).getUrn());
    }
    return false;
  }
@Test
  public void testMatchMetric() {
    String urn = MonitoringInfoConstants.Urns.API_REQUEST_COUNT;
    Map<String, String> labels = new HashMap<String, String>();
    labels.put(MonitoringInfoConstants.Labels.PTRANSFORM, "MyPtransform");
    labels.put(MonitoringInfoConstants.Labels.SERVICE, "BigQuery");
    labels.put(MonitoringInfoConstants.Labels.METHOD, "BigQueryBatchWrite");
    labels.put(MonitoringInfoConstants.Labels.RESOURCE, "Resource");
    labels.put(MonitoringInfoConstants.Labels.BIGQUERY_PROJECT_ID, "MyProject");
    labels.put(MonitoringInfoConstants.Labels.BIGQUERY_DATASET, "MyDataset");
    labels.put(MonitoringInfoConstants.Labels.BIGQUERY_TABLE, "MyTable");

    // MonitoringInfoMetricName will copy labels. So its safe to reuse this reference.
    labels.put(MonitoringInfoConstants.Labels.STATUS, "ok");
    MonitoringInfoMetricName okName = MonitoringInfoMetricName.named(urn, labels);
    labels.put(MonitoringInfoConstants.Labels.STATUS, "not_found");
    MonitoringInfoMetricName notFoundName = MonitoringInfoMetricName.named(urn, labels);

    Set<String> allowedMetricUrns = new HashSet<String>();
    allowedMetricUrns.add(MonitoringInfoConstants.Urns.API_REQUEST_COUNT);
    assertTrue(MetricsContainerImpl.matchMetric(okName, allowedMetricUrns));
    assertTrue(MetricsContainerImpl.matchMetric(notFoundName, allowedMetricUrns));

    MetricName userMetricName = MetricName.named("namespace", "name");
    assertFalse(MetricsContainerImpl.matchMetric(userMetricName, allowedMetricUrns));

    MetricName elementCountName =
        MonitoringInfoMetricName.named(
            MonitoringInfoConstants.Urns.ELEMENT_COUNT,
            Collections.singletonMap("name", "counter"));
    assertFalse(MetricsContainerImpl.matchMetric(elementCountName, allowedMetricUrns));
  }