Skip to content

Commit

Permalink
add transition from instrumentationlibrary -> scope
Browse files Browse the repository at this point in the history
This is the last piece of upgrading to v0.15.0. This PR adds the tests and code to ensure the transition plan is implemented in the collector.

Fixes #5074
  • Loading branch information
Alex Boten committed Mar 24, 2022
1 parent f496cd7 commit 9f21939
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 3 deletions.
32 changes: 31 additions & 1 deletion model/otlpgrpc/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"google.golang.org/grpc"

otlpcollectorlog "go.opentelemetry.io/collector/model/internal/data/protogen/collector/logs/v1"
v1 "go.opentelemetry.io/collector/model/internal/data/protogen/common/v1"
otlplogs "go.opentelemetry.io/collector/model/internal/data/protogen/logs/v1"
ipdata "go.opentelemetry.io/collector/model/internal/pdata"
"go.opentelemetry.io/collector/model/pdata"
Expand Down Expand Up @@ -139,7 +140,36 @@ func (lr LogsRequest) MarshalJSON() ([]byte, error) {

// UnmarshalJSON unmarshalls LogsRequest from JSON bytes.
func (lr LogsRequest) UnmarshalJSON(data []byte) error {
return jsonUnmarshaler.Unmarshal(bytes.NewReader(data), lr.orig)
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), lr.orig); err != nil {
return err
}
lr.instrumentationLibraryLogsToScope()
return nil
}

// instrumentationLibraryLogsToScope implements the translation of resource logs data
// following the v0.15.0 upgrade:
// receivers SHOULD check if instrumentation_library_logs is set
// and scope_logs is not set then the value in instrumentation_library_logs
// SHOULD be used instead by converting InstrumentationLibraryLogs into ScopeLogs.
// If scope_logs is set then instrumentation_library_logs SHOULD be ignored.
func (lr LogsRequest) instrumentationLibraryLogsToScope() {
for _, rl := range lr.orig.ResourceLogs {
if len(rl.ScopeLogs) == 0 {
for _, ill := range rl.InstrumentationLibraryLogs {
scopeMetrics := otlplogs.ScopeLogs{
Scope: v1.InstrumentationScope{
Name: ill.InstrumentationLibrary.Name,
Version: ill.InstrumentationLibrary.Version,
},
LogRecords: ill.LogRecords,
SchemaUrl: ill.SchemaUrl,
}
rl.ScopeLogs = append(rl.ScopeLogs, &scopeMetrics)
}
}
rl.InstrumentationLibraryLogs = nil
}
}

