Exemplo n.º 1
0
    def _get_db_conn(self):
        if self.db and self.db.closed != 0:
            self.db.close()
            self.db = None

        if not self.db:
            # get_database config may return None or empty
            db_config = database.get_config()
            if not db_config or "DatabaseType" not in db_config:
                raise ValueError(
                    "Database not set as VCAP or DATABASE_URL. Check "
                    "documentation to see supported configuration options.")
            if db_config["DatabaseType"] != "PostgreSQL":
                raise Exception("Metrics only supports postgresql, not %s" %
                                db_config["DatabaseType"])

            host, port = self._get_db_host_and_port(db_config["DatabaseHost"])
            self.db = psycopg2.connect(
                "options='-c statement_timeout=60s'",
                database=db_config["DatabaseName"],
                user=db_config["DatabaseUserName"],
                password=db_config["DatabasePassword"],
                host=host,
                port=port,
                connect_timeout=3,
            )
            self.db.set_isolation_level(
                psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        return self.db
Exemplo n.º 2
0
    def _get_database_tcp_latency(self, timeout: float = 5):
        db_config = database.get_config()
        host, port = self._get_db_host_and_port(db_config["DatabaseHost"])
        # New Socket and Time out
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(timeout)

        # Start a timer
        s_start = timer()

        # Try to Connect
        try:
            sock.connect((host, int(port)))
            sock.shutdown(socket.SHUT_RD)
            sock.close()

        # If something bad happens, the latency is None
        except socket.timeout:
            return None
        except OSError:
            return None

        # Stop Timer
        s_stop = timer()
        s_runtime = "%.2f" % (1000 * (s_stop - s_start))

        return s_runtime
Exemplo n.º 3
0
 def _get_database_table_size(self):
     conn = self._get_db_conn()
     db_config = database.get_config()
     with conn.cursor() as cursor:
         cursor.execute("SELECT pg_database_size('%s');" %
                        (db_config["DatabaseName"], ))
         rows = cursor.fetchall()
         return int_or_default(rows[0][0])
 def test_inline_certs(self):
     self.cert_map = CertGen().cert_map
     c = UrlDatabaseConfiguration
     native_params = {
         c.SSLCERT: self.get_cert("postgresql.crt"),
         c.SSLROOTCERT: self.get_cert("root.crt"),
         c.SSLKEY: self.get_cert("postgresql.rsa.key"),
     }
     parts = urlparse("postgres://*****:*****@host/database")
     parts = parts._replace(query=urlencode(native_params))
     native_url = urlunparse(parts)
     with mock.patch.dict(os.environ, {"DATABASE_URL": native_url},
                          clear=True):
         config = get_config()
         assert config
         assert config["DatabaseType"] == "PostgreSQL"
         native_params[c.SSLKEY] = self.get_cert("postgresql.pk8")
         jdbc_params = parse_qs(urlparse(config["DatabaseJdbcUrl"]).query)
         self.cmp_cert(native_params, jdbc_params, c.SSLCERT)
         self.cmp_cert(native_params, jdbc_params, c.SSLROOTCERT)
         self.cmp_cert(native_params, jdbc_params, c.SSLKEY)
Exemplo n.º 5
0
    def _get_database_mutations(self):
        conn = self._get_db_conn()
        db_config = database.get_config()

        with conn.cursor() as cursor:
            cursor.execute("SELECT xact_commit, "
                           "       xact_rollback, "
                           "       tup_inserted, "
                           "       tup_updated, "
                           "       tup_deleted "
                           "FROM pg_stat_database "
                           "WHERE datname = '%s';" %
                           (db_config["DatabaseName"], ))
            rows = cursor.fetchall()
            return {
                "xact_commit": int_or_default(rows[0][0]),
                "xact_rollback": int_or_default(rows[0][1]),
                "tup_inserted": int_or_default(rows[0][2]),
                "tup_updated": int_or_default(rows[0][3]),
                "tup_deleted": int_or_default(rows[0][4]),
            }
        return None
Exemplo n.º 6
0
def _set_up_environment():
    if "MXRUNTIME_License.SubscriptionSecret" in os.environ:
        os.environ["MXUMS_SUBSCRIPTION_SECRET"] = os.environ[
            "MXRUNTIME_License.SubscriptionSecret"]
    if "MXRUNTIME_License.LicenseServerURL" in os.environ:
        os.environ["MXUMS_LICENSESERVER_URL"] = os.environ[
            "MXRUNTIME_License.LicenseServerURL"]
    if "MXRUNTIME_License.EnvironmentName" in os.environ:
        os.environ["MXUMS_ENVIRONMENT_NAME"] = os.environ[
            "MXRUNTIME_License.EnvironmentName"]
    dbconfig = database.get_config()
    if dbconfig:
        os.environ[
            "MXUMS_DB_CONNECTION_URL"] = "postgres://{}:{}@{}/{}".format(
                dbconfig["DatabaseUserName"],
                dbconfig["DatabasePassword"],
                dbconfig["DatabaseHost"],
                dbconfig["DatabaseName"],
            )
    project_id = _get_project_id(os.path.join(SIDECAR_DIR,
                                              SIDECAR_CONFIG_FILE))
    os.environ["MXUMS_PROJECT_ID"] = project_id
    e = dict(os.environ.copy())
    return e
Exemplo n.º 7
0
        databroker.update_config(m2ee)
        databroker.business_events.update_config(m2ee,
                                                 util.get_vcap_services_data())

        # Start components and runtime
        telegraf.run(runtime_version)
        datadog.run(model_version, runtime_version)
        metering.run()
        logs.run(m2ee)
        runtime.run(m2ee, logs.get_loglevels())
        metrics.run(m2ee)
        appdynamics.run()
        nginx.run()

        # Wait for the runtime to be ready before starting Databroker
        if databroker.is_enabled():
            runtime.await_database_ready(m2ee)
            databroker_processes.run(database.get_config())
    except RuntimeError as re:
        # Only the runtime throws RuntimeErrors (no pun intended)
        # Don't use the stack trace for these
        logging.error("Starting application failed: %s", re)
        sys.exit(1)
    except Exception:
        ex = traceback.format_exc()
        logging.error("Starting application failed. %s", ex)
        sys.exit(1)

    # Wait loop for runtime termination
    runtime.await_termination(m2ee)
 def test_database_url(self):
     config = get_config()
     assert config
     assert config["DatabaseType"] == "PostgreSQL"
 def test_invalid_jdbc_parameters(self):
     config = get_config()
     assert "tcpKeepAlive=false" not in config["DatabaseJdbcUrl"]
 def test_mx_runtime_db_config(self):
     # Test if MXRUNTIME variables are set up if no database configuration is returned
     # based on DATABASE_URL or VCAP_SERVICES
     config = get_config()
     assert not config
 def test_no_setup(self):
     with self.assertRaises(RuntimeError):
         get_config()
Exemplo n.º 12
0
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)),
        )