github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/logutil/util.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package logutil
    15  
    16  import (
    17  	frameModel "github.com/pingcap/tiflow/engine/framework/model"
    18  	"github.com/pingcap/tiflow/engine/pkg/tenant"
    19  	"go.uber.org/zap"
    20  )
    21  
    22  const (
    23  	/// app const label
    24  	// constFieldTenantKey and constFieldProjectKey is used to recognize metric for tenant/project
    25  	constFieldTenantKey  = "tenant"
    26  	constFieldProjectKey = "project_id"
    27  	// ConstFieldJobKey is used to recognize jobs of the same job type
    28  	ConstFieldJobKey = "job_id"
    29  	// ConstFieldWorkerKey is used to recognize workers of the same job
    30  	ConstFieldWorkerKey = "worker_id"
    31  )
    32  
    33  // WithProjectInfo attaches project info to logger
    34  func WithProjectInfo(logger *zap.Logger, project tenant.ProjectInfo) *zap.Logger {
    35  	return logger.With(
    36  		zap.String(constFieldTenantKey, project.TenantID()),
    37  		zap.String(constFieldProjectKey, project.ProjectID()),
    38  	)
    39  }
    40  
    41  // WithMasterID attaches master id to logger
    42  func WithMasterID(logger *zap.Logger, masterID frameModel.MasterID) *zap.Logger {
    43  	return logger.With(
    44  		zap.String(ConstFieldJobKey, masterID),
    45  	)
    46  }
    47  
    48  // WithWorkerID attaches worker id to logger
    49  func WithWorkerID(logger *zap.Logger, workerID frameModel.WorkerID) *zap.Logger {
    50  	return logger.With(
    51  		zap.String(ConstFieldWorkerKey, workerID),
    52  	)
    53  }