github.com/m3db/m3@v1.5.0/src/x/debug/ext/namespace.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package extdebug 22 23 import ( 24 "bytes" 25 "encoding/json" 26 "fmt" 27 "io" 28 "net/http" 29 30 clusterclient "github.com/m3db/m3/src/cluster/client" 31 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 32 "github.com/m3db/m3/src/query/api/v1/handler/namespace" 33 "github.com/m3db/m3/src/query/generated/proto/admin" 34 "github.com/m3db/m3/src/x/debug" 35 "github.com/m3db/m3/src/x/instrument" 36 xhttp "github.com/m3db/m3/src/x/net/http" 37 38 "github.com/gogo/protobuf/jsonpb" 39 ) 40 41 type namespaceInfoSource struct { 42 handler *namespace.GetHandler 43 defaults handleroptions.ServiceNameAndDefaults 44 } 45 46 // NewNamespaceInfoSource returns a Source for namespace information. 47 func NewNamespaceInfoSource( 48 clusterClient clusterclient.Client, 49 allDefaults []handleroptions.ServiceNameAndDefaults, 50 instrumentOpts instrument.Options, 51 ) (debug.Source, error) { 52 var ( 53 m3dbDefault handleroptions.ServiceNameAndDefaults 54 found bool 55 ) 56 for _, def := range allDefaults { 57 if def.ServiceName == handleroptions.M3DBServiceName { 58 m3dbDefault = def 59 found = true 60 break 61 } 62 } 63 64 if !found { 65 return nil, fmt.Errorf("could not find M3DB service in defaults: %v", allDefaults) 66 } 67 68 handler := namespace.NewGetHandler(clusterClient, 69 instrumentOpts) 70 71 return &namespaceInfoSource{ 72 handler: handler, 73 defaults: m3dbDefault, 74 }, nil 75 } 76 77 // Write fetches data about the namespace and writes it in the given writer. 78 // The data is formatted in json. 79 func (n *namespaceInfoSource) Write(w io.Writer, r *http.Request) error { 80 opts := handleroptions.NewServiceOptions(n.defaults, r.Header, nil) 81 nsRegistry, err := n.handler.Get(opts) 82 if err != nil { 83 return err 84 } 85 86 resp := &admin.NamespaceGetResponse{ 87 Registry: &nsRegistry, 88 } 89 90 marshaler := jsonpb.Marshaler{EmitDefaults: true} 91 buf := new(bytes.Buffer) 92 if err := marshaler.Marshal(buf, resp); err != nil { 93 return err 94 } 95 96 toDuration, err := xhttp.NanosToDurationBytes(buf) 97 if err != nil { 98 return err 99 } 100 101 jsonData, err := json.Marshal(toDuration) 102 if err != nil { 103 return err 104 } 105 106 _, err = w.Write(jsonData) 107 if err != nil { 108 return err 109 } 110 111 return nil 112 }