func()

in pkg/export/gcm/promtest/local_export.go [74:130]


func (l *localExportWithGCM) start(t testing.TB, _ e2e.Environment) (v1.API, map[string]string) {
	t.Helper()

	ctx, cancel := context.WithCancel(signals.SetupSignalHandler())
	t.Cleanup(cancel)

	creds, err := google.CredentialsFromJSON(ctx, l.gcmSA, gcm.DefaultAuthScopes()...)
	if err != nil {
		t.Fatalf("create credentials from JSON: %s", err)
	}

	l.labelsByRef = map[storage.SeriesRef]labels.Labels{}

	cluster := "pe-github-action"
	location := "europe-west3-a"

	cl, err := api.NewClient(api.Config{
		Address: fmt.Sprintf("https://monitoring.googleapis.com/v1/projects/%s/location/global/prometheus", creds.ProjectID),
		Client:  oauth2.NewClient(ctx, creds.TokenSource),
	})
	if err != nil {
		t.Fatalf("create Prometheus client: %s", err)
	}

	exporterOpts := export.ExporterOpts{
		UserAgentEnv:        "pe-github-action-test",
		Cluster:             cluster,
		Location:            location,
		ProjectID:           creds.ProjectID,
		CredentialsFromJSON: l.gcmSA,
	}
	exporterOpts.DefaultUnsetFields()
	l.e, err = export.New(ctx, log.NewJSONLogger(os.Stderr), nil, exporterOpts, export.NopLease())
	if err != nil {
		t.Fatalf("create exporter: %v", err)
	}

	// Apply empty config, so resources labels are attached.
	if err := l.e.ApplyConfig(&config.DefaultConfig, nil); err != nil {
		t.Fatalf("apply config: %v", err)
	}
	l.e.SetLabelsByIDFunc(func(ref storage.SeriesRef) labels.Labels {
		return l.labelsByRef[ref]
	})

	go func() {
		if err := l.e.Run(); err != nil {
			t.Logf("running exporter: %s", err)
		}
	}()

	return v1.NewAPI(cl), map[string]string{
		"cluster":    cluster,
		"location":   location,
		"project_id": creds.ProjectID,
	}
}