Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add support goframe v2 #203

Merged
merged 22 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use (
./plugins/pulsar
./plugins/segmentio-kafka
./plugins/go-elasticsearchv8
./plugins/goframe

./test/benchmark-codebase/consumer
./test/benchmark-codebase/provider
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,8 @@ github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a h1:AhmOdSHeswKHBjh
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand Down
11 changes: 11 additions & 0 deletions plugins/goframe/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/apache/skywalking-go/plugins/goframe

go 1.19

require github.com/apache/skywalking-go/plugins/core v0.0.0-20240910044646-4f94a255c2d3
huicunjun marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/dave/dst v0.27.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/sys v0.20.0 // indirect
)
13 changes: 13 additions & 0 deletions plugins/goframe/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/apache/skywalking-go/plugins/core v0.0.0-20240910044646-4f94a255c2d3 h1:eU6k2oVqIeHt0qoExrP5OsvNF3+29dXBR9DFVJS56hM=
github.com/apache/skywalking-go/plugins/core v0.0.0-20240910044646-4f94a255c2d3/go.mod h1:5a8UbX22PEo+7u7aPvGyNVDPkkJHM//nZwrnk7TDXIM=
github.com/dave/dst v0.27.2 h1:4Y5VFTkhGLC1oddtNwuxxe36pnyLxMFXT51FOzH8Ekc=
github.com/dave/dst v0.27.2/go.mod h1:jHh6EOibnHgcUW3WjKHisiooEkYwqpHLBSX1iOBhEyc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
46 changes: 46 additions & 0 deletions plugins/goframe/instrument.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package goframe
huicunjun marked this conversation as resolved.
Show resolved Hide resolved

import (
"embed"
"github.com/apache/skywalking-go/plugins/core/instrument"
mrproliu marked this conversation as resolved.
Show resolved Hide resolved
"strings"
)

//go:embed *
var fs embed.FS

//skywalking:nocopy
type Instrument struct {
}

func NewInstrument() *Instrument {
return &Instrument{}
}

func (i *Instrument) Name() string {
return "goframe"
}

func (i *Instrument) BasePackage() string {
return "github.com/gogf/gf/v2"
}

func (i *Instrument) VersionChecker(version string) bool {
return strings.HasPrefix(version, "v2.")
}

func (i *Instrument) Points() []*instrument.Point {
return []*instrument.Point{
{
PackagePath: "net/ghttp",
At: instrument.NewMethodEnhance("*Server", "ServeHTTP",
instrument.WithArgsCount(2), instrument.WithArgType(0, "http.ResponseWriter"),
instrument.WithArgType(1, "*http.Request")),
Interceptor: "GoFrameServerInterceptor",
},
}
}

func (i *Instrument) FS() *embed.FS {
return &fs
}
68 changes: 68 additions & 0 deletions plugins/goframe/net/ghttp/intercepter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 ghttp

import (
"fmt"
"github.com/apache/skywalking-go/plugins/core/operator"
mrproliu marked this conversation as resolved.
Show resolved Hide resolved
"github.com/apache/skywalking-go/plugins/core/tracing"
"net/http"
)

// GoFrameServerInterceptor is used to intercept and trace HTTP requests.
type GoFrameServerInterceptor struct{}

// BeforeInvoke intercepts the HTTP request before invoking the handler.
func (h *GoFrameServerInterceptor) BeforeInvoke(invocation operator.Invocation) error {
request := invocation.Args()[1].(*http.Request)
s, err := tracing.CreateEntrySpan(fmt.Sprintf("%s:%s", request.Method, request.URL.Path), func(headerKey string) (string, error) {
return request.Header.Get(headerKey), nil
}, tracing.WithLayer(tracing.SpanLayerHTTP),
tracing.WithTag(tracing.TagHTTPMethod, request.Method),
tracing.WithTag(tracing.TagURL, request.Host+request.URL.Path),
tracing.WithComponent(5004))
mrproliu marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

s.Tag(tracing.TagHTTPParams, request.URL.RawQuery)
mrproliu marked this conversation as resolved.
Show resolved Hide resolved

writer := invocation.Args()[0].(http.ResponseWriter)
invocation.ChangeArg(0, &writerWrapper{ResponseWriter: writer, statusCode: http.StatusOK})
invocation.SetContext(s)
return nil
}

// AfterInvoke processes after the HTTP request has been handled.
func (h *GoFrameServerInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error {
fmt.Printf("goframe 拦截请求前 AfterInvoke\n")
huicunjun marked this conversation as resolved.
Show resolved Hide resolved
if invocation.GetContext() == nil {
return nil
}
span := invocation.GetContext().(tracing.Span)
if wrapped, ok := invocation.Args()[0].(*writerWrapper); ok {
span.Tag(tracing.TagStatusCode, fmt.Sprintf("%d", wrapped.statusCode))
}
span.End()
return nil
}

type writerWrapper struct {
http.ResponseWriter
statusCode int
}
4 changes: 4 additions & 0 deletions tools/go-agent/instrument/plugins/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
goelasticsearchv8 "github.com/apache/skywalking-go/plugins/go-elasticsearchv8"
goredisv9 "github.com/apache/skywalking-go/plugins/go-redisv9"
"github.com/apache/skywalking-go/plugins/go-restfulv3"
"github.com/apache/skywalking-go/plugins/goframe"
gorm_entry "github.com/apache/skywalking-go/plugins/gorm/entry"
gorm_mysql "github.com/apache/skywalking-go/plugins/gorm/mysql"
"github.com/apache/skywalking-go/plugins/grpc"
Expand Down Expand Up @@ -85,6 +86,9 @@ func init() {

// echov4 related instruments
registerFramework(echov4.NewInstrument())

//goframe
registerFramework(goframe.NewInstrument())
}

func registerFramework(ins instrument.Instrument) {
Expand Down
Loading