Skip to content

Commit

Permalink
gcp/observability: Generate unique process identifier unconditionally (
Browse files Browse the repository at this point in the history
  • Loading branch information
zasweq committed Mar 23, 2023
1 parent 1d20f1b commit 0fdfd40
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions gcp/observability/opencensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package observability

import (
"fmt"
"os"
"strconv"
"time"

"contrib.go.opencensus.io/exporter/stackdriver"
Expand Down Expand Up @@ -82,6 +84,11 @@ func newStackdriverExporter(config *config) (tracingMetricsExporter, error) {
mr := monitoredresource.Autodetect()
logger.Infof("Detected MonitoredResource:: %+v", mr)
var err error
// Custom labels completly overwrite any labels generated in the OpenCensus
// library, including their label that uniquely identifies the process.
// Thus, generate a unique process identifier here to uniquely identify
// process.
config.Labels["opencensus_task"] = generateUniqueProcessIdentifier()
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: config.ProjectID,
MonitoredResource: mr,
Expand All @@ -96,6 +103,18 @@ func newStackdriverExporter(config *config) (tracingMetricsExporter, error) {
return exporter, nil
}

// generateUniqueProcessIdentifier returns a unique process identifier for the
// process this code is running in. This is the same way the OpenCensus library
// generates the unique process identifier, in the format of
// "go-<pid>@<hostname>".
func generateUniqueProcessIdentifier() string {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
return "go-" + strconv.Itoa(os.Getpid()) + "@" + hostname
}

// This method accepts config and exporter; the exporter argument is exposed to
// assist unit testing of the OpenCensus behavior.
func startOpenCensus(config *config) error {
Expand Down

0 comments on commit 0fdfd40

Please sign in to comment.