github.com/observiq/bindplane-agent@v1.51.0/internal/report/snapshot/filter_traces_test.go (about)

     1  // Copyright observIQ, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package snapshot
    16  
    17  import (
    18  	"path/filepath"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  var unfilteredTracesDir = filepath.Join("testdata", "traces", "before")
    27  var filteredTracesDir = filepath.Join("testdata", "traces", "after")
    28  
    29  func TestFilterTraces(t *testing.T) {
    30  	testCases := []struct {
    31  		name             string
    32  		fileIn           string
    33  		searchQuery      *string
    34  		minimumTimestamp *time.Time
    35  		expectedFileOut  string
    36  	}{
    37  		{
    38  			name:            "Query matches resource attribute",
    39  			fileIn:          filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    40  			searchQuery:     asPtr("Sams-M1-Pro.local"),
    41  			expectedFileOut: filepath.Join(filteredTracesDir, "matches-resource.yaml"),
    42  		},
    43  
    44  		{
    45  			name:            "No filters",
    46  			fileIn:          filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    47  			expectedFileOut: filepath.Join(filteredTracesDir, "no-filters.yaml"),
    48  		},
    49  
    50  		{
    51  			name:            "Filters pgstore/pgResourceInternal, no timestamp",
    52  			fileIn:          filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    53  			searchQuery:     asPtr("pgstore/pgResourceInternal"),
    54  			expectedFileOut: filepath.Join(filteredTracesDir, "filters-resource-internal.yaml"),
    55  		},
    56  		{
    57  			name:             "Filters pgstore/pgResourceInternal before timestamp",
    58  			fileIn:           filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    59  			searchQuery:      asPtr("pgstore/pgResourceInternal"),
    60  			minimumTimestamp: asPtr(time.Unix(0, 1706791445370893000)),
    61  			expectedFileOut:  filepath.Join(filteredTracesDir, "filters-resource-internal-time.yaml"),
    62  		},
    63  		{
    64  			name:            "Filters parent span id",
    65  			fileIn:          filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    66  			searchQuery:     asPtr("aa50d71d28f47370"),
    67  			expectedFileOut: filepath.Join(filteredTracesDir, "filters-parent-span-id.yaml"),
    68  		},
    69  		{
    70  			name:            "Filters span id",
    71  			fileIn:          filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    72  			searchQuery:     asPtr("0f3367e6b090ffed"),
    73  			expectedFileOut: filepath.Join(filteredTracesDir, "filters-span-id.yaml"),
    74  		},
    75  		{
    76  			name:            "Filters kind",
    77  			fileIn:          filepath.Join(unfilteredTracesDir, "bindplane-traces.yaml"),
    78  			searchQuery:     asPtr("Server"),
    79  			expectedFileOut: filepath.Join(filteredTracesDir, "filters-kind-id.yaml"),
    80  		},
    81  	}
    82  
    83  	for _, tc := range testCases {
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			tracesIn, err := golden.ReadTraces(tc.fileIn)
    86  			require.NoError(t, err)
    87  
    88  			tracesOut := filterTraces(tracesIn, tc.searchQuery, tc.minimumTimestamp)
    89  
    90  			expectedTracesOut, err := golden.ReadTraces(tc.expectedFileOut)
    91  			require.NoError(t, err)
    92  			require.Equal(t, expectedTracesOut, tracesOut)
    93  		})
    94  	}
    95  }