1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-23 22:34:47 +02:00

Move core and key to kv package (#720)

* Move core to kv

* Move kv.Value to kv.value.Value

* Move key to kv
This commit is contained in:
Chen Yixiao
2020-05-14 07:06:03 +08:00
committed by GitHub
parent 88100f0fc7
commit 1301b6f3e4
104 changed files with 1823 additions and 1803 deletions

View File

@@ -19,9 +19,9 @@ import (
"google.golang.org/grpc/metadata"
"go.opentelemetry.io/otel/api/core"
"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/trace"
)
@@ -54,14 +54,14 @@ 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) ([]core.KeyValue, trace.SpanContext) {
func Extract(ctx context.Context, metadata *metadata.MD) ([]kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), &metadataSupplier{
metadata: metadata,
})
spanContext := trace.RemoteSpanContextFromContext(ctx)
var correlationCtxKVs []core.KeyValue
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
var correlationCtxKVs []kv.KeyValue
correlation.MapFromContext(ctx).Foreach(func(kv kv.KeyValue) bool {
correlationCtxKVs = append(correlationCtxKVs, kv)
return true
})

View File

@@ -28,20 +28,19 @@ import (
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/trace"
)
var (
rpcServiceKey = key.New("rpc.service")
netPeerIPKey = key.New("net.peer.ip")
netPeerPortKey = key.New("net.peer.port")
rpcServiceKey = kv.NewKey("rpc.service")
netPeerIPKey = kv.NewKey("net.peer.ip")
netPeerPortKey = kv.NewKey("net.peer.port")
messageTypeKey = key.New("message.type")
messageIDKey = key.New("message.id")
messageUncompressedSizeKey = key.New("message.uncompressed_size")
messageTypeKey = kv.NewKey("message.type")
messageIDKey = kv.NewKey("message.id")
messageUncompressedSizeKey = kv.NewKey("message.uncompressed_size")
)
const (
@@ -399,28 +398,28 @@ func StreamServerInterceptor(tracer trace.Tracer) grpc.StreamServerInterceptor {
}
}
func peerInfoFromTarget(target string) []core.KeyValue {
func peerInfoFromTarget(target string) []kv.KeyValue {
host, port, err := net.SplitHostPort(target)
if err != nil {
return []core.KeyValue{}
return []kv.KeyValue{}
}
if host == "" {
host = "127.0.0.1"
}
return []core.KeyValue{
return []kv.KeyValue{
netPeerIPKey.String(host),
netPeerPortKey.String(port),
}
}
func peerInfoFromContext(ctx context.Context) []core.KeyValue {
func peerInfoFromContext(ctx context.Context) []kv.KeyValue {
p, ok := peer.FromContext(ctx)
if !ok {
return []core.KeyValue{}
return []kv.KeyValue{}
}
return peerInfoFromTarget(p.Addr.String())

View File

@@ -24,17 +24,16 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/trace"
)
var (
HTTPStatus = key.New("http.status")
HTTPHeaderMIME = key.New("http.mime")
HTTPRemoteAddr = key.New("http.remote")
HTTPLocalAddr = key.New("http.local")
HTTPStatus = kv.NewKey("http.status")
HTTPHeaderMIME = kv.NewKey("http.mime")
HTTPRemoteAddr = kv.NewKey("http.remote")
HTTPLocalAddr = kv.NewKey("http.local")
)
var (
@@ -90,7 +89,7 @@ func NewClientTrace(ctx context.Context) *httptrace.ClientTrace {
}
}
func (ct *clientTracer) start(hook, spanName string, attrs ...core.KeyValue) {
func (ct *clientTracer) start(hook, spanName string, attrs ...kv.KeyValue) {
ct.mtx.Lock()
defer ct.mtx.Unlock()
@@ -110,7 +109,7 @@ func (ct *clientTracer) start(hook, spanName string, attrs ...core.KeyValue) {
}
}
func (ct *clientTracer) end(hook string, err error, attrs ...core.KeyValue) {
func (ct *clientTracer) end(hook string, err error, attrs ...kv.KeyValue) {
ct.mtx.Lock()
defer ct.mtx.Unlock()
if ctx, ok := ct.activeHooks[hook]; ok {
@@ -198,7 +197,7 @@ func (ct *clientTracer) wroteHeaderField(k string, v []string) {
if ct.span("http.headers") == nil {
ct.start("http.headers", "http.headers")
}
ct.root.SetAttributes(key.String("http."+strings.ToLower(k), sliceToString(v)))
ct.root.SetAttributes(kv.String("http."+strings.ToLower(k), sliceToString(v)))
}
func (ct *clientTracer) wroteHeaders() {

View File

@@ -23,9 +23,8 @@ import (
"github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/plugin/httptrace"
export "go.opentelemetry.io/otel/sdk/export/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
@@ -102,19 +101,19 @@ func TestHTTPRequestWithClientTrace(t *testing.T) {
testLen := []struct {
name string
attributes []core.KeyValue
attributes []kv.KeyValue
parent string
}{
{
name: "http.connect",
attributes: []core.KeyValue{key.String("http.remote", address.String())},
attributes: []kv.KeyValue{kv.String("http.remote", address.String())},
parent: "http.getconn",
},
{
name: "http.getconn",
attributes: []core.KeyValue{
key.String("http.remote", address.String()),
key.String("http.host", address.String()),
attributes: []kv.KeyValue{
kv.String("http.remote", address.String()),
kv.String("http.host", address.String()),
},
parent: "test",
},
@@ -141,18 +140,18 @@ func TestHTTPRequestWithClientTrace(t *testing.T) {
}
}
actualAttrs := make(map[core.Key]string)
actualAttrs := make(map[kv.Key]string)
for _, attr := range span.Attributes {
actualAttrs[attr.Key] = attr.Value.Emit()
}
expectedAttrs := make(map[core.Key]string)
expectedAttrs := make(map[kv.Key]string)
for _, attr := range tl.attributes {
expectedAttrs[attr.Key] = attr.Value.Emit()
}
if tl.name == "http.getconn" {
local := key.New("http.local")
local := kv.NewKey("http.local")
// http.local attribute is not deterministic, just make sure it exists for `getconn`.
if _, ok := actualAttrs[local]; ok {
delete(actualAttrs, local)

View File

@@ -18,30 +18,29 @@ import (
"context"
"net/http"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/correlation"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/trace"
)
var (
HostKey = key.New("http.host")
URLKey = key.New("http.url")
HostKey = kv.NewKey("http.host")
URLKey = kv.NewKey("http.url")
)
// Returns the Attributes, Context Entries, and SpanContext that were encoded by Inject.
func Extract(ctx context.Context, req *http.Request) ([]core.KeyValue, []core.KeyValue, trace.SpanContext) {
func Extract(ctx context.Context, req *http.Request) ([]kv.KeyValue, []kv.KeyValue, trace.SpanContext) {
ctx = propagation.ExtractHTTP(ctx, global.Propagators(), req.Header)
attrs := []core.KeyValue{
attrs := []kv.KeyValue{
URLKey.String(req.URL.String()),
// Etc.
}
var correlationCtxKVs []core.KeyValue
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
var correlationCtxKVs []kv.KeyValue
correlation.MapFromContext(ctx).Foreach(func(kv kv.KeyValue) bool {
correlationCtxKVs = append(correlationCtxKVs, kv)
return true
})

View File

@@ -17,24 +17,25 @@ package othttp
import (
"net/http"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/trace"
)
// Attribute keys that can be added to a span.
const (
HostKey = core.Key("http.host") // the HTTP host (http.Request.Host)
MethodKey = core.Key("http.method") // the HTTP method (http.Request.Method)
PathKey = core.Key("http.path") // the HTTP path (http.Request.URL.Path)
URLKey = core.Key("http.url") // the HTTP URL (http.Request.URL.String())
UserAgentKey = core.Key("http.user_agent") // the HTTP user agent (http.Request.UserAgent())
RouteKey = core.Key("http.route") // the HTTP route (ex: /users/:id)
RemoteAddrKey = core.Key("http.remote_addr") // the network address of the client that sent the HTTP request (http.Request.RemoteAddr)
StatusCodeKey = core.Key("http.status_code") // if set, the HTTP status
ReadBytesKey = core.Key("http.read_bytes") // if anything was read from the request body, the total number of bytes read
ReadErrorKey = core.Key("http.read_error") // If an error occurred while reading a request, the string of the error (io.EOF is not recorded)
WroteBytesKey = core.Key("http.wrote_bytes") // if anything was written to the response writer, the total number of bytes written
WriteErrorKey = core.Key("http.write_error") // if an error occurred while writing a reply, the string of the error (io.EOF is not recorded)
HostKey = kv.Key("http.host") // the HTTP host (http.Request.Host)
MethodKey = kv.Key("http.method") // the HTTP method (http.Request.Method)
PathKey = kv.Key("http.path") // the HTTP path (http.Request.URL.Path)
URLKey = kv.Key("http.url") // the HTTP URL (http.Request.URL.String())
UserAgentKey = kv.Key("http.user_agent") // the HTTP user agent (http.Request.UserAgent())
RouteKey = kv.Key("http.route") // the HTTP route (ex: /users/:id)
RemoteAddrKey = kv.Key("http.remote_addr") // the network address of the client that sent the HTTP request (http.Request.RemoteAddr)
StatusCodeKey = kv.Key("http.status_code") // if set, the HTTP status
ReadBytesKey = kv.Key("http.read_bytes") // if anything was read from the request body, the total number of bytes read
ReadErrorKey = kv.Key("http.read_error") // If an error occurred while reading a request, the string of the error (io.EOF is not recorded)
WroteBytesKey = kv.Key("http.wrote_bytes") // if anything was written to the response writer, the total number of bytes written
WriteErrorKey = kv.Key("http.write_error") // if an error occurred while writing a reply, the string of the error (io.EOF is not recorded)
)
// Filter is a predicate used to determine whether a given http.request should

View File

@@ -18,8 +18,8 @@ import (
"io"
"net/http"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/propagation"
"go.opentelemetry.io/otel/api/trace"
)
@@ -29,7 +29,7 @@ var _ http.Handler = &Handler{}
// Handler is http middleware that corresponds to the http.Handler interface and
// is designed to wrap a http.Mux (or equivalent), while individual routes on
// the mux are wrapped with WithRouteTag. A Handler will add various attributes
// to the span using the core.Keys defined in this package.
// to the span using the kv.Keys defined in this package.
type Handler struct {
operation string
handler http.Handler
@@ -121,7 +121,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func setAfterServeAttributes(span trace.Span, read, wrote, statusCode int64, rerr, werr error) {
kv := make([]core.KeyValue, 0, 5)
kv := make([]kv.KeyValue, 0, 5)
// TODO: Consider adding an event after each read and write, possibly as an
// option (defaulting to off), so as to not create needlessly verbose spans.
if read > 0 {

View File

@@ -22,8 +22,9 @@ import (
"net/http"
"strings"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/exporters/trace/stdout"
"go.opentelemetry.io/otel/plugin/othttp"
@@ -65,7 +66,7 @@ func ExampleNewHandler() {
case "":
err = fmt.Errorf("expected /hello/:name in %q", s)
default:
trace.SpanFromContext(ctx).SetAttributes(key.String("name", pp[1]))
trace.SpanFromContext(ctx).SetAttributes(kv.String("name", pp[1]))
}
return pp[1], err
}