github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/snapshot.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package instana
     5  
     6  import (
     7  	"runtime"
     8  	"sync"
     9  	"time"
    10  
    11  	"github.com/instana/go-sensor/acceptor"
    12  )
    13  
    14  // SnapshotCollector returns a snapshot of Go runtime
    15  type SnapshotCollector struct {
    16  	ServiceName        string
    17  	CollectionInterval time.Duration
    18  
    19  	mu                 sync.RWMutex
    20  	lastCollectionTime time.Time
    21  }
    22  
    23  // Collect returns a snaphot of current runtime state. Any call this
    24  // method made before the next interval elapses will return nil
    25  func (sc *SnapshotCollector) Collect() *acceptor.RuntimeInfo {
    26  	sc.mu.RLock()
    27  	lastSnapshotCollectionTime := sc.lastCollectionTime
    28  	sc.mu.RUnlock()
    29  
    30  	if time.Since(lastSnapshotCollectionTime) < sc.CollectionInterval {
    31  		return nil
    32  	}
    33  
    34  	sc.mu.Lock()
    35  	defer sc.mu.Unlock()
    36  
    37  	sc.lastCollectionTime = time.Now()
    38  
    39  	return &acceptor.RuntimeInfo{
    40  		Name:          sc.ServiceName,
    41  		Version:       runtime.Version(),
    42  		Root:          runtime.GOROOT(),
    43  		MaxProcs:      runtime.GOMAXPROCS(0),
    44  		Compiler:      runtime.Compiler,
    45  		NumCPU:        runtime.NumCPU(),
    46  		SensorVersion: Version,
    47  	}
    48  }