集成Quarkus中的OpenTelemetry与数据库

huangapple go评论60阅读模式
英文:

Integrate db with OpenTelemetry in quarkus

问题

我正在尝试将Quarkus应用程序与OpenTelemetry集成。为此,我希望为JDBC进行仪表化。在文档https://quarkus.io/guides/opentelemetry#jdbc中提到要使用https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/jdbc/library#driver-way。但由于项目配置的原因,我无法使用这种方法。但在文档中还有另一种方法。https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/jdbc/library#datasource-way。我应该如何在Quarkus中实现这种解决方案。

有人能帮我吗?

这是我的liquibase运行文件。

package .....infrastructure.data;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;

import io.quarkus.runtime.LaunchMode;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkus.runtime.StartupEvent;
import io.quarkus.runtime.util.ExceptionUtil;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import io.quarkus.logging.Log;

@ApplicationScoped
public class LiquibaseRunner {

    @ConfigProperty(name = "quarkus.datasource.jdbc.url")
    String datasourceUrl;

    @ConfigProperty(name = "quarkus.datasource.username")
    String datasourceUsername;

    @ConfigProperty(name = "quarkus.datasource.password")
    String datasourcePassword;

    @ConfigProperty(name = "quarkus.liquibase.change-log")
    String changeLogLocation;

    public void onApplicationStart(@Observes StartupEvent event) {
        LaunchMode mode = io.quarkus.runtime.LaunchMode.current();
        if (mode != LaunchMode.TEST) {
            this.runMigration();
        } else {
            Log.info("Skipping DB migrations in TEST mode.");
        }
    }

    public void runMigration() {
        Log.info("Migrating DB " + datasourceUrl);
        Liquibase liquibase = null;
        try {
            ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
            DatabaseConnection conn = DatabaseFactory.getInstance().openConnection(datasourceUrl, datasourceUsername, datasourcePassword, null, resourceAccessor);

            liquibase = new Liquibase(changeLogLocation, resourceAccessor, conn);
            liquibase.update(new Contexts(), new LabelExpression());
        } catch (Exception e) {
            Log.error("Liquibase Migration Exception: " + ExceptionUtil.generateStackTrace(e));
        } finally {
            if (liquibase != null) {
                try {
                    liquibase.close();
                } catch (LiquibaseException e) {
                    Log.info(e.getMessage());
                }
            }
        }
    }

}

这是您提供的代码的翻译部分。如果您需要更多帮助,请告诉我。

英文:

I am try to integrate quarkus application with OpenTelemetry. For that I want Instrumentation for JDBC. In the documentation https://quarkus.io/guides/opentelemetry#jdbc it mentions to use https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/jdbc/library#driver-way . But bucause of the project configuration I can use this. But in the documentation there is another way. https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/jdbc/library#datasource-way . How can I implement this solution in quarkus.

Can some one help me to this?

This is my liquibase runner file

package .....infrastructure.data;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import io.quarkus.runtime.LaunchMode;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.runtime.util.ExceptionUtil;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import io.quarkus.logging.Log;
@ApplicationScoped
public class LiquibaseRunner {
@ConfigProperty(name = "quarkus.datasource.jdbc.url")
String datasourceUrl;
@ConfigProperty(name = "quarkus.datasource.username")
String datasourceUsername;
@ConfigProperty(name = "quarkus.datasource.password")
String datasourcePassword;
@ConfigProperty(name = "quarkus.liquibase.change-log")
String changeLogLocation;
public void onApplicationStart(@Observes StartupEvent even) {
LaunchMode mode = io.quarkus.runtime.LaunchMode.current();
if(mode != LaunchMode.TEST) {
this.runMigration();
} else {
Log.info("Skipping DB migrations in TEST mode.");
}
}
public void runMigration() {
Log.info("Migrating DB " + datasourceUrl);
Liquibase liquibase = null;
try {
ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader());
DatabaseConnection conn = DatabaseFactory.getInstance().openConnection(datasourceUrl, datasourceUsername, datasourcePassword, null, resourceAccessor);
liquibase = new Liquibase(changeLogLocation, resourceAccessor, conn);
liquibase.update(new Contexts(), new LabelExpression());
} catch (Exception e) {
Log.error("Liquibase Migration Exception: " + ExceptionUtil.generateStackTrace(e));
}
finally {
if(liquibase!=null)
{
try {
liquibase.close();
} catch (LiquibaseException e) {
Log.info(e.getMessage());
}
}
}
}
}

答案1

得分: 1

这应该有助于你。你需要准备好你的 DataSource,然后还要使用它的 Connection


public void runMigration() {
    Log.info("迁移数据库 " + datasourceUrl);

    try (OpenTelemetryDataSource dataSource = dataSource();
            Connection connection = dataSource.getConnection();
            ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(Thread.currentThread()
                    .getContextClassLoader());
            DatabaseConnection conn = DatabaseFactory.getInstance()
                    .findCorrectDatabaseImplementation(new JdbcConnection(connection))
                    .getConnection();
            Liquibase liquibase = new Liquibase(changeLogLocation, resourceAccessor, conn)) {

        liquibase.update(new Contexts(), new LabelExpression());

    } catch (Exception e) {
        Log.error("Liquibase 迁移异常: " + ExceptionUtil.generateStackTrace(e));
    }
}

private OpenTelemetryDataSource dataSource() {
    // TODO 返回适当的数据源
    final DataSource dataSource = null;
    return new OpenTelemetryDataSource(dataSource);
}

英文:

This should help you. You have to prepare your DataSource and then use also the Connection from it.


public void runMigration() {
    Log.info("Migrating DB " + datasourceUrl);

    try (OpenTelemetryDataSource dataSource = dataSource();
            Connection connection = dataSource.getConnection();
            ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(Thread.currentThread()
                    .getContextClassLoader());
            DatabaseConnection conn = DatabaseFactory.getInstance()
                    .findCorrectDatabaseImplementation(new JdbcConnection(connection))
                    .getConnection();
            Liquibase liquibase = new Liquibase(changeLogLocation, resourceAccessor, conn)) {

        liquibase.update(new Contexts(), new LabelExpression());

    } catch (Exception e) {
        Log.error("Liquibase Migration Exception: " + ExceptionUtil.generateStackTrace(e));
    }
}

private OpenTelemetryDataSource dataSource() {
    // TODO return proper datasource
    final DataSource dataSource = null;
    return new OpenTelemetryDataSource(dataSource);
}

huangapple
  • 本文由 发表于 2023年3月1日 15:02:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定