Skip to content

Commit

Permalink
remove opencensus from carbonreceiver and wavefrontreceiver (open-tel…
Browse files Browse the repository at this point in the history
…emetry#23248)

Update carbonreceiver and wavefrontreceiver to use pdata directly instead of OpenCensus
  • Loading branch information
atoulme committed Jun 13, 2023
1 parent e0249e0 commit 9e4d50f
Show file tree
Hide file tree
Showing 17 changed files with 476 additions and 567 deletions.
20 changes: 20 additions & 0 deletions .chloggen/use-pdata-with-carbonreceiver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: carbonreceiver, wavefrontreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove use of opencensus model in carbonreceiver and wavefrontreceiver

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [20759, 20761]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
16 changes: 2 additions & 14 deletions receiver/carbonreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbon
go 1.19

require (
github.com/census-instrumentation/opencensus-proto v0.4.1
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.79.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/opencensus v0.79.0
github.com/stretchr/testify v1.8.4
go.opencensus.io v0.24.0
go.opentelemetry.io/collector v0.79.0
go.opentelemetry.io/collector/component v0.79.0
go.opentelemetry.io/collector/confmap v0.79.0
go.opentelemetry.io/collector/consumer v0.79.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012
go.opentelemetry.io/collector/receiver v0.79.0
go.uber.org/zap v1.24.0
google.golang.org/protobuf v1.30.0
)

require (
Expand All @@ -37,7 +35,6 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.79.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.15.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
Expand All @@ -46,8 +43,6 @@ require (
github.com/prometheus/statsd_exporter v0.22.7 // indirect
go.opentelemetry.io/collector/exporter v0.79.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0012 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012 // indirect
go.opentelemetry.io/collector/semconv v0.79.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.39.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
Expand All @@ -61,22 +56,15 @@ require (
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/opencensus => ../../pkg/translator/opencensus

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

retract (
v0.76.2
v0.76.1
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil
8 changes: 0 additions & 8 deletions receiver/carbonreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 2 additions & 36 deletions receiver/carbonreceiver/protocol/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
package protocol // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver/protocol"

import (
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"google.golang.org/protobuf/types/known/timestamppb"
"go.opentelemetry.io/collector/pdata/pmetric"
)

// Parser abstracts the type of parsing being done by the receiver.
Expand All @@ -24,38 +23,5 @@ type Parser interface {
//
// The <metric_timestamp> is the Unix time text of when the measurement was
// made.
Parse(line string) (*metricspb.Metric, error)
}

// Below a few helper functions useful to different parsers.
func buildMetricForSinglePoint(
metricName string,
metricType metricspb.MetricDescriptor_Type,
labelKeys []*metricspb.LabelKey,
labelValues []*metricspb.LabelValue,
point *metricspb.Point,
) *metricspb.Metric {
return &metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: metricName,
Type: metricType,
LabelKeys: labelKeys,
},
Timeseries: []*metricspb.TimeSeries{
{
// TODO: StartTimestamp can be set if each cumulative time series are
// tracked but right now it is not clear if it brings benefits.
// Perhaps as an option so cost is "pay for play".
LabelValues: labelValues,
Points: []*metricspb.Point{point},
},
},
}
}

func convertUnixSec(sec int64) *timestamppb.Timestamp {
ts := &timestamppb.Timestamp{
Seconds: sec,
}
return ts
Parse(line string) (pmetric.Metric, error)
}
69 changes: 32 additions & 37 deletions receiver/carbonreceiver/protocol/path_parser_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"fmt"
"strconv"
"strings"
"time"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)

// PathParser implements the code needed to handle only the <metric_path> part of
Expand All @@ -35,10 +37,8 @@ type PathParser interface {
type ParsedPath struct {
// MetricName extracted/generated by the parser.
MetricName string
// LabelKeys extracted/generated by the parser.
LabelKeys []*metricspb.LabelKey
// LabelValues extracted/generated by the parser.
LabelValues []*metricspb.LabelValue
// Attributes extracted/generated by the parser.
Attributes pcommon.Map
// MetricType instructs the helper to generate the metric as the specified
// TargetMetricType.
MetricType TargetMetricType
Expand Down Expand Up @@ -88,10 +88,10 @@ func NewParser(pathParser PathParser) (Parser, error) {
//
// The <metric_timestamp> is the Unix time text of when the measurement was
// made.
func (pph *PathParserHelper) Parse(line string) (*metricspb.Metric, error) {
func (pph *PathParserHelper) Parse(line string) (pmetric.Metric, error) {
parts := strings.SplitN(line, " ", 4)
if len(parts) != 3 {
return nil, fmt.Errorf("invalid carbon metric [%s]", line)
return pmetric.Metric{}, fmt.Errorf("invalid carbon metric [%s]", line)
}

path := parts[0]
Expand All @@ -101,44 +101,39 @@ func (pph *PathParserHelper) Parse(line string) (*metricspb.Metric, error) {
parsedPath := ParsedPath{}
err := pph.pathParser.ParsePath(path, &parsedPath)
if err != nil {
return nil, fmt.Errorf("invalid carbon metric [%s]: %w", line, err)
return pmetric.Metric{}, fmt.Errorf("invalid carbon metric [%s]: %w", line, err)
}

unixTime, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid carbon metric time [%s]: %w", line, err)
return pmetric.Metric{}, fmt.Errorf("invalid carbon metric time [%s]: %w", line, err)
}

var metricType metricspb.MetricDescriptor_Type
point := metricspb.Point{
Timestamp: convertUnixSec(unixTime),
}
intVal, err := strconv.ParseInt(valueStr, 10, 64)
if err == nil {
if parsedPath.MetricType == CumulativeMetricType {
metricType = metricspb.MetricDescriptor_CUMULATIVE_INT64
} else {
metricType = metricspb.MetricDescriptor_GAUGE_INT64
}
point.Value = &metricspb.Point_Int64Value{Int64Value: intVal}
} else {
dblVal, err := strconv.ParseFloat(valueStr, 64)
intVal, errIsFloat := strconv.ParseInt(valueStr, 10, 64)
var dblVal float64
if errIsFloat != nil {
dblVal, err = strconv.ParseFloat(valueStr, 64)
if err != nil {
return nil, fmt.Errorf("invalid carbon metric value [%s]: %w", line, err)
}
if parsedPath.MetricType == CumulativeMetricType {
metricType = metricspb.MetricDescriptor_CUMULATIVE_DOUBLE
} else {
metricType = metricspb.MetricDescriptor_GAUGE_DOUBLE
return pmetric.Metric{}, fmt.Errorf("invalid carbon metric value [%s]: %w", line, err)
}
point.Value = &metricspb.Point_DoubleValue{DoubleValue: dblVal}
}

metric := buildMetricForSinglePoint(
parsedPath.MetricName,
metricType,
parsedPath.LabelKeys,
parsedPath.LabelValues,
&point)
return metric, nil
m := pmetric.NewMetric()
m.SetName(parsedPath.MetricName)
var dp pmetric.NumberDataPoint
if parsedPath.MetricType == CumulativeMetricType {
sum := m.SetEmptySum()
sum.SetIsMonotonic(true)
dp = sum.DataPoints().AppendEmpty()
} else {
dp = m.SetEmptyGauge().DataPoints().AppendEmpty()
}
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(unixTime, 0)))
if errIsFloat != nil {
dp.SetDoubleValue(dblVal)
} else {
dp.SetIntValue(intVal)
}
parsedPath.Attributes.CopyTo(dp.Attributes())
return m, nil
}
14 changes: 3 additions & 11 deletions receiver/carbonreceiver/protocol/plaintext_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"strings"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"go.opentelemetry.io/collector/pdata/pcommon"
)

// PlaintextConfig holds the configuration for the plaintext parser.
Expand Down Expand Up @@ -44,6 +44,7 @@ func (p *PlaintextPathParser) ParsePath(path string, parsedPath *ParsedPath) err
}

parsedPath.MetricName = parts[0]
parsedPath.Attributes = pcommon.NewMap()
if len(parts) == 1 {
// No tags, no more work here.
return nil
Expand All @@ -55,26 +56,17 @@ func (p *PlaintextPathParser) ParsePath(path string, parsedPath *ParsedPath) err
}

tags := strings.Split(parts[1], ";")
keys := make([]*metricspb.LabelKey, 0, len(tags))
values := make([]*metricspb.LabelValue, 0, len(tags))
for _, tag := range tags {
idx := strings.IndexByte(tag, '=')
if idx < 1 {
return fmt.Errorf("cannot parse metric path [%s]: incorrect key value separator for [%s]", path, tag)
}

key := tag[:idx]
keys = append(keys, &metricspb.LabelKey{Key: key})

value := tag[idx+1:] // If value is empty, ie.: tag == "k=", this will return "".
values = append(values, &metricspb.LabelValue{
Value: value,
HasValue: true,
})
parsedPath.Attributes.PutStr(key, value)
}

parsedPath.LabelKeys = keys
parsedPath.LabelValues = values
return nil
}

Expand Down
Loading

0 comments on commit 9e4d50f

Please sign in to comment.