github.com/erda-project/erda-infra@v1.0.9/providers/expvar/provider.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package expvar 16 17 import ( 18 "expvar" 19 "fmt" 20 "net/http" 21 "os" 22 23 "github.com/erda-project/erda-infra/base/servicehub" 24 "github.com/erda-project/erda-infra/providers/httpserver" 25 ) 26 27 type config struct { 28 Publish []string `file:"publish"` 29 } 30 31 // +provider 32 type provider struct { 33 Cfg *config 34 Router httpserver.Router `autowired:"http-server@admin"` 35 } 36 37 // Run this is optional 38 func (p *provider) Init(ctx servicehub.Context) error { 39 for _, item := range p.Cfg.Publish { 40 v, ok := defaultVars[item] 41 if !ok { 42 return fmt.Errorf("var %q not exit", item) 43 } 44 expvar.Publish(item, v) 45 } 46 p.Router.Add(http.MethodGet, "/debug/vars", expvar.Handler()) 47 return nil 48 } 49 50 var defaultVars = map[string]expvar.Var{ 51 "envs": expvar.Func(func() interface{} { 52 return os.Environ() 53 }), 54 } 55 56 func init() { 57 servicehub.Register("expvar", &servicehub.Spec{ 58 Services: []string{"expvar"}, 59 Description: "expvar", 60 ConfigFunc: func() interface{} { return &config{} }, 61 Creator: func() servicehub.Provider { 62 return &provider{} 63 }, 64 }) 65 }