feat: enable tracing (#3528)

This commit is contained in:
Livio Amstutz
2022-04-28 14:44:13 +02:00
committed by GitHub
parent 08ae39ae19
commit 44a2b81bef
6 changed files with 76 additions and 70 deletions

View File

@@ -1,8 +1,7 @@
package google
import (
"os"
"strings"
"strconv"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
@@ -18,15 +17,26 @@ type Config struct {
Fraction float64
}
func NewTracer(rawConfig map[string]interface{}) (err error) {
c := new(Config)
c.ProjectID, _ = rawConfig["projectid"].(string)
c.MetricPrefix, _ = rawConfig["metricprefix"].(string)
fraction, ok := rawConfig["fraction"].(string)
if ok {
c.Fraction, err = strconv.ParseFloat(fraction, 32)
if err != nil {
return errors.ThrowInternal(err, "GOOGLE-Dsag3", "could not map fraction")
}
}
return c.NewTracer()
}
type Tracer struct {
otel.Tracer
}
func (c *Config) NewTracer() error {
if !envIsSet() {
return errors.ThrowInvalidArgument(nil, "GOOGL-sdh3a", "env not properly set, GOOGLE_APPLICATION_CREDENTIALS is misconfigured or missing")
}
sampler := sdk_trace.ParentBased(sdk_trace.TraceIDRatioBased(c.Fraction))
exporter, err := texporter.New(texporter.WithProjectID(c.ProjectID))
if err != nil {
@@ -37,8 +47,3 @@ func (c *Config) NewTracer() error {
return nil
}
func envIsSet() bool {
gAuthCred := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
return strings.Contains(gAuthCred, ".json")
}