func StartOTel()

in pkg/metrics/otel.go [49:96]


func StartOTel(ctx context.Context, cfg common.Config) (*otelProvider, error) {
	// Prepare "monitored resource" attributes for Cloud Monitoring
	// (see https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/24420159f1b4bc16579462847eae74b00227b534/exporter/metric/metric.go#L351)
	// k8s_cluster requires location, cluster name
	// k8s_node requires location, cluster name, node name
	attrs := []attribute.KeyValue{
		attribute.String(mexporter.CloudKeyProvider, mexporter.CloudProviderGCP),
		attribute.String(mexporter.CloudKeyZone, cfg.Location), // this is called "zone" but represents location
		attribute.String(mexporter.K8SKeyClusterName, cfg.Cluster),
	}
	if cfg.NodeName != "" {
		attrs = append(attrs, attribute.String(mexporter.HostKeyName, cfg.NodeName))
	}

	popts := []controller.Option{
		controller.WithResource(resource.NewSchemaless(attrs...)),
	}

	// if os.Getenv("DEBUG") == "true" {
	// 	opts := []stdout.Option{
	// 		stdout.WithPrettyPrint(),
	// 	}
	// 	_, pusher, err = stdout.InstallNewPipeline(opts, popts)
	// 	return pusher, err
	// }

	formatter := func(d *sdkapi.Descriptor) string {
		return fmt.Sprintf("%s/%s", common.MetricPrefix, d.Name())
	}
	// Initialize exporter option.
	opts := []mexporter.Option{
		mexporter.WithProjectID(cfg.ProjectID),
		mexporter.WithInterval(cfg.ReportInterval),
		mexporter.WithMetricDescriptorTypeFormatter(formatter),
		mexporter.WithMonitoringClientOptions(
			option.WithUserAgent(cfg.UserAgent),
		),
	}
	pusher, err := mexporter.InstallNewPipeline(opts, popts...)
	if err != nil {
		go func() {
			<-ctx.Done()
			pusher.Stop(context.Background())
		}()
	}

	return &otelProvider{}, err
}