Skip to content

Commit

Permalink
Merge pull request #119 from INFURA/error-wrapping
Browse files Browse the repository at this point in the history
metrics: use fmt.Errorf to wrap errors instead of errors.Join
  • Loading branch information
MichaelMure authored Jul 11, 2023
2 parents f2837be + c3b2a12 commit fec60cf
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions plugin/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/ipfs/kubo/plugin"
"go.opentelemetry.io/contrib/propagators/autoprop"
Expand Down Expand Up @@ -89,7 +90,9 @@ func (m *MetricsPlugin) Init(_ *plugin.Environment) error {

// Close safely shuts down the plugin.
func (m *MetricsPlugin) Close() error {
return m.Shutdown(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return m.Shutdown(ctx)
}

func newMeterProvider(ctx context.Context) (shutDownMeterProvider, []metric.Exporter, error) {
Expand All @@ -102,7 +105,7 @@ func newMeterProvider(ctx context.Context) (shutDownMeterProvider, []metric.Expo
return &noopShutdownMeterProvider{noop.NewMeterProvider()}, nil, nil
}

options := []metric.Option{}
var options []metric.Option
for _, exporter := range exporters {
options = append(options, metric.WithReader(metric.NewPeriodicReader(exporter)))
}
Expand Down Expand Up @@ -158,26 +161,26 @@ func newMetricsExporters(ctx context.Context) ([]metric.Exporter, error) {
case otlpProtocolNameHTTP:
exporter, err := otlpmetrichttp.New(ctx)
if err != nil {
return nil, errors.Join(errBuildingOTLPHTTPExporter, err)
return nil, fmt.Errorf("%w: %w", errBuildingOTLPHTTPExporter, err)
}
exporters = append(exporters, exporter)
case otlpProtocolNameGRPC:
exporter, err := otlpmetricgrpc.New(ctx)
if err != nil {
return nil, errors.Join(errBuildingOTLPgRPCExporter, err)
return nil, fmt.Errorf("%w: %w", errBuildingOTLPgRPCExporter, err)
}
exporters = append(exporters, exporter)
default:
return nil, fmt.Errorf("%w %s", errUnsupportedProtocol, protocol)
return nil, fmt.Errorf("%w: %s", errUnsupportedProtocol, protocol)
}
case "stdout":
exporter, err := stdoutmetric.New()
if err != nil {
return nil, errors.Join(errBuildingStdoutExporter, err)
return nil, fmt.Errorf("%w: %w", errBuildingStdoutExporter, err)
}
exporters = append(exporters, exporter)
default:
return nil, fmt.Errorf("%w %s", errUnsupportedExporter, s)
return nil, fmt.Errorf("%w: %s", errUnsupportedExporter, s)
}
}

Expand Down

0 comments on commit fec60cf

Please sign in to comment.