Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to list keys in context.Context?

Tags:

go

So, I have a context.Context(https://golang.org/pkg/context/) variable with me, is there a way I can list all the keys this variable holds?

like image 345
Pensu Avatar asked Feb 28 '19 13:02

Pensu


People also ask

How do you store values in context?

The answer you're seeking for depends on how you want to use the values stored in the context. context. Context is an immutable object, "extending" it with a key-value pair is only possible by making a copy of it and adding the new key-value to the copy (which is done under the hood, by the context package).

What is context value?

Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions. The same Context may be passed to functions running in different goroutines; Contexts are safe for simultaneous use by multiple goroutines.


1 Answers

It is possible to list the internals of context.Context using unsafe reflection and using that information to figure out the keys and/or see if the information you want is in the context.

There are some pitfall such as if the context implementation returns a hardcoded value for a key it wont show up here, and it can be quite unclear on how to actually access the values using keys.

This is nothing I run in production. But in my case I needed to be able to inspect the context.Context to understand better what information it contains.

func printContextInternals(ctx interface{}, inner bool) {
    contextValues := reflect.ValueOf(ctx).Elem()
    contextKeys := reflect.TypeOf(ctx).Elem()

    if !inner {
        fmt.Printf("\nFields for %s.%s\n", contextKeys.PkgPath(), contextKeys.Name())
    }

    if contextKeys.Kind() == reflect.Struct {
        for i := 0; i < contextValues.NumField(); i++ {
            reflectValue := contextValues.Field(i)
            reflectValue = reflect.NewAt(reflectValue.Type(), unsafe.Pointer(reflectValue.UnsafeAddr())).Elem()

            reflectField := contextKeys.Field(i)

            if reflectField.Name == "Context" {
                printContextInternals(reflectValue.Interface(), true)
            } else {
                fmt.Printf("field name: %+v\n", reflectField.Name)
                fmt.Printf("value: %+v\n", reflectValue.Interface())
            }
        }
    } else {
        fmt.Printf("context is empty (int)\n")
    }
}

Examples:

func Ping(w http.ResponseWriter, r *http.Request) {
    printContextInternals(r.Context(), false)
    /* Prints
        Fields for context.valueCtx
        context is empty (int)
        field name: key
        value: net/http context value http-server
        field name: val
        value: &{Addr::20885 Handler:0xc00001c000 TLSConfig:0xc000001c80 ReadTimeout:0s ReadHeaderTimeout:0s WriteTimeout:0s IdleTimeout:0s MaxHeaderBytes:0 TLSNextProto:map[h2:0x12db010] ConnState:<nil> ErrorLog:<nil> BaseContext:<nil> ConnContext:<nil> disableKeepAlives:0 inShutdown:0 nextProtoOnce:{done:1 m:{state:0 sema:0}} nextProtoErr:<nil> mu:{state:0 sema:0} listeners:map[0xc00015a840:{}] activeConn:map[0xc000556fa0:{}] doneChan:<nil> onShutdown:[0x12e9670]}
        field name: key
        value: net/http context value local-addr
        field name: val
        value: [::1]:20885
        field name: mu
        value: {state:0 sema:0}
        field name: done
        value: 0xc00003c2a0
        field name: children
        value: map[context.Background.WithValue(type *http.contextKey, val <not Stringer>).WithValue(type *http.contextKey, val [::1]:20885).WithCancel.WithCancel:{}]
        field name: err
        value: <nil>
        field name: mu
        value: {state:0 sema:0}
        field name: done
        value: <nil>
        field name: children
        value: map[]
        field name: err
        value: <nil>
        field name: key
        value: 0
        field name: val
        value: map[]
        field name: key
        value: 1
        field name: val
        value: &{handler:0x151cf50 buildOnly:false name: err:<nil> namedRoutes:map[] routeConf:{useEncodedPath:false strictSlash:false skipClean:false regexp:{host:<nil> path:0xc0003d78f0 queries:[]} matchers:[0xc0003d78f0 [GET POST]] buildScheme: buildVarsFunc:<nil>}}

    */


    printContextInternals(context.Background(), false)
    /* Prints
        Fields for context.emptyCtx
        context is empty (int)
    */
}
like image 187
Robin Andersson Avatar answered Oct 19 '22 18:10

Robin Andersson