Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change signalfx translator to expose To/From translator structs #9740

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 🛑 Breaking changes 🛑

- `datadogexporter`: Replace HistogramMode defined as string with enum.
- `pkg/translator/signalfx`: Change signalfx translator to expose To/From translator structs. (#9740)

### 🚩 Deprecations 🚩

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var (
sfxMetricTypeGauge = sfxpb.MetricType_GAUGE
sfxMetricTypeCumulativeCounter = sfxpb.MetricType_CUMULATIVE_COUNTER
sfxMetricTypeCounter = sfxpb.MetricType_COUNTER

translator = &signalfx.FromTranslator{}
)

// MetricsConverter converts MetricsData to sfxpb DataPoints. It holds an optional
Expand Down Expand Up @@ -85,7 +87,7 @@ func (c *MetricsConverter) MetricsToSignalFxV2(md pmetric.Metrics) []*sfxpb.Data
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ilm := rm.ScopeMetrics().At(j)
for k := 0; k < ilm.Metrics().Len(); k++ {
dps := signalfx.FromMetric(ilm.Metrics().At(k), extraDimensions)
dps := translator.FromMetric(ilm.Metrics().At(k), extraDimensions)
dps = c.translateAndFilter(dps)
sfxDataPoints = append(sfxDataPoints, dps...)
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/translator/signalfx/from_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ const (
quantileDimensionKey = "quantile"
)

// FromTranslator converts from pdata to SignalFx proto data model.
type FromTranslator struct{}

// FromMetrics converts pmetric.Metrics to SignalFx proto data points.
func FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, error) {
func (ft *FromTranslator) FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, error) {
var sfxDataPoints []*sfxpb.DataPoint

rms := md.ResourceMetrics()
Expand All @@ -55,7 +58,7 @@ func FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, error) {
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ilm := rm.ScopeMetrics().At(j)
for k := 0; k < ilm.Metrics().Len(); k++ {
sfxDataPoints = append(sfxDataPoints, FromMetric(ilm.Metrics().At(k), extraDimensions)...)
sfxDataPoints = append(sfxDataPoints, ft.FromMetric(ilm.Metrics().At(k), extraDimensions)...)
}
}
}
Expand All @@ -65,7 +68,7 @@ func FromMetrics(md pmetric.Metrics) ([]*sfxpb.DataPoint, error) {

// FromMetric converts pmetric.Metric to SignalFx proto data points.
// TODO: Remove this and change signalfxexporter to us FromMetrics.
func FromMetric(m pmetric.Metric, extraDimensions []*sfxpb.Dimension) []*sfxpb.DataPoint {
func (ft *FromTranslator) FromMetric(m pmetric.Metric, extraDimensions []*sfxpb.Dimension) []*sfxpb.DataPoint {
var dps []*sfxpb.DataPoint

mt := fromMetricTypeToMetricType(m)
Expand Down
4 changes: 2 additions & 2 deletions pkg/translator/signalfx/from_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ func Test_FromMetrics(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
md := tt.metricsFn()
gotSfxDataPoints, err := FromMetrics(md)
from := &FromTranslator{}
gotSfxDataPoints, err := from.FromMetrics(tt.metricsFn())
require.NoError(t, err)
// Sort SFx dimensions since they are built from maps and the order
// of those is not deterministic.
Expand Down
5 changes: 4 additions & 1 deletion pkg/translator/signalfx/to_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import (
"go.uber.org/multierr"
)

// ToTranslator converts from SignalFx proto data model to pdata.
type ToTranslator struct{}

// ToMetrics converts SignalFx proto data points to pmetric.Metrics.
func ToMetrics(sfxDataPoints []*model.DataPoint) (pmetric.Metrics, error) {
func (tt *ToTranslator) ToMetrics(sfxDataPoints []*model.DataPoint) (pmetric.Metrics, error) {
// TODO: not optimized at all, basically regenerating everything for each
// data point.
md := pmetric.NewMetrics()
Expand Down
3 changes: 2 additions & 1 deletion pkg/translator/signalfx/to_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ func Test_ToMetrics(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
md, err := ToMetrics(tt.sfxDataPoints)
to := &ToTranslator{}
md, err := to.ToMetrics(tt.sfxDataPoints)
if tt.wantError {
assert.Error(t, err)
} else {
Expand Down
4 changes: 3 additions & 1 deletion receiver/signalfxreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ var (
errNextConsumerRespBody = initJSONResponse(responseErrNextConsumer)
errLogsNotConfigured = initJSONResponse(responseErrLogsNotConfigured)
errMetricsNotConfigured = initJSONResponse(responseErrMetricsNotConfigured)

translator = &signalfx.ToTranslator{}
)

// sfxReceiver implements the component.MetricsReceiver for SignalFx metric protocol.
Expand Down Expand Up @@ -237,7 +239,7 @@ func (r *sfxReceiver) handleDatapointReq(resp http.ResponseWriter, req *http.Req
return
}

md, err := signalfx.ToMetrics(msg.Datapoints)
md, err := translator.ToMetrics(msg.Datapoints)
if err != nil {
r.settings.Logger.Debug("SignalFx conversion error", zap.Error(err))
}
Expand Down