Skip to content

Commit

Permalink
Merge pull request #439 from sophieliu15/metrics_fix_1
Browse files Browse the repository at this point in the history
Fix stackdriver_sink_successfully_sent_entry_count.
  • Loading branch information
sophieliu15 authored Oct 4, 2021
2 parents 8db3ffc + 7e2fd14 commit e4144a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
11 changes: 1 addition & 10 deletions event-exporter/sinks/stackdriver/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ var (
Subsystem: "stackdriver_sink",
},
)

successfullySentEntryCount = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "successfully_sent_entry_count",
Help: "Number of entries successfully ingested by Stackdriver",
Subsystem: "stackdriver_sink",
},
)
)

// sdSink satisfies sinks.Sink interface.
Expand Down Expand Up @@ -165,8 +157,7 @@ func (s *sdSink) flushBuffer() {
func (s *sdSink) sendEntries(entries []*sd.LogEntry) {
glog.V(4).Infof("Sending %d entries to Stackdriver", len(entries))

written := s.writer.Write(entries, s.logName, s.sdResourceFactory.defaultResource)
successfullySentEntryCount.Add(float64(written))
s.writer.Write(entries, s.logName, s.sdResourceFactory.defaultResource)

<-s.concurrencyChannel

Expand Down
16 changes: 12 additions & 4 deletions event-exporter/sinks/stackdriver/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ var (
},
[]string{"code"},
)

successfullySentEntryCount = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "successfully_sent_entry_count",
Help: "Number of entries successfully ingested by Stackdriver",
Subsystem: "stackdriver_sink",
},
)
)

type sdWriter interface {
Write([]*sd.LogEntry, string, *sd.MonitoredResource) int
Write([]*sd.LogEntry, string, *sd.MonitoredResource)
}

type sdWriterImpl struct {
Expand All @@ -58,7 +66,7 @@ func newSdWriter(service *sd.Service) sdWriter {

// Writer writes log entries to Stackdriver. It retries writing logs forever
// unless the API returns BadRequest error.
func (w sdWriterImpl) Write(entries []*sd.LogEntry, logName string, resource *sd.MonitoredResource) int {
func (w sdWriterImpl) Write(entries []*sd.LogEntry, logName string, resource *sd.MonitoredResource) {
req := &sd.WriteLogEntriesRequest{
Entries: entries,
LogName: logName,
Expand All @@ -72,8 +80,10 @@ func (w sdWriterImpl) Write(entries []*sd.LogEntry, logName string, resource *sd
for {
res, err := w.service.Entries.Write(req).Do()

// The entry is successfully sent to Stackdriver.
if err == nil {
requestCount.WithLabelValues(strconv.Itoa(res.HTTPStatusCode)).Inc()
successfullySentEntryCount.Add(float64(len(entries)))
break
}

Expand All @@ -96,6 +106,4 @@ func (w sdWriterImpl) Write(entries []*sd.LogEntry, logName string, resource *sd
glog.Warningf("Failed to send request to Stackdriver: %v", err)
time.Sleep(retryDelay)
}

return len(entries)
}

0 comments on commit e4144a8

Please sign in to comment.