def update_config(m2ee, app_name): if not is_enabled(): logging.debug( "Skipping Dynatrace setup, no DT_TENANTTOKEN found in environment") return logging.info("Enabling Dynatrace") try: manifest = get_manifest() except Exception as e: logging.warning("Failed to parse Dynatrace manifest file", exc_info=True) return # dynamic default default_env.update({"DT_TENANTTOKEN": manifest.get("tenantToken")}) for key, dv in default_env.items(): value = os.environ.get(key, dv) if value: util.upsert_custom_environment_variable(m2ee, key, value) util.upsert_custom_environment_variable(m2ee, "DT_CONNECTION_POINT", get_connection_endpoint()) agent_path = os.path.join(".local", get_agent_path()) if not os.path.exists(agent_path): raise Exception( "Dynatrace Agent not found: {agent_path}".format(agent_path)) util.upsert_javaopts( m2ee, [ "-agentpath:{path}".format(path=os.path.abspath(agent_path)), "-Xshare:off", ], )
def test_upsert_javaopts_list(self): m2ee = M2EEMock() m2ee.config._conf["m2ee"] = {"javaopts": ["-DSomeOption3"]} util.upsert_javaopts(m2ee, ["-DSomeOption1", "-DSomeOption2"]) assert util.get_javaopts(m2ee) == [ "-DSomeOption3", "-DSomeOption1", "-DSomeOption2", ]
def _set_user_provided_java_options(m2ee): options = os.environ.get("JAVA_OPTS", None) if options: try: options = json.loads(options) except ValueError: logging.error( "Failed to parse JAVA_OPTS: invalid JSON", exc_info=True, ) raise util.upsert_javaopts(m2ee, options)
def update_config(m2ee, app_name): if get_new_relic_license_key() is None: logging.debug( "Skipping New Relic setup, no license key found in environment") return logging.info("Adding new relic") util.upsert_custom_environment_variable(m2ee, "NEW_RELIC_LICENSE_KEY", get_new_relic_license_key()) util.upsert_custom_environment_variable(m2ee, "NEW_RELIC_APP_NAME", app_name) util.upsert_custom_environment_variable( m2ee, "NEW_RELIC_LOG", os.path.join(_get_destination_dir(), "newrelic", "agent.log"), ) util.upsert_javaopts( m2ee, "-javaagent:{}".format( os.path.join(_get_destination_dir(), "newrelic", "newrelic.jar")), )
def update_config(m2ee): if not appdynamics_used(): return if not _is_javaagent_installed(): logging.warning("AppDynamics Java Agent isn't installed yet. " "Please redeploy your application to complete " "AppDynamics Java Agent installation.") return logging.info( "AppDynamics Java Agent env. variables are configured. Starting...") util.upsert_javaopts( m2ee, [ "-javaagent:{path}".format(path=APPDYNAMICS_JAVAAGENT_PATH), "-Dappagent.install.dir={path}".format( path=APPDYNAMICS_INSTALL_PATH), ], ) _set_default_env(m2ee)
def _set_jvm_memory(m2ee, vcap): max_memory = os.environ.get("MEMORY_LIMIT") if max_memory: match = re.search("([0-9]+)M", max_memory.upper()) limit = int(match.group(1)) else: limit = int(vcap["limits"]["mem"]) if limit >= 32768: heap_size = limit - 4096 elif limit >= 16384: heap_size = limit - 3072 elif limit >= 8192: heap_size = limit - 2048 elif limit >= 4096: heap_size = limit - 1536 elif limit >= 2048: heap_size = limit - 1024 else: heap_size = int(limit / 2) heap_size = str(heap_size) + "M" env_heap_size = os.environ.get("HEAP_SIZE") if env_heap_size: if int(env_heap_size[:-1]) < limit: heap_size = env_heap_size else: logging.warning( "The specified heap size [{}] is larger than the maximum memory of the " "container ([{}]). Falling back to a heap size of [{}]".format( env_heap_size, str(limit) + "M", heap_size ) ) util.upsert_javaopts(m2ee, "-Xmx%s" % heap_size) util.upsert_javaopts(m2ee, "-Xms%s" % heap_size) util.upsert_javaopts(m2ee, "-XX:MaxMetaspaceSize=256M") logging.debug("Java heap size set to %s", heap_size) if os.getenv("MALLOC_ARENA_MAX"): logging.info("Using provided environment setting for MALLOC_ARENA_MAX") else: util.upsert_custom_environment_variable( m2ee, "MALLOC_ARENA_MAX", str(max(1, limit / 1024) * 2) )
def test_upsert_javaopts_string(self): m2ee = M2EEMock() m2ee.config._conf["m2ee"] = {"javaopts": []} util.upsert_javaopts(m2ee, "-DSomeOption") assert util.get_javaopts(m2ee) == ["-DSomeOption"]
def _enable_mx_java_agent(m2ee): jar = os.path.join( _get_destination_dir(), os.path.basename(util.get_dependency(DEPENDENCY)["artifact"]), ) logging.debug("Checking if Mendix Java Agent is enabled...") if 0 in [ v.find("-javaagent:{}".format(jar)) for v in util.get_javaopts(m2ee) ]: logging.debug("Mendix Java Agent is already enabled") return logging.debug("Enabling Mendix Java Agent...") mx_agent_args = [] if "METRICS_AGENT_CONFIG" in os.environ: mx_agent_args.append( _to_arg( "config", _to_file( "METRICS_AGENT_CONFIG", os.environ.get("METRICS_AGENT_CONFIG"), ), )) elif "MetricsAgentConfig" in util.get_custom_runtime_settings(m2ee): logging.warning( "Passing MetricsAgentConfig with custom runtime " "settings is deprecated. " "Please use the METRICS_AGENT_CONFIG environment variable.") mx_agent_args.append( _to_arg( "config", _to_file( "METRICS_AGENT_CONFIG", util.get_custom_runtime_setting(m2ee, "MetricsAgentConfig"), ), )) # Default config for fallback instrumentation_config = os.path.join(_get_destination_dir(), "DefaultInstrumentationConfig.json") if "METRICS_AGENT_INSTRUMENTATION_CONFIG" in os.environ: instrumentation_config = _to_file( "METRICS_AGENT_INSTRUMENTATION_CONFIG", os.environ.get("METRICS_AGENT_INSTRUMENTATION_CONFIG"), ) mx_agent_args.append( _to_arg("instrumentation_config", instrumentation_config)) mx_agent_args = list(filter(lambda x: x, mx_agent_args)) mx_agent_args_str = f'={",".join(mx_agent_args)}' if mx_agent_args else "" util.upsert_javaopts(m2ee, "-javaagent:{}{}".format(jar, mx_agent_args_str)) # If not explicitly set, # - default to StatsD (MxVersion < metrics.MXVERSION_MICROMETER) # - default to micrometer (MxVersion >= metrics.MXVERSION_MICROMETER) # NOTE : Runtime is moving away from statsd type metrics. If we # have customers preferring statsd format, they would need to configure # StatsD registry for micrometer. # https://docs.mendix.com/refguide/metrics metrics_type = "statsd" if metrics.micrometer_metrics_enabled(runtime.get_runtime_version()): metrics_type = "micrometer" try: util.upsert_custom_runtime_setting(m2ee, "com.mendix.metrics.Type", metrics_type) except ValueError: logging.debug( "com.mendix.metrics.Type custom runtime setting exists, not setting" )
def _set_up_dd_java_agent( m2ee, model_version, runtime_version, jmx_config_files ): jar = os.path.join( SIDECAR_ROOT_DIR, os.path.basename( util.get_dependency(TRACE_AGENT_DEPENDENCY)["artifact"] ), ) # Check if already configured if 0 in [ v.find("-javaagent:{}".format(jar)) for v in util.get_javaopts(m2ee) ]: return # Inject Datadog Java agent # Add tags and explicit reserved tags util.upsert_javaopts( m2ee, [ "-javaagent:{}".format(jar), "-D{}={}".format("dd.tags", _get_datadog_tags(model_version)), "-D{}={}".format("dd.env", get_env_tag()), "-D{}={}".format("dd.service", get_service_tag()), "-D{}={}".format("dd.version", get_version_tag(model_version)), ], ) # Expllicitly set tracing flag util.upsert_javaopts( m2ee, "-D{}={}".format( "dd.trace.enabled", str(bool(_is_tracing_enabled())).lower() ), ) # Explicitly set profiling flag util.upsert_javaopts( m2ee, "-D{}={}".format( "dd.profiling.enabled", str(bool(_is_profiling_enabled(runtime_version))).lower(), ), ) # Extend with tracing options if _is_tracing_enabled(): util.upsert_javaopts( m2ee, "-D{}={}".format("dd.logs.injection", "true"), ) # Extend with database service mapping dbconfig = database.get_config() if dbconfig and "postgres" in dbconfig["DatabaseType"].lower(): util.upsert_javaopts( m2ee, "-D{}={}".format( "dd.service.mapping", "{}:{}.db".format("postgresql", get_service_tag()), ), ) # Extend with JMX options util.upsert_javaopts( m2ee, [ "-D{}={}".format("dd.jmxfetch.enabled", "true"), "-D{}={}".format("dd.jmxfetch.statsd.port", get_statsd_port()), ], ) if jmx_config_files: # Set up Java Agent JMX configuration util.upsert_javaopts( m2ee, "-D{}={}".format("dd.jmxfetch.config", ",".join(jmx_config_files)), )
def _set_jvm_locale(m2ee, java_major_version): # override locale providers for java8 if java_major_version == 8: util.upsert_javaopts(m2ee, "-Djava.locale.providers=JRE,SPI,CLDR")