| 1 | //go:build cgo |
| 2 | |
| 3 | package mp |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/matcher" |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/framework" |
| 14 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/modules/websphere/common" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/modules/websphere/mp/contexts" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/ibm.d/protocols/openmetrics" |
| 17 | ) |
| 18 | |
| 19 | func defaultConfig() Config { |
| 20 | return Config{ |
| 21 | Config: framework.Config{ |
| 22 | UpdateEvery: 1, |
| 23 | ObsoletionIterations: 60, |
| 24 | }, |
| 25 | Vnode: "", |
| 26 | CellName: "", |
| 27 | NodeName: "", |
| 28 | ServerName: "", |
| 29 | MetricsEndpoint: "/metrics", |
| 30 | CollectJVMMetrics: confopt.AutoBoolEnabled, |
| 31 | CollectRESTMetrics: confopt.AutoBoolEnabled, |
| 32 | MaxRESTEndpoints: 50, |
| 33 | CollectRESTMatching: "", |
| 34 | HTTPConfig: web.HTTPConfig{ |
| 35 | RequestConfig: web.RequestConfig{ |
| 36 | URL: "https://localhost:9443", |
| 37 | }, |
| 38 | ClientConfig: web.ClientConfig{ |
| 39 | Timeout: confopt.Duration(defaultTimeout), |
| 40 | }, |
| 41 | }, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (c *Collector) Init(ctx context.Context) error { |
| 46 | // Propagate job-level overrides into the embedded framework collector before init |
| 47 | if c.Config.ObsoletionIterations != 0 { |
| 48 | c.Collector.Config.ObsoletionIterations = c.Config.ObsoletionIterations |
| 49 | } |
| 50 | if c.Config.UpdateEvery != 0 { |
| 51 | c.Collector.Config.UpdateEvery = c.Config.UpdateEvery |
| 52 | } |
| 53 | if c.Config.CollectionGroups != nil { |
| 54 | c.Collector.Config.CollectionGroups = c.Config.CollectionGroups |
| 55 | } |
| 56 | |
| 57 | c.RegisterContexts(contexts.GetAllContexts()...) |
| 58 | |
| 59 | if err := c.Collector.Init(ctx); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | c.SetImpl(c) |
| 63 | |
| 64 | cfg := c.Config |
| 65 | baseURL := strings.TrimSpace(cfg.HTTPConfig.RequestConfig.URL) |
| 66 | if baseURL == "" { |
| 67 | return fmt.Errorf("url is required") |
| 68 | } |
| 69 | |
| 70 | if cfg.ClientConfig.Timeout == 0 { |
| 71 | c.Config.ClientConfig.Timeout = confopt.Duration(defaultTimeout) |
| 72 | } |
| 73 | |
| 74 | metricsURL := baseURL |
| 75 | if endpoint := strings.TrimSpace(cfg.MetricsEndpoint); endpoint != "" { |
| 76 | if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") { |
| 77 | metricsURL = endpoint |
| 78 | } else { |
| 79 | baseNormalized := strings.TrimRight(baseURL, "/") |
| 80 | endpointNormalized := strings.TrimLeft(endpoint, "/") |
| 81 | if endpointNormalized == "" { |
| 82 | metricsURL = baseNormalized |
| 83 | } else if strings.HasSuffix(baseNormalized, "/"+endpointNormalized) { |
| 84 | metricsURL = baseNormalized |
| 85 | } else { |
| 86 | metricsURL = baseNormalized + "/" + endpointNormalized |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | c.Config.HTTPConfig.RequestConfig.URL = metricsURL |
| 91 | |
| 92 | client, err := openmetrics.NewClient(openmetrics.Config{ |
| 93 | HTTPConfig: c.Config.HTTPConfig, |
| 94 | }) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | c.client = client |
| 99 | |
| 100 | if cfg.CollectRESTMatching != "" { |
| 101 | sel, err := matcher.NewSimplePatternsMatcher(cfg.CollectRESTMatching) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | c.restSelector = sel |
| 106 | } |
| 107 | |
| 108 | if cfg.MaxRESTEndpoints < 0 { |
| 109 | c.MaxRESTEndpoints = 0 |
| 110 | } |
| 111 | |
| 112 | c.identity = common.Identity{ |
| 113 | Cell: cfg.CellName, |
| 114 | Node: cfg.NodeName, |
| 115 | Server: cfg.ServerName, |
| 116 | } |
| 117 | |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | func (c *Collector) Cleanup(context.Context) { |
| 122 | } |