...

Source file src/cloud.google.com/go/logging/instrumentation.go

Documentation: cloud.google.com/go/logging

     1  // Copyright 2022 Google LLC
     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  //      https://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 logging
    16  
    17  import (
    18  	"strings"
    19  
    20  	logpb "cloud.google.com/go/logging/apiv2/loggingpb"
    21  	"cloud.google.com/go/logging/internal"
    22  )
    23  
    24  const diagnosticLogID = "diagnostic-log"
    25  
    26  // instrumentationPayload defines telemetry log entry payload for capturing instrumentation info
    27  type instrumentationPayload struct {
    28  	InstrumentationSource []map[string]string `json:"instrumentation_source"`
    29  	Runtime               string              `json:"runtime,omitempty"`
    30  }
    31  
    32  var (
    33  	instrumentationInfo = &instrumentationPayload{
    34  		InstrumentationSource: []map[string]string{
    35  			{
    36  				"name":    "go",
    37  				"version": internal.Version,
    38  			},
    39  		},
    40  		Runtime: internal.VersionGo(),
    41  	}
    42  )
    43  
    44  // instrumentLogs appends log entry with library instrumentation info to the
    45  // list of log entries on the first function's call.
    46  func (l *Logger) instrumentLogs(entries []*logpb.LogEntry) ([]*logpb.LogEntry, bool) {
    47  	var instrumentationAdded bool
    48  
    49  	internal.InstrumentOnce.Do(func() {
    50  		ie, err := l.instrumentationEntry()
    51  		if err != nil {
    52  			// do not retry instrumenting logs if failed creating instrumentation entry
    53  			return
    54  		}
    55  		// populate LogName only when  directly ingesting entries
    56  		if l.redirectOutputWriter == nil {
    57  			ie.LogName = internal.LogPath(l.client.parent, diagnosticLogID)
    58  		}
    59  		entries = append(entries, ie)
    60  		instrumentationAdded = true
    61  	})
    62  	return entries, instrumentationAdded
    63  }
    64  
    65  func (l *Logger) instrumentationEntry() (*logpb.LogEntry, error) {
    66  	ent := Entry{
    67  		Payload: map[string]*instrumentationPayload{
    68  			"logging.googleapis.com/diagnostic": instrumentationInfo,
    69  		},
    70  	}
    71  	// pass nil for Logger and 0 for skip levels to ignore auto-population
    72  	return toLogEntryInternal(ent, nil, l.client.parent, 0)
    73  }
    74  
    75  // hasInstrumentation returns true if any of the log entries has diagnostic LogId
    76  func hasInstrumentation(entries []*logpb.LogEntry) bool {
    77  	for _, ent := range entries {
    78  		if strings.HasSuffix(ent.LogName, diagnosticLogID) {
    79  			return true
    80  		}
    81  	}
    82  	return false
    83  }
    84  

View as plain text