class JvmNativeMemoryMetrics {
  private static final String JVM_NATIVE_MEMORY_RESERVED_BYTES = "jvm_native_memory_reserved_bytes";
  private static final String JVM_NATIVE_MEMORY_COMMITTED_BYTES = "jvm_native_memory_committed_bytes";

  private static final Pattern pattern = Pattern.compile("\\s*([A-Z][A-Za-z\\s]*[A-Za-z]+).*reserved=(\\d+), committed=(\\d+)");

  /**
   * Package private. For testing only.
   */
  static final AtomicBoolean isEnabled = new AtomicBoolean(true);

  private final PrometheusProperties config;
  private final PlatformMBeanServerAdapter adapter;

  private JvmNativeMemoryMetrics(PrometheusProperties config, PlatformMBeanServerAdapter adapter) {
    this.config = config;
    this.adapter = adapter;
  }

  private void register(PrometheusRegistry registry) {
    // first call will check if enabled and set the flag
    vmNativeMemorySummaryInBytesOrEmpty();
    if (isEnabled.get()) {
      GaugeWithCallback.builder(config)
          .name(JVM_NATIVE_MEMORY_RESERVED_BYTES)
          .help("Reserved bytes of a given JVM. Reserved memory represents the total amount of memory the JVM can potentially use.")
          .unit(Unit.BYTES)
          .labelNames("pool")
          .callback(makeCallback(true))
          .register(registry);

      GaugeWithCallback.builder(config)
          .name(JVM_NATIVE_MEMORY_COMMITTED_BYTES)
          .help("Committed bytes of a given JVM. Committed memory represents the amount of memory the JVM is using right now.")
          .unit(Unit.BYTES)
          .labelNames("pool")
          .callback(makeCallback(false))
          .register(registry);
    }
  }

  private Consumer<GaugeWithCallback.Callback> makeCallback(Boolean reserved) {<FILL_FUNCTION_BODY>}

  private String vmNativeMemorySummaryInBytesOrEmpty() {
    if (!isEnabled.get()) {
      return "";
    }
    try {
      // requires -XX:NativeMemoryTracking=summary
      String summary = adapter.vmNativeMemorySummaryInBytes();
      if (summary.isEmpty() || summary.trim().contains("Native memory tracking is not enabled")) {
        isEnabled.set(false);
        return "";
      } else {
        return summary;
      }
    } catch (Exception ex) {
      // ignore errors
      isEnabled.set(false);
      return "";
    }
  }

  interface PlatformMBeanServerAdapter {
    String vmNativeMemorySummaryInBytes();
  }

  static class DefaultPlatformMBeanServerAdapter implements PlatformMBeanServerAdapter {
    @Override
    public String vmNativeMemorySummaryInBytes() {
      try {
        return (String) ManagementFactory.getPlatformMBeanServer().invoke(
            new ObjectName("com.sun.management:type=DiagnosticCommand"),
            "vmNativeMemory",
            new Object[]{new String[]{"summary", "scale=B"}},
            new String[]{"[Ljava.lang.String;"});
      } catch (ReflectionException | MalformedObjectNameException | InstanceNotFoundException | MBeanException e) {
        throw new IllegalStateException("Native memory tracking is not enabled", e);
      }
    }
  }

  public static Builder builder() {
    return new Builder(PrometheusProperties.get());
  }

  public static Builder builder(PrometheusProperties config) {
    return new Builder(config);
  }

  public static class Builder {

    private final PrometheusProperties config;
    private final PlatformMBeanServerAdapter adapter;

    private Builder(PrometheusProperties config) {
      this(config, new DefaultPlatformMBeanServerAdapter());
    }

    /**
     * Package private. For testing only.
     */
    Builder(PrometheusProperties config, PlatformMBeanServerAdapter adapter) {
      this.config = config;
      this.adapter = adapter;
    }

    public void register() {
      register(PrometheusRegistry.defaultRegistry);
    }

    public void register(PrometheusRegistry registry) {
      new JvmNativeMemoryMetrics(config, adapter).register(registry);
    }
  }
}

    return callback -> {
      String summary = vmNativeMemorySummaryInBytesOrEmpty();
      if (!summary.isEmpty()) {
        Matcher matcher = pattern.matcher(summary);
        while (matcher.find()) {
          String category = matcher.group(1);
          long value;
          if (reserved) {
            value = Long.parseLong(matcher.group(2));
          } else {
            value = Long.parseLong(matcher.group(3));
          }
          callback.call(value, category);
        }
      }
    };
  
<no_super_class>