github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/ingest/ingestionStats.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ingest
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/siglens/siglens/pkg/instrumentation"
    23  	segwriter "github.com/siglens/siglens/pkg/segment/writer"
    24  	vtable "github.com/siglens/siglens/pkg/virtualtable"
    25  
    26  	log "github.com/sirupsen/logrus"
    27  )
    28  
    29  func InitIngestionMetrics() {
    30  	go ingestionMetricsLooper()
    31  }
    32  
    33  func ingestionMetricsLooper() {
    34  	for {
    35  		time.Sleep(1 * time.Minute)
    36  
    37  		currentEventCount := int64(0)
    38  		currentBytesReceived := int64(0)
    39  		currentOnDiskBytes := int64(0)
    40  
    41  		// change to loop for all orgs
    42  		allVirtualTableNames, err := vtable.GetVirtualTableNames(0)
    43  
    44  		if err != nil {
    45  			log.Errorf("ingestionMetricsLooper: Error in getting virtual table names, err:%v", err)
    46  		}
    47  		for indexName := range allVirtualTableNames {
    48  			if indexName == "" {
    49  				log.Errorf("ingestionMetricsLooper: skipping an empty index name indexName=%v", indexName)
    50  				continue
    51  			}
    52  			byteCount, eventCount, onDiskBytes := segwriter.GetVTableCounts(indexName, 0)
    53  			unrotatedByteCount, unrotatedEventCount, unrotatedOnDiskBytes := segwriter.GetUnrotatedVTableCounts(indexName, 0)
    54  
    55  			totalEventsForIndex := uint64(eventCount) + uint64(unrotatedEventCount)
    56  			currentEventCount += int64(totalEventsForIndex)
    57  			instrumentation.SetEventCountPerIndex(currentEventCount, "indexname", indexName)
    58  
    59  			totalBytesReceivedForIndex := byteCount + unrotatedByteCount
    60  			currentBytesReceived += int64(totalBytesReceivedForIndex)
    61  			instrumentation.SetBytesCountPerIndex(currentBytesReceived, "indexname", indexName)
    62  
    63  			totalOnDiskBytesForIndex := onDiskBytes + unrotatedOnDiskBytes
    64  			currentOnDiskBytes += int64(totalOnDiskBytesForIndex)
    65  			instrumentation.SetOnDiskBytesPerIndex(currentOnDiskBytes, "indexname", indexName)
    66  
    67  		}
    68  		instrumentation.SetGaugeCurrentEventCount(currentEventCount)
    69  		instrumentation.SetGaugeCurrentBytesReceivedGauge(currentBytesReceived)
    70  		instrumentation.SetGaugeOnDiskBytesGauge(currentOnDiskBytes)
    71  	}
    72  }