All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.contrib.jfr.connection.MapConfiguration Maven / Gradle / Ivy

There is a newer version: 1.42.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.jfr.connection;

import java.io.IOException;
import java.util.Map;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.OpenDataException;
import javax.management.openmbean.TabularData;

/** A {@code RecordingConfiguration} defined from a map. */
public class MapConfiguration implements RecordingConfiguration {

  private final Map configuration;

  /**
   * Sets a configuration from a Map
   *
   * @param configuration A map defining the JFR events to register. For example:
   *     {jdk.ObjectAllocationInNewTLAB#enabled=true, jdk.ObjectAllocationOutsideTLAB#enabled=true}
   */
  public MapConfiguration(Map configuration) {
    this.configuration = configuration;
  }

  @Override
  public void invokeSetConfiguration(
      long id, MBeanServerConnection mBeanServerConnection, ObjectName objectName)
      throws IOException, JfrConnectionException {
    if (!configuration.isEmpty()) {
      try {
        TabularData configAsTabular = OpenDataUtils.makeOpenData(configuration);
        Object[] args = new Object[] {id, configAsTabular};
        String[] argTypes = new String[] {long.class.getName(), TabularData.class.getName()};
        mBeanServerConnection.invoke(objectName, "setRecordingSettings", args, argTypes);
      } catch (OpenDataException
          | InstanceNotFoundException
          | MBeanException
          | ReflectionException e) {
        throw JfrConnectionException.canonicalJfrConnectionException(
            getClass(), "invokeSetConfiguration", e);
      }
    }
  }

  @Override
  public String toString() {
    return configuration.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy