package main

import (
	"fmt"
	"time"

	"go.temporal.io/sdk/client"
)

const (
	activityLatency        = "activity_latency"
	scheduleToStartLatency = "schedule_to_start_latency"

	activityStartedCount = "activity_started"
	activityFailedCount  = "activity_failed"
	activitySuccessCount = "activity_succeeded"
)

func main() {

	// **********metrics handler TO FIND THE CPU USAGE*******

	handler := client.NewMetricsHandler()
	// Record the start of an activity.
	recordActivityStart(handler, "my-activity", time.Now().UnixNano())

	// Get the CPU stats.
	cpuStats := handler.GetCpuStats()

	// Print the CPU stats.
	fmt.Println("Total CPU cores:", cpuStats.TotalCores)
	fmt.Println("Number of CPU cores in use:", cpuStats.UsedCores)
	fmt.Println("Percentage of CPU in use:", cpuStats.Percentage)

	// Do some work.
	time.Sleep(time.Second)

	// Record the end of an activity.
	recordActivityEnd(handler, time.Now().UnixNano(), nil)

	// Close the metrics handler.
	handler.Close()

	// **********metrics handler TO FIND THE NEWTWORK TRAFFIC*******

	handler = client.NewMetricsHandler()
	// Record the start of an activity.
	recordActivityStart(handler, "my-activity", time.Now().UnixNano())

	// Get the network stats.
	networkStats := handler.GetNetworkStats()

	// Print the network stats.
	fmt.Println("Total network traffic:", networkStats.TotalBytes)
	fmt.Println("Inbound network traffic:", networkStats.InboundBytes)
	fmt.Println("Outbound network traffic:", networkStats.OutboundBytes)

	// Do some work.
	time.Sleep(time.Second)

	// Record the end of an activity.
	recordActivityEnd(handler, time.Now().UnixNano(), nil)

	// Close the metrics handler.
	handler.Close()

	// **********metrics handler TO FIND  THE NUMBER OF ERRORS*******

	handler = client.NewMetricsHandler()

	// Record the start of an activity.
	recordActivityStart(handler, "my-activity", time.Now().UnixNano())

	err := MakeSomefunction() //------------------

	// Record the end of an activity.
	recordActivityEnd(handler, time.Now().UnixNano(), err)

	// Close the metrics handler.
	handler.Close()

	// Get the number of errors.
	numberOfErrors := handler.GetCounter(activityFailedCount).Value()

	// Print the number of errors.
	fmt.Println("Number of errors:", numberOfErrors)

}

func recordActivityStart(
	handler client.MetricsHandler,
	activityType string,
	scheduledTimeNanos int64,
) client.MetricsHandler {
	handler = handler.WithTags(map[string]string{"operation": activityType})
	scheduleToStartDuration := time.Duration(time.Now().UnixNano() - scheduledTimeNanos)
	handler.Timer(scheduleToStartLatency).Record(scheduleToStartDuration)
	handler.Counter(activityStartedCount).Inc(1)
	return handler
}

// recordActivityEnd emits metrics at the end of an activity function
func recordActivityEnd(handler client.MetricsHandler, startTime time.Time, err error) {
	handler.Timer(activityLatency).Record(time.Since(startTime))
	if err != nil {
		handler.Counter(activityFailedCount).Inc(1)
		return
	}
	handler.Counter(activitySuccessCount).Inc(1)
}
Powered by Codespace