过滤前根据请求设置跟踪ID

时间:2019-05-06 17:50:55

标签: java spring-boot spring-cloud spring-webflux

您好,我有一个WebFilter代码,用于记录来自我的应用程序的请求和响应,但是我想在对应用程序的第一个请求上添加一个跟踪ID,但是它没有出现:

class MyFragment : androidx.fragment.app.Fragment() {
    private lateinit var mRecyclerView: RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_rv, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        val v = view

        mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)

        mRecyclerView.layoutManager = LinearLayoutManager(activity)

        val myList = ArrayList<Item>()

        val myListTitles = ArrayList<String>()
        myListTitles.add("Tab A")
        myListTitles.add("Tab B")
        myListTitles.add("Tab C")

        val myListDescriptions = ArrayList<String>()
        myListDescriptions.add("Description A")
        myListDescriptions.add("Description B")
        myListDescriptions.add("Description C")


        val mAdapter = MyAdapter(myList, childFragmentManager)

        mRecyclerView.adapter = mAdapter

        super.onActivityCreated(savedInstanceState)
    }
}

正如您在我的回复中看到的那样,我有trace-id,但是在请求中却没有。

是否可以使用跟踪ID记录请求?

这是我尝试实现的代码。

Request:
2019-05-06 14:23:23.590 INFO [application,,,] 4151 --- [ctor-http-nio-3] c.m.m.shared.filters.RequestFilter : msg="Request to Application", path=/v1/car/1111", 

Response:
2019-05-06 14:23:28.196 INFO [application,f1dd6a9997339ht6,f1cc6ff0gg7969306,false] 4151 --- [ctor-http-nio-4] c.m.m.shared.filters.RequestFilter : msg="Response from Application", status_code="OK",

我的要求:

@Component
@Order(TraceWebFilter.ORDER + 5)
class RequestFilter implements WebFilter {
    private Tracer tracer;

    private CustomLogger logger = new CustomLogger(RequestFilter.class);

    public RequestFilter(Tracer tracer) {
        this.tracer = tracer;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {

        Span currentSpan = this.tracer.currentSpan();
        if (currentSpan != null) {
            serverWebExchange.getRequest().getHeaders().add("TRACE-ID", currentSpan.context().traceIdString());
        }

        logger.info("Request to Application")
            .add("path", serverWebExchange.getRequest().getPath().pathWithinApplication().value())
            .add("headers", serverWebExchange.getRequest().getHeaders().toString())
            .log();

        serverWebExchange.getResponse().beforeCommit(() -> {
            Span currentSpan2 = this.tracer.currentSpan();
            if (currentSpan2 != null) {
                serverWebExchange.getResponse().getHeaders().add("TRACE-ID", currentSpan2.context().traceIdString());
            }
            logger.info("Response from Application").add("status_code", serverWebExchange.getResponse().getStatusCode()).log();
            return Mono.empty();
        });

        return webFilterChain.filter(serverWebExchange);
    }
}

0 个答案:

没有答案