Skip to content

Commit

Permalink
switch to fmt.Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
huyan0 committed Aug 26, 2020
1 parent a39929e commit 89a6e31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
26 changes: 13 additions & 13 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"

"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/prometheus/prometheus/prompb"

"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/consumer/pdatautil"
"go.opentelemetry.io/collector/internal/data"
Expand All @@ -50,12 +50,12 @@ type prwExporter struct {
func newPrwExporter(namespace string, endpoint string, client *http.Client) (*prwExporter, error) {

if client == nil {
return nil, errors.New("http client cannot be nil")
return nil, fmt.Errorf("http client cannot be nil")
}

endpointURL, err := url.ParseRequestURI(endpoint)
if err != nil {
return nil, errors.New("invalid endpoint")
return nil, fmt.Errorf("invalid endpoint")
}

return &prwExporter{
Expand Down Expand Up @@ -83,11 +83,11 @@ func (prwe *prwExporter) pushMetrics(ctx context.Context, md pdata.Metrics) (int
defer prwe.wg.Done()
select {
case <-prwe.closeChan:
return pdatautil.MetricCount(md), errors.New("shutdown has been called")
return pdatautil.MetricCount(md), fmt.Errorf("shutdown has been called")
default:
tsMap := map[string]*prompb.TimeSeries{}
dropped := 0
errs := []string{}
errs := []error{}

resourceMetrics := data.MetricDataToOtlp(pdatautil.MetricsToInternalMetrics(md))
for _, resourceMetric := range resourceMetrics {
Expand All @@ -98,7 +98,7 @@ func (prwe *prwExporter) pushMetrics(ctx context.Context, md pdata.Metrics) (int
// check for valid type and temporality combination
if ok := validateMetrics(metric.MetricDescriptor); !ok {
dropped++
errs = append(errs, "invalid temporality and type combination")
errs = append(errs, fmt.Errorf("invalid temporality and type combination"))
continue
}
// handle individual metric based on type
Expand All @@ -107,7 +107,7 @@ func (prwe *prwExporter) pushMetrics(ctx context.Context, md pdata.Metrics) (int
otlp.MetricDescriptor_MONOTONIC_DOUBLE, otlp.MetricDescriptor_DOUBLE:
if err := prwe.handleScalarMetric(tsMap, metric); err != nil {
dropped++
errs = append(errs, err.Error())
errs = append(errs, err)
}
}
}
Expand All @@ -119,7 +119,7 @@ func (prwe *prwExporter) pushMetrics(ctx context.Context, md pdata.Metrics) (int
}

if dropped != 0 {
return dropped, errors.New(strings.Join(errs, "\n"))
return dropped, componenterror.CombineErrors(errs)
}

return 0, nil
Expand All @@ -137,7 +137,7 @@ func (prwe *prwExporter) handleScalarMetric(tsMap map[string]*prompb.TimeSeries,
// int points
case otlp.MetricDescriptor_MONOTONIC_INT64, otlp.MetricDescriptor_INT64:
if metric.Int64DataPoints == nil {
return errors.New("nil data point field in metric" + metric.GetMetricDescriptor().Name)
return fmt.Errorf("nil data point field in metric %v", metric.GetMetricDescriptor().Name)
}

for _, pt := range metric.Int64DataPoints {
Expand All @@ -158,7 +158,7 @@ func (prwe *prwExporter) handleScalarMetric(tsMap map[string]*prompb.TimeSeries,
// double points
case otlp.MetricDescriptor_MONOTONIC_DOUBLE, otlp.MetricDescriptor_DOUBLE:
if metric.DoubleDataPoints == nil {
return errors.New("nil data point field in metric" + metric.GetMetricDescriptor().Name)
return fmt.Errorf("nil data point field in metric %v", metric.GetMetricDescriptor().Name)
}
for _, pt := range metric.DoubleDataPoints {

Expand All @@ -175,7 +175,7 @@ func (prwe *prwExporter) handleScalarMetric(tsMap map[string]*prompb.TimeSeries,
return nil
}

return errors.New("invalid metric type: wants int or double data points")
return fmt.Errorf("invalid metric type: wants int or double data points")
}

// export sends a Snappy-compressed WriteRequest containing TimeSeries to a remote write endpoint in order
Expand Down Expand Up @@ -223,7 +223,7 @@ func (prwe *prwExporter) export(ctx context.Context, tsMap map[string]*prompb.Ti
line = scanner.Text()
}
errMsg := "server returned HTTP status " + httpResp.Status + ": " + line
return errors.New(errMsg)
return fmt.Errorf(errMsg)
}
return nil
}
4 changes: 2 additions & 2 deletions exporter/prometheusremotewriteexporter/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package prometheusremotewriteexporter

import (
"fmt"
"log"
"sort"
"strings"
"time"
"unicode"

"github.com/pkg/errors"
"github.com/prometheus/prometheus/prompb"

common "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1"
Expand Down Expand Up @@ -186,7 +186,7 @@ func getPromMetricName(desc *otlp.MetricDescriptor, ns string) string {
// and creates a WriteRequest from the struct -- can move to the helper.go file
func wrapTimeSeries(tsMap map[string]*prompb.TimeSeries) (*prompb.WriteRequest, error) {
if len(tsMap) == 0 {
return nil, errors.Errorf("invalid tsMap: cannot be empty map")
return nil, fmt.Errorf("invalid tsMap: cannot be empty map")
}
TsArray := []prompb.TimeSeries{}
for _, v := range tsMap {
Expand Down

0 comments on commit 89a6e31

Please sign in to comment.