This is an automated email from the ASF dual-hosted git repository. liuhan pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/skywalking-go.git
The following commit(s) were added to refs/heads/main by this push: new 9265c5c fix lost trace when multi middleware handlerFunc (#167) 9265c5c is described below commit 9265c5c05b5a0bbdb6d612608316bd8853ff81ae Author: Ruff <38658935+ruff-n...@users.noreply.github.com> AuthorDate: Tue Feb 6 18:43:01 2024 +0800 fix lost trace when multi middleware handlerFunc (#167) --- CHANGES.md | 1 + plugins/gin/intercepter.go | 11 +++++++++++ plugins/gin/structures.go | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 88dc93a..9cf0395 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ Release Notes. * Fix enhance method error when unknown parameter type. * Fix wrong tracing context when trace have been sampled. * Fix enhance param error when there are multiple params. +* Fix lost trace when multi middleware `handlerFunc` in `gin` plugin. #### Issues and PR - All issues are [here](https://github.com/apache/skywalking/milestone/197?closed=1) diff --git a/plugins/gin/intercepter.go b/plugins/gin/intercepter.go index 653f305..af07621 100644 --- a/plugins/gin/intercepter.go +++ b/plugins/gin/intercepter.go @@ -30,6 +30,9 @@ type ContextInterceptor struct { } func (h *ContextInterceptor) BeforeInvoke(invocation operator.Invocation) error { + if !isFirstHandle(invocation.CallerInstance()) { + return nil + } context := invocation.CallerInstance().(*gin.Context) s, err := tracing.CreateEntrySpan( fmt.Sprintf("%s:%s", context.Request.Method, context.FullPath()), func(headerKey string) (string, error) { @@ -59,3 +62,11 @@ func (h *ContextInterceptor) AfterInvoke(invocation operator.Invocation, result span.End() return nil } + +func isFirstHandle(c interface{}) bool { + // index of HandlersChain, incremented in #Next(), -1 indicates that no handler has been executed + if context, ok := c.(*nativeContext); ok { + return context.index < 0 + } + return true +} diff --git a/plugins/gin/structures.go b/plugins/gin/structures.go new file mode 100644 index 0000000..1477725 --- /dev/null +++ b/plugins/gin/structures.go @@ -0,0 +1,23 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package gin + +//skywalking:native github.com/gin-gonic/gin Context +type nativeContext struct { + index int8 +}