1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-09-16 09:26:25 +02:00

Merge branch 'master' into oterror

This commit is contained in:
Tyler Yahn
2020-06-02 08:08:08 -07:00
committed by GitHub
3 changed files with 235 additions and 8 deletions

View File

@@ -26,6 +26,29 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
// Option is a function that allows configuration of the grpctrace Extract()
// and Inject() functions
type Option func(*config)
type config struct {
propagators propagation.Propagators
}
func newConfig(opts []Option) *config {
c := &config{propagators: global.Propagators()}
for _, o := range opts {
o(c)
}
return c
}
// WithPropagators sets the propagators to use for Extraction and Injection
func WithPropagators(props propagation.Propagators) Option {
return func(c *config) {
c.propagators = props
}
}
type metadataSupplier struct {
metadata *metadata.MD
}
@@ -45,8 +68,9 @@ func (s *metadataSupplier) Set(key string, value string) {
// Inject injects correlation context and span context into the gRPC
// metadata object. This function is meant to be used on outgoing
// requests.
func Inject(ctx context.Context, metadata *metadata.MD) {
propagation.InjectHTTP(ctx, global.Propagators(), &metadataSupplier{
func Inject(ctx context.Context, metadata *metadata.MD, opts ...Option) {
c := newConfig(opts)
propagation.InjectHTTP(ctx, c.propagators, &metadataSupplier{
metadata: metadata,
})
}
@@ -54,8 +78,9 @@ func Inject(ctx context.Context, metadata *metadata.MD) {
// Extract returns the correlation context and span context that
// another service encoded in the gRPC metadata object with Inject.
// This function is meant to be used on incoming requests.
func Extract(ctx context.Context, metadata *metadata.MD) ([]kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), &metadataSupplier{
func Extract(ctx context.Context, metadata *metadata.MD, opts ...Option) ([]kv.KeyValue, trace.SpanContext) {
c := newConfig(opts)
ctx = propagation.ExtractHTTP(ctx, c.propagators, &metadataSupplier{
metadata: metadata,
})

View File

@@ -26,9 +26,33 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
// Option is a function that allows configuration of the httptrace Extract()
// and Inject() functions
type Option func(*config)
type config struct {
propagators propagation.Propagators
}
func newConfig(opts []Option) *config {
c := &config{propagators: global.Propagators()}
for _, o := range opts {
o(c)
}
return c
}
// WithPropagators sets the propagators to use for Extraction and Injection
func WithPropagators(props propagation.Propagators) Option {
return func(c *config) {
c.propagators = props
}
}
// Returns the Attributes, Context Entries, and SpanContext that were encoded by Inject.
func Extract(ctx context.Context, req *http.Request) ([]kv.KeyValue, []kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), req.Header)
func Extract(ctx context.Context, req *http.Request, opts ...Option) ([]kv.KeyValue, []kv.KeyValue, trace.SpanContext) {
c := newConfig(opts)
ctx = propagation.ExtractHTTP(ctx, c.propagators, req.Header)
attrs := append(
standard.HTTPServerAttributesFromHTTPRequest("", "", req),
@@ -44,6 +68,7 @@ func Extract(ctx context.Context, req *http.Request) ([]kv.KeyValue, []kv.KeyVal
return attrs, correlationCtxKVs, trace.RemoteSpanContextFromContext(ctx)
}
func Inject(ctx context.Context, req *http.Request) {
propagation.InjectHTTP(ctx, global.Propagators(), req.Header)
func Inject(ctx context.Context, req *http.Request, opts ...Option) {
c := newConfig(opts)
propagation.InjectHTTP(ctx, c.propagators, req.Header)
}

View File

@@ -0,0 +1,177 @@
// Copyright The OpenTelemetry Authors
//
// 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 httptrace_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/standard"
"go.opentelemetry.io/otel/instrumentation/httptrace"
export "go.opentelemetry.io/otel/sdk/export/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func TestRoundtrip(t *testing.T) {
exp := &testExporter{
spanMap: make(map[string][]*export.SpanData),
}
tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
global.SetTraceProvider(tp)
tr := tp.Tracer("httptrace/client")
var expectedAttrs map[kv.Key]string
expectedCorrs := map[kv.Key]string{kv.Key("foo"): "bar"}
// Mock http server
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attrs, corrs, span := httptrace.Extract(r.Context(), r)
actualAttrs := make(map[kv.Key]string)
for _, attr := range attrs {
if attr.Key == standard.NetPeerPortKey {
// Peer port will be non-deterministic
continue
}
actualAttrs[attr.Key] = attr.Value.Emit()
}
if diff := cmp.Diff(actualAttrs, expectedAttrs); diff != "" {
t.Fatalf("[TestRoundtrip] Attributes are different: %v", diff)
}
actualCorrs := make(map[kv.Key]string)
for _, corr := range corrs {
actualCorrs[corr.Key] = corr.Value.Emit()
}
if diff := cmp.Diff(actualCorrs, expectedCorrs); diff != "" {
t.Fatalf("[TestRoundtrip] Correlations are different: %v", diff)
}
if !span.IsValid() {
t.Fatalf("[TestRoundtrip] Invalid span extracted: %v", span)
}
_, err := w.Write([]byte("OK"))
if err != nil {
t.Fatal(err)
}
}),
)
defer ts.Close()
address := ts.Listener.Addr()
hp := strings.Split(address.String(), ":")
expectedAttrs = map[kv.Key]string{
standard.HTTPFlavorKey: "1.1",
standard.HTTPHostKey: address.String(),
standard.HTTPMethodKey: "GET",
standard.HTTPSchemeKey: "http",
standard.HTTPTargetKey: "/",
standard.HTTPUserAgentKey: "Go-http-client/1.1",
standard.NetHostIPKey: hp[0],
standard.NetHostPortKey: hp[1],
standard.NetPeerIPKey: "127.0.0.1",
standard.NetTransportKey: "IP.TCP",
}
client := ts.Client()
err := tr.WithSpan(context.Background(), "test",
func(ctx context.Context) error {
ctx = correlation.ContextWithMap(ctx, correlation.NewMap(correlation.MapUpdate{SingleKV: kv.Key("foo").String("bar")}))
req, _ := http.NewRequest("GET", ts.URL, nil)
httptrace.Inject(ctx, req)
res, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %s", err.Error())
}
_ = res.Body.Close()
return nil
})
if err != nil {
panic("unexpected error in http request: " + err.Error())
}
}
func TestSpecifyPropagators(t *testing.T) {
exp := &testExporter{
spanMap: make(map[string][]*export.SpanData),
}
tp, _ := sdktrace.NewProvider(sdktrace.WithSyncer(exp), sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}))
global.SetTraceProvider(tp)
tr := tp.Tracer("httptrace/client")
expectedCorrs := map[kv.Key]string{kv.Key("foo"): "bar"}
// Mock http server
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, corrs, span := httptrace.Extract(r.Context(), r, httptrace.WithPropagators(propagation.New(propagation.WithExtractors(correlation.DefaultHTTPPropagator()))))
actualCorrs := make(map[kv.Key]string)
for _, corr := range corrs {
actualCorrs[corr.Key] = corr.Value.Emit()
}
if diff := cmp.Diff(actualCorrs, expectedCorrs); diff != "" {
t.Fatalf("[TestRoundtrip] Correlations are different: %v", diff)
}
if span.IsValid() {
t.Fatalf("[TestRoundtrip] valid span extracted, expected none: %v", span)
}
_, err := w.Write([]byte("OK"))
if err != nil {
t.Fatal(err)
}
}),
)
defer ts.Close()
client := ts.Client()
err := tr.WithSpan(context.Background(), "test",
func(ctx context.Context) error {
ctx = correlation.ContextWithMap(ctx, correlation.NewMap(correlation.MapUpdate{SingleKV: kv.Key("foo").String("bar")}))
req, _ := http.NewRequest("GET", ts.URL, nil)
httptrace.Inject(ctx, req, httptrace.WithPropagators(propagation.New(propagation.WithInjectors(correlation.DefaultHTTPPropagator()))))
res, err := client.Do(req)
if err != nil {
t.Fatalf("Request failed: %s", err.Error())
}
_ = res.Body.Close()
return nil
})
if err != nil {
panic("unexpected error in http request: " + err.Error())
}
}