Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: open-telemetry/opentelemetry-go-contrib
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e5a9fc2b0ac35432a972f82555a32d57785442bb
Choose a base ref
..
head repository: open-telemetry/opentelemetry-go-contrib
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 512911284f5540f32f2f05b0ab50b0e0faab275a
Choose a head ref
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ module go.opentelemetry.io/contrib
go 1.14

require (
github.com/stretchr/testify v1.4.0
go.opentelemetry.io/otel v0.4.2
google.golang.org/grpc v1.28.1
gopkg.in/yaml.v2 v2.2.8 // indirect
27 changes: 21 additions & 6 deletions plugins/gin-gonic/gin/common.go → internal/trace/http.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package gin
package trace

import (
"fmt"
@@ -27,7 +27,11 @@ import (
otelkey "go.opentelemetry.io/otel/api/key"
)

func netAttributesFromHTTPRequest(network string, request *http.Request) []otelcore.KeyValue {
// NetAttributesFromHTTPRequest generates attributes of the net
// namespace as specified by opentelemetry specification for a span.
// The network parameter is a string that net.Dial function from
// standard library can understand.
func NetAttributesFromHTTPRequest(network string, request *http.Request) []otelcore.KeyValue {
transport := ""
switch network {
case "tcp", "tcp4", "tcp6":
@@ -116,14 +120,20 @@ func netAttributesFromHTTPRequest(network string, request *http.Request) []otelc
return attrs
}

func endUserAttributesFromHTTPRequest(request *http.Request) []otelcore.KeyValue {
// EndUserAttributesFromHTTPRequest generates attributes of the
// enduser namespace as specified by opentelemetry specification for a
// span. Currently, only basic authentication is supported.
func EndUserAttributesFromHTTPRequest(request *http.Request) []otelcore.KeyValue {
if username, _, ok := request.BasicAuth(); ok {
return []otelcore.KeyValue{otelkey.String("enduser.id", username)}
}
return nil
}

func httpServerAttributesFromHTTPRequest(serverName, route string, request *http.Request) []otelcore.KeyValue {
// HTTPServerAttributesFromHTTPRequest generates attributes of the http
// namespace as specified by opentelemetry specification for a span on
// the server side. Currently, only basic authentication is supported.
func HTTPServerAttributesFromHTTPRequest(serverName, route string, request *http.Request) []otelcore.KeyValue {
attrs := []otelcore.KeyValue{
otelkey.String("http.method", request.Method),
otelkey.String("http.target", request.RequestURI),
@@ -162,7 +172,10 @@ func httpServerAttributesFromHTTPRequest(serverName, route string, request *http
return attrs
}

func httpAttributesFromHTTPStatusCode(code int) []otelcore.KeyValue {
// HTTPAttributesFromHTTPStatusCode generates attributes of the http
// namespace as specified by opentelemetry specification for a
// span.
func HTTPAttributesFromHTTPStatusCode(code int) []otelcore.KeyValue {
attrs := []otelcore.KeyValue{
otelkey.Int("http.status_code", code),
}
@@ -207,7 +220,9 @@ var validRangesPerCategory = map[int][]codeRange{
},
}

func spanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
// SpanStatusFromHTTPStatusCode generates a status code and a message
// as specified by opentelemetry specification for a span.
func SpanStatusFromHTTPStatusCode(code int) (codes.Code, string) {
spanCode := func() codes.Code {
category := code / 100
ranges, ok := validRangesPerCategory[category]
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package gin
package trace

import (
"crypto/tls"
@@ -398,19 +398,19 @@ func TestNetAttributesFromHTTPRequest(t *testing.T) {
}
for idx, tc := range testcases {
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, noTLS)
got := netAttributesFromHTTPRequest(tc.network, r)
got := NetAttributesFromHTTPRequest(tc.network, r)
assertElementsMatch(t, tc.expected, got, "testcase %d - %s", idx, tc.name)
}
}

func TestEndUserAttributesFromHTTPRequest(t *testing.T) {
r := testRequest("GET", "/user/123", "HTTP/1.1", "", "", nil, http.Header{}, withTLS)
var expected []otelcore.KeyValue
got := endUserAttributesFromHTTPRequest(r)
got := EndUserAttributesFromHTTPRequest(r)
assert.ElementsMatch(t, expected, got)
r.SetBasicAuth("admin", "password")
expected = []otelcore.KeyValue{otelkey.String("enduser.id", "admin")}
got = endUserAttributesFromHTTPRequest(r)
got = EndUserAttributesFromHTTPRequest(r)
assert.ElementsMatch(t, expected, got)
}

@@ -662,7 +662,7 @@ func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) {
}
for idx, tc := range testcases {
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, tc.tls)
got := httpServerAttributesFromHTTPRequest(tc.serverName, tc.route, r)
got := HTTPServerAttributesFromHTTPRequest(tc.serverName, tc.route, r)
assertElementsMatch(t, tc.expected, got, "testcase %d - %s", idx, tc.name)
}
}
@@ -672,20 +672,20 @@ func TestHTTPAttributesFromHTTPStatusCode(t *testing.T) {
otelkey.Int("http.status_code", 404),
otelkey.String("http.status_text", "Not Found"),
}
got := httpAttributesFromHTTPStatusCode(http.StatusNotFound)
got := HTTPAttributesFromHTTPStatusCode(http.StatusNotFound)
assertElementsMatch(t, expected, got, "with valid HTTP status code")
assert.ElementsMatch(t, expected, got)
expected = []otelcore.KeyValue{
otelkey.Int("http.status_code", 499),
}
got = httpAttributesFromHTTPStatusCode(499)
got = HTTPAttributesFromHTTPStatusCode(499)
assertElementsMatch(t, expected, got, "with invalid HTTP status code")
}

func TestSpanStatusFromHTTPStatusCode(t *testing.T) {
for code := 0; code < 1000; code++ {
expected := getExpectedGRPCCodeForHTTPCode(code)
got, _ := spanStatusFromHTTPStatusCode(code)
got, _ := SpanStatusFromHTTPStatusCode(code)
assert.Equalf(t, expected, got, "%s vs %s", expected, got)
}
}
2 changes: 1 addition & 1 deletion plugins/gin-gonic/gin/doc.go
Original file line number Diff line number Diff line change
@@ -19,4 +19,4 @@
// instrumenting the routing of a received message (the Middleware
// function) and instrumenting the response generation through
// template evaluation (the HTML function).
package gin // import "github.com/open-telemetry/opentelemetry-go-contrib/plugins/gin-gonic/gin"
package gin // import "go.opentelemetry.io/contrib/plugins/gin-gonic/gin"
11 changes: 6 additions & 5 deletions plugins/gin-gonic/gin/gintrace.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ import (
"github.com/gin-gonic/gin"
"google.golang.org/grpc/codes"

"go.opentelemetry.io/contrib/internal/trace"
otelglobal "go.opentelemetry.io/otel/api/global"
otelkey "go.opentelemetry.io/otel/api/key"
otelpropagation "go.opentelemetry.io/otel/api/propagation"
@@ -56,9 +57,9 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
}()
ctx := otelpropagation.ExtractHTTP(savedCtx, cfg.Propagators, c.Request.Header)
opts := []oteltrace.StartOption{
oteltrace.WithAttributes(netAttributesFromHTTPRequest("tcp", c.Request)...),
oteltrace.WithAttributes(endUserAttributesFromHTTPRequest(c.Request)...),
oteltrace.WithAttributes(httpServerAttributesFromHTTPRequest(service, c.FullPath(), c.Request)...),
oteltrace.WithAttributes(trace.NetAttributesFromHTTPRequest("tcp", c.Request)...),
oteltrace.WithAttributes(trace.EndUserAttributesFromHTTPRequest(c.Request)...),
oteltrace.WithAttributes(trace.HTTPServerAttributesFromHTTPRequest(service, c.FullPath(), c.Request)...),
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
}
spanName := c.FullPath()
@@ -75,8 +76,8 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
c.Next()

status := c.Writer.Status()
attrs := httpAttributesFromHTTPStatusCode(status)
spanStatus, spanMessage := spanStatusFromHTTPStatusCode(status)
attrs := trace.HTTPAttributesFromHTTPStatusCode(status)
spanStatus, spanMessage := trace.SpanStatusFromHTTPStatusCode(status)
span.SetAttributes(attrs...)
span.SetStatus(spanStatus, spanMessage)
if len(c.Errors) > 0 {