From f13a23eab7d52811675251e6190dad1b90472e0b Mon Sep 17 00:00:00 2001 From: April Ayres <127258092+ajayreshc@users.noreply.github.com> Date: Fri, 22 Sep 2023 14:15:23 -0700 Subject: [PATCH] fix (telemetry): ignore fields without public class --- internal/event/telemetry_filter.go | 2 +- internal/event/telemetry_filter_test.go | 35 ++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/internal/event/telemetry_filter.go b/internal/event/telemetry_filter.go index 2c74af5f1..d82927b48 100644 --- a/internal/event/telemetry_filter.go +++ b/internal/event/telemetry_filter.go @@ -30,7 +30,7 @@ type protoFilter func(field reflect.StructField) bool // telemetryFilter checks a struct field should be included in observation telemetry data func telemetryFilter(field reflect.StructField) bool { - if field.Tag.Get("eventstream") == "observation" { + if field.Tag.Get("eventstream") == "observation" && field.Tag.Get("class") == "public" { return true } return false diff --git a/internal/event/telemetry_filter_test.go b/internal/event/telemetry_filter_test.go index 3148ec4ce..89ed597f9 100644 --- a/internal/event/telemetry_filter_test.go +++ b/internal/event/telemetry_filter_test.go @@ -115,7 +115,7 @@ func Test_scalarSliceFilterAllZeroVals(t *testing.T) { assert := tassert.New(t) type testType struct { NonObservableStrings []string - ObservableInts []int `eventstream:"observation"` + ObservableInts []int `eventstream:"observation" class:"public"` } data := &testType{ NonObservableStrings: []string{"a", "b", "c"}, @@ -131,7 +131,7 @@ func Test_mapFilter(t *testing.T) { assert := tassert.New(t) type testType struct { NonObservableMap map[string]int - ObservableMap map[string]int `eventstream:"observation"` + ObservableMap map[string]int `eventstream:"observation" class:"public"` } data := &testType{ NonObservableMap: map[string]int{ @@ -160,11 +160,11 @@ func Test_coreProtoTypes(t *testing.T) { assert := tassert.New(t) type testType struct { Timestamp *timestamppb.Timestamp - TimestampObs *timestamppb.Timestamp `eventstream:"observation"` + TimestampObs *timestamppb.Timestamp `eventstream:"observation" class:"public"` WrappedString *wrapperspb.StringValue - WrappedStringObs *wrapperspb.StringValue `eventstream:"observation"` + WrappedStringObs *wrapperspb.StringValue `eventstream:"observation" class:"public"` Fieldmask *fieldmaskpb.FieldMask - FieldmaskObs *fieldmaskpb.FieldMask `eventstream:"observation"` + FieldmaskObs *fieldmaskpb.FieldMask `eventstream:"observation" class:"public"` } data := &testType{ Timestamp: ×tamppb.Timestamp{ @@ -206,7 +206,7 @@ func Test_mapStructPBValues(t *testing.T) { assert := tassert.New(t) type testType struct { ListValueMap map[string]*structpb.ListValue - ListValueMapObs map[string]*structpb.ListValue `eventstream:"observation"` + ListValueMapObs map[string]*structpb.ListValue `eventstream:"observation" class:"public"` } data := &testType{ ListValueMap: map[string]*structpb.ListValue{ @@ -256,7 +256,7 @@ func Test_sliceStructPBValues(t *testing.T) { assert := tassert.New(t) type testType struct { ListValueSlice []*structpb.ListValue - ListValueSliceObs []*structpb.ListValue `eventstream:"observation"` + ListValueSliceObs []*structpb.ListValue `eventstream:"observation" class:"public"` ListValueArray [2]*structpb.ListValue } data := &testType{ @@ -313,3 +313,24 @@ func Test_sliceStructPBValues(t *testing.T) { assert.Nil(data.ListValueArray[0]) assert.Nil(data.ListValueArray[1]) } + +func Test_onlyPublicTelemetryFields(t *testing.T) { + assert := tassert.New(t) + type testType struct { + NonObservableString string + NonPublicObservableString string `eventstream:"observation"` + PublicObservableString string `eventstream:"observation" class:"public"` + SecretObservableString string `eventstream:"observation" class:"secret"` + } + data := &testType{ + NonObservableString: "empty", + NonPublicObservableString: "empty", + PublicObservableString: "observable", + SecretObservableString: "empty", + } + err := recurseStructureWithProtoFilter(reflect.ValueOf(data), telemetryFilter, false) + assert.NoError(err) + assert.Equal(&testType{ + PublicObservableString: "observable", + }, data) +}