You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在graphql-spring-boot中为GraphQL执行添加Instrumentation?

解决graphql-spring-boot中添加Instrumentation的问题

我刚帮你梳理了这个问题,其实在graphql-spring-boot中添加Instrumentation有几种更可靠的实现方式,你之前尝试的InstrumentationProvider大概率是因为版本适配问题没被框架识别到,下面给你两种可行方案:

方案一:直接注册Instrumentation类型的Spring Bean

这是当前版本(graphql-spring-boot 12.x及以上)最推荐的方式,框架会自动检测并加载所有Instrumentation类型的Bean,无需额外的Provider接口。

示例代码如下:

import graphql.ExecutionResult;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.SimpleInstrumentationContext;
import graphql.execution.instrumentation.parameters.InstrumentationExecuteQueryParameters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GraphQLInstrumentationConfig {

    @Bean
    public Instrumentation customGraphQLInstrumentation() {
        return new SimpleInstrumentation() {
            // 重写你需要监控的方法,比如查询执行前后的逻辑
            @Override
            public SimpleInstrumentationContext<ExecutionResult> beginExecuteQuery(InstrumentationExecuteQueryParameters parameters) {
                long startTime = System.currentTimeMillis();
                return new SimpleInstrumentationContext<>() {
                    @Override
                    public void onCompleted(ExecutionResult result, Throwable throwable) {
                        long duration = System.currentTimeMillis() - startTime;
                        // 这里可以替换成你的业务逻辑,比如记录监控指标、打印日志等
                        System.out.printf("GraphQL查询执行耗时:%dms,查询语句:%s%n", duration, parameters.getQuery());
                    }
                };
            }
        };
    }
}

方案二:组合多个Instrumentation(如果需要)

如果你的项目需要同时使用多个Instrumentation,可以用CompositeInstrumentation来组合它们,同样通过Spring Bean注册:

import graphql.execution.instrumentation.CompositeInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.SimpleInstrumentation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class GraphQLInstrumentationConfig {

    @Bean
    public Instrumentation compositeInstrumentation() {
        return new CompositeInstrumentation(
            Arrays.asList(
                queryTimingInstrumentation(),
                errorLoggingInstrumentation()
            )
        );
    }

    private Instrumentation queryTimingInstrumentation() {
        // 实现计时逻辑的Instrumentation
        return new SimpleInstrumentation() {
            // ... 自定义逻辑
        };
    }

    private Instrumentation errorLoggingInstrumentation() {
        // 实现错误日志的Instrumentation
        return new SimpleInstrumentation() {
            // ... 自定义逻辑
        };
    }
}

为什么你的InstrumentationProvider没被调用?

大概率是因为版本兼容性问题:较新的graphql-spring-boot版本已经不再使用InstrumentationProvider接口作为加载入口,转而直接识别Instrumentation类型的Bean。另外也有可能你的@Service类所在的包没有被Spring的组件扫描覆盖到,但即使解决扫描问题,这种方式也不是当前版本的推荐用法了。

验证方式很简单:启动项目后执行任意GraphQL查询,查看你在Instrumentation中添加的日志或监控逻辑是否触发即可。

内容的提问来源于stack exchange,提问作者Michael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:27:36