-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
487 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ jobs: | |
- plugin_exclusion | ||
- runtime_metrics | ||
- grpc | ||
- mux | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/apache/skywalking-go/plugins/mux | ||
|
||
go 1.18 | ||
|
||
require github.com/gorilla/mux v1.8.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= | ||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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 mux | ||
|
||
import ( | ||
"embed" | ||
|
||
"github.com/apache/skywalking-go/plugins/core/instrument" | ||
) | ||
|
||
//go:embed * | ||
var fs embed.FS | ||
|
||
//skywalking:nocopy | ||
type Instrument struct { | ||
} | ||
|
||
func NewInstrument() *Instrument { | ||
return &Instrument{} | ||
} | ||
|
||
func (i *Instrument) Name() string { | ||
return "mux" | ||
} | ||
|
||
func (i *Instrument) BasePackage() string { | ||
return "github.com/gorilla/mux" | ||
} | ||
|
||
func (i *Instrument) VersionChecker(version string) bool { | ||
return true | ||
} | ||
|
||
func (i *Instrument) Points() []*instrument.Point { | ||
return []*instrument.Point{ | ||
{ | ||
PackagePath: "", | ||
At: instrument.NewMethodEnhance("*Router", "ServeHTTP", | ||
instrument.WithArgsCount(2), | ||
instrument.WithArgType(0, "http.ResponseWriter"), instrument.WithArgType(1, "*http.Request"), | ||
instrument.WithResultCount(0)), | ||
Interceptor: "ServeHTTPInterceptor", | ||
}, | ||
{ | ||
PackagePath: "", | ||
At: instrument.NewMethodEnhance("*Router", "Match", | ||
instrument.WithArgsCount(2), | ||
instrument.WithArgType(0, "*http.Request"), instrument.WithArgType(1, "*RouteMatch"), | ||
instrument.WithResultCount(1), instrument.WithResultType(0, "bool")), | ||
Interceptor: "MatchInterceptor", | ||
}, | ||
} | ||
} | ||
|
||
func (i *Instrument) FS() *embed.FS { | ||
return &fs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 mux | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/apache/skywalking-go/plugins/core/operator" | ||
"github.com/apache/skywalking-go/plugins/core/tracing" | ||
) | ||
|
||
type MatchInterceptor struct { | ||
} | ||
|
||
func (n *MatchInterceptor) BeforeInvoke(invocation operator.Invocation) error { | ||
return nil | ||
} | ||
|
||
func (n *MatchInterceptor) AfterInvoke(invocation operator.Invocation, results ...interface{}) error { | ||
// only process with matched route | ||
if matched, ok := results[0].(bool); !ok || !matched { | ||
return nil | ||
} | ||
match, ok := invocation.Args()[1].(*RouteMatch) | ||
req := invocation.Args()[0].(*http.Request) | ||
if !ok || match == nil || match.Route == nil || req == nil { | ||
return nil | ||
} | ||
|
||
span := tracing.ActiveSpan() | ||
if span == nil { | ||
return nil | ||
} | ||
|
||
// find matched template | ||
var routePrefix, routePath string | ||
for _, matcher := range match.Route.matchers { | ||
if regexp, ok := matcher.(*routeRegexp); ok && regexp != nil { | ||
if regexp.regexpType == 2 { | ||
routePrefix = regexp.template | ||
} else if regexp.regexpType == 0 { | ||
routePath = regexp.template | ||
} | ||
} | ||
} | ||
|
||
opName := routePrefix | ||
if routePath != "" { | ||
opName = routePath | ||
} | ||
|
||
// re-set the operation name if route path/prefix not empty | ||
if opName != "" { | ||
span.SetOperationName(req.Method + ":" + opName) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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 mux | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/apache/skywalking-go/plugins/core/operator" | ||
"github.com/apache/skywalking-go/plugins/core/tracing" | ||
) | ||
|
||
type ServeHTTPInterceptor struct { | ||
} | ||
|
||
func (n *ServeHTTPInterceptor) BeforeInvoke(invocation operator.Invocation) error { | ||
request := invocation.Args()[1].(*http.Request) | ||
s, err := tracing.CreateEntrySpan(fmt.Sprintf("%s:%s", request.Method, request.RequestURI), func(headerKey string) (string, error) { | ||
return request.Header.Get(headerKey), nil | ||
}, tracing.WithLayer(tracing.SpanLayerHTTP), | ||
tracing.WithComponent(5017), | ||
tracing.WithTag(tracing.TagHTTPMethod, request.Method), | ||
tracing.WithTag(tracing.TagURL, request.Host+request.URL.Path)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
writer := invocation.Args()[0].(http.ResponseWriter) | ||
invocation.ChangeArg(0, &writerWrapper{ResponseWriter: writer, statusCode: http.StatusOK}) | ||
invocation.SetContext(s) | ||
return nil | ||
} | ||
|
||
func (n *ServeHTTPInterceptor) AfterInvoke(invocation operator.Invocation, results ...interface{}) error { | ||
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 | ||
} | ||
|
||
func (w *writerWrapper) WriteHeader(statusCode int) { | ||
// cache the status code | ||
w.statusCode = statusCode | ||
w.ResponseWriter.WriteHeader(statusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 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 mux | ||
|
||
//skywalking:native github.com/gorilla/mux RouteMatch | ||
type RouteMatch struct { | ||
Route *Route | ||
} | ||
|
||
type Route struct { | ||
routeConf | ||
} | ||
|
||
type routeConf struct { | ||
matchers []interface{} | ||
} | ||
|
||
//skywalking:native github.com/gorilla/mux routeRegexp | ||
type routeRegexp struct { | ||
template string | ||
regexpType int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
# | ||
# Licensed to the 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. The 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. | ||
|
||
home="$(cd "$(dirname $0)"; pwd)" | ||
go build ${GO_BUILD_OPTS} -o mux | ||
|
||
./mux |
Oops, something went wrong.