func (lr LogsRequest) SetLogs(ld pdata.Logs) {
Expand Down
73 changes: 73 additions & 0 deletions model/otlpgrpc/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,67 @@ var logsRequestJSON = []byte(`
]
}`)

var logsTransitionData = [][]byte{
[]byte(`
{
"resourceLogs": [
{
"resource": {},
"instrumentationLibraryLogs": [
{
"instrumentationLibrary": {},
"logRecords": [
{
"body": {
"stringValue": "test_log_record"
},
"traceId": "",
"spanId": ""
}
]
}
]
}
]
}`),
[]byte(`
{
"resourceLogs": [
{
"resource": {},
"instrumentationLibraryLogs": [
{
"instrumentationLibrary": {},
"logRecords": [
{
"body": {
"stringValue": "test_log_record"
},
"traceId": "",
"spanId": ""
}
]
}
],
"scopeLogs": [
{
"scope": {},
"logRecords": [
{
"body": {
"stringValue": "test_log_record"
},
"traceId": "",
"spanId": ""
}
]
}
]
}
]
}`),
}

func TestLogsRequestJSON(t *testing.T) {
lr := NewLogsRequest()
assert.NoError(t, lr.UnmarshalJSON(logsRequestJSON))
Expand All @@ -73,6 +134,18 @@ func TestLogsRequestJSON(t *testing.T) {
assert.Equal(t, strings.Join(strings.Fields(string(logsRequestJSON)), ""), string(got))
}

func TestLogsRequestJSONTransition(t *testing.T) {
for _, data := range logsTransitionData {
lr := NewLogsRequest()
assert.NoError(t, lr.UnmarshalJSON(data))
assert.Equal(t, "test_log_record", lr.Logs().ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Body().AsString())

got, err := lr.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, strings.Join(strings.Fields(string(logsRequestJSON)), ""), string(got))
}
}

func TestLogsRequestJSON_Deprecated(t *testing.T) {
lr, err := UnmarshalJSONLogsRequest(logsRequestJSON)
assert.NoError(t, err)
Expand Down
32 changes: 31 additions & 1 deletion model/otlpgrpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"google.golang.org/grpc"

otlpcollectormetrics "go.opentelemetry.io/collector/model/internal/data/protogen/collector/metrics/v1"
v1 "go.opentelemetry.io/collector/model/internal/data/protogen/common/v1"
otlpmetrics "go.opentelemetry.io/collector/model/internal/data/protogen/metrics/v1"
ipdata "go.opentelemetry.io/collector/model/internal/pdata"
"go.opentelemetry.io/collector/model/pdata"
Expand Down Expand Up @@ -135,7 +136,36 @@ func (mr MetricsRequest) MarshalJSON() ([]byte, error) {

// UnmarshalJSON unmarshalls MetricsRequest from JSON bytes.
func (mr MetricsRequest) UnmarshalJSON(data []byte) error {
return jsonUnmarshaler.Unmarshal(bytes.NewReader(data), mr.orig)
if err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), mr.orig); err != nil {
return err
}
mr.instrumentationLibraryMetricsToScope()
return nil
}

// instrumentationLibraryMetricsToScope implements the translation of resource metrics data
// following the v0.15.0 upgrade:
// receivers SHOULD check if instrumentation_library_metrics is set
// and scope_metrics is not set then the value in instrumentation_library_metrics
// SHOULD be used instead by converting InstrumentationLibraryMetrics into ScopeMetrics.
// If scope_metrics is set then instrumentation_library_metrics SHOULD be ignored.
func (mr MetricsRequest) instrumentationLibraryMetricsToScope() {
for _, rm := range mr.orig.ResourceMetrics {
if len(rm.ScopeMetrics) == 0 {
for _, ilm := range rm.InstrumentationLibraryMetrics {
scopeMetrics := otlpmetrics.ScopeMetrics{
Scope: v1.InstrumentationScope{
Name: ilm.InstrumentationLibrary.Name,
Version: ilm.InstrumentationLibrary.Version,
},
Metrics: ilm.Metrics,
SchemaUrl: ilm.SchemaUrl,
}
rm.ScopeMetrics = append(rm.ScopeMetrics, &scopeMetrics)
}
}
rm.InstrumentationLibraryMetrics = nil
}
}

func (mr MetricsRequest) SetMetrics(ld pdata.Metrics) {
Expand Down
61 changes: 61 additions & 0 deletions model/otlpgrpc/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,55 @@ var metricsRequestJSON = []byte(`
]
}`)

var metricsTransitionData = [][]byte{
[]byte(`
{
"resourceMetrics": [
{
"resource": {},
"instrumentationLibraryMetrics": [
{
"instrumentationLibrary": {},
"metrics": [
{
"name": "test_metric"
}
]
}
]
}
]
}`),
[]byte(`
{
"resourceMetrics": [
{
"resource": {},
"instrumentationLibraryMetrics": [
{
"instrumentationLibrary": {},
"metrics": [
{
"name": "test_metric"
}
]
}
],
"scopeMetrics": [
{
"scope": {},
"metrics": [
{
"name": "test_metric"
}
]
}
]
}
]
}`),
}

func TestMetricsRequestJSON(t *testing.T) {
mr := NewMetricsRequest()
assert.NoError(t, mr.UnmarshalJSON(metricsRequestJSON))
Expand All @@ -69,6 +118,18 @@ func TestMetricsRequestJSON(t *testing.T) {
assert.Equal(t, strings.Join(strings.Fields(string(metricsRequestJSON)), ""), string(got))
}

func TestMetricsRequestJSONTransition(t *testing.T) {
for _, data := range metricsTransitionData {
mr := NewMetricsRequest()
assert.NoError(t, mr.UnmarshalJSON(data))
assert.Equal(t, "test_metric", mr.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Name())

got, err := mr.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, strings.Join(strings.Fields(string(metricsRequestJSON)), ""), string(got))
}
}

func TestMetricsRequestJSON_Deprecated(t *testing.T) {
mr, err := UnmarshalJSONMetricsRequest(metricsRequestJSON)
assert.NoError(t, err)
Expand Down
33 changes: 32 additions & 1 deletion model/otlpgrpc/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"google.golang.org/grpc"

otlpcollectortrace "go.opentelemetry.io/collector/model/internal/data/protogen/collector/trace/v1"
v1 "go.opentelemetry.io/collector/model/internal/data/protogen/common/v1"
otlptrace "go.opentelemetry.io/collector/model/internal/data/protogen/trace/v1"
ipdata "go.opentelemetry.io/collector/model/internal/pdata"
"go.opentelemetry.io/collector/model/pdata"
Expand Down Expand Up @@ -135,7 +136,37 @@ func (tr TracesRequest) MarshalJSON() ([]byte, error) {

// UnmarshalJSON unmarshalls TracesRequest from JSON bytes.
func (tr TracesRequest) UnmarshalJSON(data []byte) error {
return jsonUnmarshaler.Unmarshal(bytes.NewReader(data), tr.orig)
err := jsonUnmarshaler.Unmarshal(bytes.NewReader(data), tr.orig)
if err != nil {
return err
}
tr.instrumentationLibraryToScope()
return nil
}

// instrumentationLibraryToScope implements the translation of resource span data
// following the v0.15.0 upgrade:
// receivers SHOULD check if instrumentation_library_spans is set
// and scope_spans is not set then the value in instrumentation_library_spans
// SHOULD be used instead by converting InstrumentationLibrarySpans into ScopeSpans.
// If scope_spans is set then instrumentation_library_spans SHOULD be ignored.
func (tr TracesRequest) instrumentationLibraryToScope() {
for _, rs := range tr.orig.ResourceSpans {
if len(rs.ScopeSpans) == 0 {
for _, ils := range rs.InstrumentationLibrarySpans {
scopeSpans := otlptrace.ScopeSpans{
Scope: v1.InstrumentationScope{
Name: ils.InstrumentationLibrary.Name,
Version: ils.InstrumentationLibrary.Version,
},
Spans: ils.Spans,
SchemaUrl: ils.SchemaUrl,
}
rs.ScopeSpans = append(rs.ScopeSpans, &scopeSpans)
}
}
rs.InstrumentationLibrarySpans = nil
}
}

func (tr TracesRequest) SetTraces(td pdata.Traces) {
Expand Down
Loading

0 comments on commit 9f21939

Please sign in to comment.