func()

in pkg/exporter/skywalking.go [66:128]


func (exporter *SkyWalking) Init(ctx context.Context) error {
	config := SkyWalkingConfig{}

	if c := configs.GlobalConfig.Exporters[exporter.Name()]; c == nil {
		return fmt.Errorf("configs of %+v exporter cannot be empty", exporter.Name())
	} else if marshal, err := json.Marshal(c); err != nil {
		return err
	} else if err := json.Unmarshal(marshal, &config); err != nil {
		return err
	}

	if err := config.Template.Init(); err != nil {
		return err
	}

	var dialOption grpc.DialOption
	if config.EnableTLS {
		if isFileExisted(config.ClientCertPath) && isFileExisted(config.ClientKeyPath) {
			clientCert, err := tls.LoadX509KeyPair(config.ClientCertPath, config.ClientKeyPath)
			if err != nil {
				return err
			}
			trustedCert, err := os.ReadFile(config.TrustedCertPath)
			if err != nil {
				return err
			}
			certPool := x509.NewCertPool()
			certPool.AppendCertsFromPEM(trustedCert)

			tlsConfig := &tls.Config{
				Certificates: []tls.Certificate{clientCert},
				RootCAs:      certPool,
				MinVersion:   tls.VersionTLS13,
				MaxVersion:   tls.VersionTLS13,
			}
			tlsConfig.InsecureSkipVerify = config.InsecureSkipVerify
			dialOption = grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
		} else {
			cred, _ := credentials.NewClientTLSFromFile(config.TrustedCertPath, "")
			dialOption = grpc.WithTransportCredentials(cred)
		}
	} else {
		dialOption = grpc.WithInsecure()
	}

	conn, err := grpc.Dial(config.Address, dialOption)
	if err != nil {
		return err
	}

	exporter.config = config
	exporter.client = sw.NewEventServiceClient(conn)

	go func() {
		<-ctx.Done()

		if err := conn.Close(); err != nil {
			logger.Log.Errorf("failed to close connection. %+v", err)
		}
	}()

	return nil
}