go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/util/loggingutil/loggingutil.go (about)

     1  // Copyright 2022 The LUCI Authors.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package loggingutil contains utility functions for logging.
    16  package loggingutil
    17  
    18  import (
    19  	"context"
    20  
    21  	"go.chromium.org/luci/bisection/util/datastoreutil"
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/common/logging"
    24  )
    25  
    26  // UpdateLoggingWithAnalysisID returns a context with logging field analysis_id
    27  // and the corresponding analyzed_bbid set.
    28  func UpdateLoggingWithAnalysisID(c context.Context, analysisID int64) (context.Context, error) {
    29  	c = SetAnalysisID(c, analysisID)
    30  	cfa, err := datastoreutil.GetCompileFailureAnalysis(c, analysisID)
    31  	if err != nil {
    32  		return c, errors.Annotate(err, "failed GetCompileFailureAnalysis ID: %d", analysisID).Err()
    33  	}
    34  	if cfa.CompileFailure != nil && cfa.CompileFailure.Parent() != nil {
    35  		c = SetAnalyzedBBID(c, cfa.CompileFailure.Parent().IntID())
    36  	}
    37  	return c, nil
    38  }
    39  
    40  // SetAnalyzedBBID returns a context with the logging field analyzed_bbid set
    41  func SetAnalyzedBBID(c context.Context, bbid int64) context.Context {
    42  	return logging.SetField(c, "analyzed_bbid", bbid)
    43  }
    44  
    45  // SetAnalysisID returns a context with the logging field analysis_id set
    46  func SetAnalysisID(c context.Context, analysisID int64) context.Context {
    47  	return logging.SetField(c, "analysis_id", analysisID)
    48  }
    49  
    50  // SetRerunBBID returns a context with the logging field rerun_bbid set
    51  func SetRerunBBID(c context.Context, rerunBBID int64) context.Context {
    52  	return logging.SetField(c, "rerun_bbid", rerunBBID)
    53  }
    54  
    55  // SetQueryBBID returns a context with the logging field query_bbid set
    56  func SetQueryBBID(c context.Context, bbid int64) context.Context {
    57  	return logging.SetField(c, "query_bbid", bbid)
    58  }
    59  
    60  // SetProject returns a context with the logging field project set
    61  func SetProject(c context.Context, project string) context.Context {
    62  	return logging.SetField(c, "project", project)
    63  }