Skip to content

Commit

Permalink
Merge branch 'main' into rm-any-attr
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored Aug 13, 2021
2 parents babdc3d + 4e8d667 commit 654e3df
Show file tree
Hide file tree
Showing 30 changed files with 328 additions and 450 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Metric SDK/API implementation type `InstrumentKind` moves into `sdkapi` sub-package. (#2091)
- The Metrics SDK export record no longer contains a Resource pointer, the SDK `"go.opentelemetry.io/otel/sdk/trace/export/metric".Exporter.Export()` function for push-based exporters now takes a single Resource argument, pull-based exporters use `"go.opentelemetry.io/otel/sdk/metric/controller/basic".Controller.Resource()`. (#2120)

### Deprecated

Expand All @@ -37,6 +38,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `fromEnv` detector no longer throws an error when `OTEL_RESOURCE_ATTRIBUTES` environment variable is not set or empty. (#2138)
- Setting the global `ErrorHandler` with `"go.opentelemetry.io/otel".SetErrorHandler` multiple times is now supported. (#2160, #2140)
- The `"go.opentelemetry.io/otel/attribute".Any` function now supports `int32` values. (#2169)
- Multiple calls to `"go.opentelemetry.io/otel/sdk/metric/controller/basic".WithResource()` are handled correctly, and when no resources are provided `"go.opentelemetry.io/otel/sdk/resource".Default()` is used. (#2120)

### Security

Expand Down
9 changes: 6 additions & 3 deletions bridge/opencensus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ type exporter struct {

// ExportMetrics implements the OpenCensus metric Exporter interface
func (e *exporter) ExportMetrics(ctx context.Context, metrics []*metricdata.Metric) error {
return e.base.Export(ctx, &checkpointSet{metrics: metrics})
res := resource.Empty()
if len(metrics) != 0 {
res = convertResource(metrics[0].Resource)
}
return e.base.Export(ctx, res, &checkpointSet{metrics: metrics})
}

type checkpointSet struct {
Expand All @@ -69,7 +73,6 @@ func (d *checkpointSet) ForEach(exporter export.ExportKindSelector, f func(expor
otel.Handle(err)
continue
}
res := convertResource(m.Resource)
for _, ts := range m.TimeSeries {
if len(ts.Points) == 0 {
continue
Expand All @@ -87,7 +90,6 @@ func (d *checkpointSet) ForEach(exporter export.ExportKindSelector, f func(expor
if err := f(export.NewRecord(
&descriptor,
&ls,
res,
agg,
ts.StartTime,
agg.end(),
Expand Down Expand Up @@ -119,6 +121,7 @@ func convertLabels(keys []metricdata.LabelKey, values []metricdata.LabelValue) (
}

// convertResource converts an OpenCensus Resource to an OpenTelemetry Resource
// Note: the ocresource.Resource Type field is not used.
func convertResource(res *ocresource.Resource) *resource.Resource {
labels := []attribute.KeyValue{}
if res == nil {
Expand Down
28 changes: 19 additions & 9 deletions bridge/opencensus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ import (

type fakeExporter struct {
export.Exporter
records []export.Record
err error
records []export.Record
resource *resource.Resource
err error
}

func (f *fakeExporter) Export(ctx context.Context, cps exportmetric.CheckpointSet) error {
func (f *fakeExporter) Export(ctx context.Context, res *resource.Resource, cps exportmetric.CheckpointSet) error {
return cps.ForEach(f, func(record exportmetric.Record) error {
f.resource = res
f.records = append(f.records, record)
return f.err
})
Expand Down Expand Up @@ -82,6 +84,7 @@ func TestExportMetrics(t *testing.T) {
input []*metricdata.Metric
exportErr error
expected []export.Record
expectedResource *resource.Resource
expectedHandledError error
}{
{
Expand Down Expand Up @@ -142,6 +145,12 @@ func TestExportMetrics(t *testing.T) {
desc: "success",
input: []*metricdata.Metric{
{
Resource: &ocresource.Resource{
Labels: map[string]string{
"R1": "V1",
"R2": "V2",
},
},
TimeSeries: []*metricdata.TimeSeries{
{
StartTime: now,
Expand All @@ -152,11 +161,14 @@ func TestExportMetrics(t *testing.T) {
},
},
},
expectedResource: resource.NewSchemaless(
attribute.String("R1", "V1"),
attribute.String("R2", "V2"),
),
expected: []export.Record{
export.NewRecord(
&basicDesc,
attribute.EmptySet(),
resource.NewSchemaless(),
&ocExactAggregator{
points: []aggregation.Point{
{
Expand Down Expand Up @@ -188,7 +200,6 @@ func TestExportMetrics(t *testing.T) {
export.NewRecord(
&basicDesc,
attribute.EmptySet(),
resource.NewSchemaless(),
&ocExactAggregator{
points: []aggregation.Point{
{
Expand Down Expand Up @@ -223,7 +234,6 @@ func TestExportMetrics(t *testing.T) {
export.NewRecord(
&basicDesc,
attribute.EmptySet(),
resource.NewSchemaless(),
&ocExactAggregator{
points: []aggregation.Point{
{
Expand Down Expand Up @@ -255,16 +265,16 @@ func TestExportMetrics(t *testing.T) {
if len(tc.expected) != len(output) {
t.Fatalf("ExportMetrics(%+v) = %d records, want %d records", tc.input, len(output), len(tc.expected))
}
if fakeExporter.resource.String() != tc.expectedResource.String() {
t.Errorf("ExportMetrics(%+v)[i].Resource() = %+v, want %+v", tc.input, fakeExporter.resource.String(), tc.expectedResource.String())
}
for i, expected := range tc.expected {
if output[i].StartTime() != expected.StartTime() {
t.Errorf("ExportMetrics(%+v)[i].StartTime() = %+v, want %+v", tc.input, output[i].StartTime(), expected.StartTime())
}
if output[i].EndTime() != expected.EndTime() {
t.Errorf("ExportMetrics(%+v)[i].EndTime() = %+v, want %+v", tc.input, output[i].EndTime(), expected.EndTime())
}
if output[i].Resource().String() != expected.Resource().String() {
t.Errorf("ExportMetrics(%+v)[i].Resource() = %+v, want %+v", tc.input, output[i].Resource().String(), expected.Resource().String())
}
if output[i].Descriptor().Name() != expected.Descriptor().Name() {
t.Errorf("ExportMetrics(%+v)[i].Descriptor() = %+v, want %+v", tc.input, output[i].Descriptor().Name(), expected.Descriptor().Name())
}
Expand Down
5 changes: 3 additions & 2 deletions exporters/otlp/otlpmetric/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"go.opentelemetry.io/otel/metric"
metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
"go.opentelemetry.io/otel/sdk/resource"
)

var (
Expand All @@ -42,8 +43,8 @@ type Exporter struct {
}

// Export exports a batch of metrics.
func (e *Exporter) Export(ctx context.Context, checkpointSet metricsdk.CheckpointSet) error {
rms, err := metrictransform.CheckpointSet(ctx, e, checkpointSet, 1)
func (e *Exporter) Export(ctx context.Context, res *resource.Resource, checkpointSet metricsdk.CheckpointSet) error {
rms, err := metrictransform.CheckpointSet(ctx, e, res, checkpointSet, 1)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 654e3df

Please sign in to comment.