Skip to content

Commit 45ec842

Browse files
improve parsing of target param to handle missing scheme format (#75)
1 parent a0a6a7b commit 45ec842

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

exporter/exporter.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ type Plugin interface {
114114

115115
// NewExporter returns an initialized Exporter for a redfish API capable device.
116116
func NewExporter(ctx context.Context, target, uri, profile, model string, excludes Excludes, plugins ...Plugin) (*Exporter, error) {
117-
var fqdn *url.URL
117+
var u *url.URL
118118
var tasks []*pool.Task
119119
var exp = Exporter{
120120
ctx: ctx,
@@ -158,15 +158,17 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
158158
exp.client = retryClient
159159

160160
// Check that the target passed in has http:// or https:// prefixed
161-
fqdn, err := url.ParseRequestURI(target)
162-
if err != nil {
163-
fqdn = &url.URL{
164-
Scheme: config.GetConfig().BMCScheme,
165-
Host: target,
161+
u, err := url.ParseRequestURI(target)
162+
if err != nil || u.Host == "" {
163+
u, err = url.ParseRequestURI(config.GetConfig().BMCScheme + "://" + target)
164+
if err != nil {
165+
log.Error("error parsing target param", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
166+
return &exp, err
166167
}
167168
}
168-
exp.host = fqdn.Hostname()
169-
exp.url = fqdn.String()
169+
170+
exp.host = u.Hostname()
171+
exp.url = u.String()
170172

171173
// check if host is on the ignored list, if so we immediately return
172174
if _, ok := common.IgnoredDevices[exp.host]; ok {
@@ -177,7 +179,7 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
177179

178180
chassisEndpoints, err := getMemberUrls(exp.url+uri+"/Chassis/", target, retryClient)
179181
if err != nil {
180-
log.Error("error when getting chassis url from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
182+
log.Error("error when getting chassis url", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
181183
if errors.Is(err, common.ErrInvalidCredential) {
182184
common.IgnoredDevices[exp.host] = common.IgnoredDevice{
183185
Name: exp.host,
@@ -198,7 +200,7 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
198200

199201
mgrEndpoints, err := getMemberUrls(exp.url+uri+"/Managers/", target, retryClient)
200202
if err != nil {
201-
log.Error("error when getting manager endpoint from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
203+
log.Error("error when getting manager endpoint", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
202204
return nil, err
203205
}
204206

@@ -232,7 +234,7 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
232234
// point for the systems and manager endpoints
233235
sysEndpoints, err := getSystemEndpoints(chasUrlsFinal, target, retryClient, excludes)
234236
if err != nil {
235-
log.Error("error when getting chassis endpoints from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
237+
log.Error("error when getting chassis endpoints", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
236238
return nil, err
237239
}
238240

@@ -250,7 +252,7 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
250252
// call /redfish/v1/Systems/XXXXX/ for BIOS, Serial number
251253
sysResp, err = getSystemsMetadata(exp.url+sysEndpoints.systems[0], target, retryClient)
252254
if err != nil {
253-
log.Error("error when getting BIOS version from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
255+
log.Error("error when getting BIOS version", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
254256
return nil, err
255257
}
256258
exp.biosVersion = sysResp.BiosVersion
@@ -267,14 +269,14 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
267269
// DIMM endpoints array
268270
dimms, err = getDIMMEndpoints(exp.url+sysEndpoints.systems[0]+"Memory/", target, retryClient)
269271
if err != nil {
270-
log.Error("error when getting DIMM endpoints from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
272+
log.Error("error when getting DIMM endpoints", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
271273
return nil, err
272274
}
273275

274276
// CPU processor metrics
275277
processors, err = getProcessorEndpoints(exp.url+sysEndpoints.systems[0]+"Processors/", target, retryClient)
276278
if err != nil {
277-
log.Error("error when getting Processors endpoints from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
279+
log.Error("error when getting Processors endpoints", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
278280
return nil, err
279281
}
280282
} else {
@@ -298,7 +300,7 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
298300
if ss != "" {
299301
driveEndpointsResp, err = getAllDriveEndpoints(ctx, exp.url, exp.url+ss, target, retryClient)
300302
if err != nil {
301-
log.Error("error when getting drive endpoints from "+model, zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
303+
log.Error("error when getting drive endpoints", zap.Error(err), zap.Any("trace_id", ctx.Value("traceID")))
302304
return nil, err
303305
}
304306
}
@@ -441,7 +443,7 @@ func (e *Exporter) scrape() {
441443
}
442444
var upMetric = (*e.DeviceMetrics)["up"]
443445
(*upMetric)["up"].WithLabelValues().Set(float64(deviceState))
444-
log.Error("error calling redfish api from "+e.Model, zap.Error(task.Err), zap.String("url", task.URL), zap.Any("trace_id", e.ctx.Value("traceID")))
446+
log.Error("error calling redfish api", zap.Error(task.Err), zap.String("url", task.URL), zap.Any("trace_id", e.ctx.Value("traceID")))
445447
return
446448
}
447449

@@ -450,7 +452,7 @@ func (e *Exporter) scrape() {
450452
}
451453

452454
if err != nil {
453-
log.Error("error exporting metrics - from "+e.Model, zap.Error(err), zap.String("url", task.URL), zap.Any("trace_id", e.ctx.Value("traceID")))
455+
log.Error("error exporting metrics", zap.Error(err), zap.String("url", task.URL), zap.Any("trace_id", e.ctx.Value("traceID")))
454456
continue
455457
}
456458
scrapeChan <- 1

0 commit comments

Comments
 (0)