func NewPixieMetricProvider(vizierClient *pxapi.VizierClient, k8sClient dynamic.Interface, mapper apimeta.RESTMapper) provider.CustomMetricsProvider {
	provider := &pixieMetricsProvider{
		vizierClient:    vizierClient,
		client:          k8sClient,
		mapper:          mapper,
		podRequestsPerS: make(map[string]float64),
	}
	go provider.runMetricsLoop()
	return provider
}
NewPixieMetricProvider returns an instance of the Pixie metrics provider.
package main
import (
	"context"
	"fmt"
	"log"
	"sync"
	"time"
	apierr "k8s.io/apimachinery/pkg/api/errors"
	apimeta "k8s.io/apimachinery/pkg/api/meta"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/dynamic"
	"k8s.io/metrics/pkg/apis/custom_metrics"
	"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
	"sigs.k8s.io/custom-metrics-apiserver/pkg/provider/helpers"
	"px.dev/pxapi"
	pxTypes "px.dev/pxapi/types"
)
const httpRpsMetricName = "px-http-requests-per-second"
const pxlScript = `import px
# Get list of pods (even non-HTTP)
df = px.DataFrame(table='process_stats', start_time='-30s')
df.pod = df.ctx['pod']
pods_list = df.groupby('pod').agg()
# Get HTTP events (not all pods will have this)
df = px.DataFrame(table='http_events', start_time='-30s')
df.pod = df.ctx['pod']
df = df.groupby('pod').agg(
    requests=('latency', px.count)
)
df.rps = df.requests / 30;
df = pods_list.merge(df, how='left', left_on='pod', right_on='pod', suffixes=['', '_x'])
px.display(df[['pod', 'rps']], 'pod_stats')`
type pixieMetricsProvider struct {
	vizierClient    *pxapi.VizierClient
	client          dynamic.Interface
	mapper          apimeta.RESTMapper
	dataMux         sync.Mutex
	podRequestsPerS map[string]float64
}
func (p *pixieMetricsProvider) computeMetrics(ctx context.Context) {
	tm := &tableMux{
		onPodStatsComplete: func(newStats map[string]float64) {
			p.dataMux.Lock()
			defer p.dataMux.Unlock()
			p.podRequestsPerS = newStats
		},
	}
	log.Println("Refreshing Pixie metrics.")
	results, err := p.vizierClient.ExecuteScript(ctx, pxlScript, tm)
	if err != nil {
		log.Printf("Error executing PxL script: %s\n", err.Error())
	}
	if err = results.Stream(); err != nil {
		log.Printf("Error executing PxL script: %s\n", err.Error())
	}
}
func (p *pixieMetricsProvider) runMetricsLoop() {
	ctx := context.Background()
	for {
		p.computeMetrics(ctx)
		<-time.After(30 * time.Second)
	}
}