1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-15 08:10:39 +00:00

Revert "Merge pull request #4325 from flycash/revert1"

This reverts commit fad897346f, reversing
changes made to e284b0ddae.
This commit is contained in:
Ming Deng
2020-12-12 21:28:58 +08:00
parent fad897346f
commit debd68cbe4
455 changed files with 30003 additions and 4667 deletions

View File

@ -0,0 +1,71 @@
// Copyright 2020 beego
//
// Licensed 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 opentracing
import (
"context"
"net/http"
"github.com/astaxie/beego/client/httplib"
logKit "github.com/go-kit/kit/log"
opentracingKit "github.com/go-kit/kit/tracing/opentracing"
"github.com/opentracing/opentracing-go"
)
type FilterChainBuilder struct {
// CustomSpanFunc users are able to custom their span
CustomSpanFunc func(span opentracing.Span, ctx context.Context,
req *httplib.BeegoHTTPRequest, resp *http.Response, err error)
}
func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filter {
return func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
method := req.GetRequest().Method
operationName := method + "#" + req.GetRequest().URL.String()
span, spanCtx := opentracing.StartSpanFromContext(ctx, operationName)
defer span.Finish()
inject := opentracingKit.ContextToHTTP(opentracing.GlobalTracer(), logKit.NewNopLogger())
inject(spanCtx, req.GetRequest())
resp, err := next(spanCtx, req)
if resp != nil {
span.SetTag("http.status_code", resp.StatusCode)
}
span.SetTag("http.method", method)
span.SetTag("peer.hostname", req.GetRequest().URL.Host)
span.SetTag("http.url", req.GetRequest().URL.String())
span.SetTag("http.scheme", req.GetRequest().URL.Scheme)
span.SetTag("span.kind", "client")
span.SetTag("component", "beego")
if err != nil {
span.SetTag("error", true)
span.SetTag("message", err.Error())
} else if resp != nil && !(resp.StatusCode < 300 && resp.StatusCode >= 200) {
span.SetTag("error", true)
}
span.SetTag("peer.address", req.GetRequest().RemoteAddr)
span.SetTag("http.proto", req.GetRequest().Proto)
if builder.CustomSpanFunc != nil {
builder.CustomSpanFunc(span, ctx, req, resp, err)
}
return resp, err
}
}

View File

@ -0,0 +1,42 @@
// Copyright 2020 beego
//
// Licensed 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 opentracing
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/astaxie/beego/client/httplib"
)
func TestFilterChainBuilder_FilterChain(t *testing.T) {
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
time.Sleep(100 * time.Millisecond)
return &http.Response{
StatusCode: 404,
}, errors.New("hello")
}
builder := &FilterChainBuilder{}
filter := builder.FilterChain(next)
req := httplib.Get("https://github.com/notifications?query=repo%3Aastaxie%2Fbeego")
resp, err := filter(context.Background(), req)
assert.NotNil(t, resp)
assert.NotNil(t, err)
}

View File

@ -0,0 +1,77 @@
// Copyright 2020 beego
//
// Licensed 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 prometheus
import (
"context"
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/astaxie/beego/client/httplib"
)
type FilterChainBuilder struct {
summaryVec prometheus.ObserverVec
AppName string
ServerName string
RunMode string
}
func (builder *FilterChainBuilder) FilterChain(next httplib.Filter) httplib.Filter {
builder.summaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "beego",
Subsystem: "remote_http_request",
ConstLabels: map[string]string{
"server": builder.ServerName,
"env": builder.RunMode,
"appname": builder.AppName,
},
Help: "The statics info for remote http requests",
}, []string{"proto", "scheme", "method", "host", "path", "status", "duration", "isError"})
return func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
startTime := time.Now()
resp, err := next(ctx, req)
endTime := time.Now()
go builder.report(startTime, endTime, ctx, req, resp, err)
return resp, err
}
}
func (builder *FilterChainBuilder) report(startTime time.Time, endTime time.Time,
ctx context.Context, req *httplib.BeegoHTTPRequest, resp *http.Response, err error) {
proto := req.GetRequest().Proto
scheme := req.GetRequest().URL.Scheme
method := req.GetRequest().Method
host := req.GetRequest().URL.Host
path := req.GetRequest().URL.Path
status := -1
if resp != nil {
status = resp.StatusCode
}
dur := int(endTime.Sub(startTime) / time.Millisecond)
builder.summaryVec.WithLabelValues(proto, scheme, method, host, path,
strconv.Itoa(status), strconv.Itoa(dur), strconv.FormatBool(err == nil))
}

View File

@ -0,0 +1,41 @@
// Copyright 2020 beego
//
// Licensed 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 prometheus
import (
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/astaxie/beego/client/httplib"
)
func TestFilterChainBuilder_FilterChain(t *testing.T) {
next := func(ctx context.Context, req *httplib.BeegoHTTPRequest) (*http.Response, error) {
time.Sleep(100 * time.Millisecond)
return &http.Response{
StatusCode: 404,
}, nil
}
builder := &FilterChainBuilder{}
filter := builder.FilterChain(next)
req := httplib.Get("https://github.com/notifications?query=repo%3Aastaxie%2Fbeego")
resp, err := filter(context.Background(), req)
assert.NotNil(t, resp)
assert.Nil(t, err)
}