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

Spring cleaning. #115

Merged
merged 4 commits into from
Mar 24, 2021
Merged

Spring cleaning. #115

merged 4 commits into from
Mar 24, 2021

Conversation

a-feld
Copy link

@a-feld a-feld commented Mar 23, 2021

Some spring cleaning 😄

details.dataOutputCount += len(nrMetrics)
batch.Metrics = append(batch.Metrics, nrMetrics...)
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you reorganized it as follows, we'd be able to independently test the preparation of the payload from the measurement / sending of the http request (and all the code would be indented one fewer tab):

func (e exporter) pushMetricData(ctx context.Context, md pdata.Metrics) (outputErr error) {
	details := newMetricMetadata(ctx)
	_, details.dataInputCount = md.MetricAndDataPointCount()
	bb := func () (telemetry.PayloadEntry, error) {
		return e.buildMetricPayload(details, ctx, md)
	}
	return e.export(&details, ctx, bb);
}

func (e exporter) buildMetricPayload(details exportMetadata, ctx context.Context, md pdata.Metrics) (telemetry.PayloadEntry, error) {
	var (
		errs  []error
		batch telemetry.MetricBatch
	)

	rms := md.ResourceMetrics()
	_, dpCount := md.MetricAndDataPointCount()
	batch.Metrics = make([]telemetry.Metric, 0, dpCount)
	for i := 0; i < rms.Len(); i++ {
		rm := rms.At(i)
		ilms := rm.InstrumentationLibraryMetrics()
		for j := 0; j < ilms.Len(); j++ {
			ilm := ilms.At(j)
			ms := ilm.Metrics()
			transform := newTransformer(rm.Resource(), ilm.InstrumentationLibrary())
			for k := 0; k < ms.Len(); k++ {
				m := ms.At(k)
				nrMetrics, err := transform.Metric(m)
				if err != nil {
					e.logger.Debug("Transform of metric failed.", zap.Error(err))
					errs = append(errs, err)
					continue
				}
				details.dataOutputCount += len(nrMetrics)
				batch.Metrics = append(batch.Metrics, nrMetrics...)
			}
		}
	}

	return &batch, consumererror.CombineErrors(errs)
}

@a-feld a-feld force-pushed the newrelic-main branch 3 times, most recently from 895ec28 to 3208027 Compare March 24, 2021 14:55
@a-feld a-feld force-pushed the cleanup-exporter branch from 1959352 to b2e7590 Compare March 24, 2021 14:57
@a-feld a-feld force-pushed the cleanup-exporter branch from b2e7590 to 2254f52 Compare March 24, 2021 15:00
@a-feld a-feld force-pushed the newrelic-main branch 2 times, most recently from 2d2941c to c445064 Compare March 24, 2021 15:03
@a-feld a-feld force-pushed the cleanup-exporter branch from 2254f52 to 6bf8cf1 Compare March 24, 2021 15:21
@a-feld a-feld marked this pull request as ready for review March 24, 2021 15:23
e.logger.Error("An error occurred recording metrics.", zap.Error(err))
}
}()
func (e exporter) buildTracePayload(details *exportMetadata, td pdata.Traces) (telemetry.PayloadEntry, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops gotta change *exportMetadata to exportMetadata.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that mean we wouldn't be able to modify attributes in the details? 🤔

Copy link

@jack-berg jack-berg Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maybe not. export has to have a pointer for exportMetadata because it needs to change its values.

I think that suggests we should use pointers to *exportMetadata throughout for consistency, including in metrics.go.

return nil, fmt.Errorf("invalid config: %#v", c)
}

func newExporter(l *zap.Logger, nrConfig endpointConfig, createFactory factoryBuilder) (*exporter, error) {
Copy link

@jack-berg jack-berg Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to return a pointer or value here? If pointer, should we make all the methods attached to type exporter work off pointers as well? Or does that not matter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change to a value type, that means we can no longer return nil. Is that preferable? 🤔

@a-feld a-feld merged commit bde6d05 into newrelic-main Mar 24, 2021
@a-feld a-feld deleted the cleanup-exporter branch March 24, 2021 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants