2020-11-20 06:57:39 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-12-02 07:50:59 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing/otel"
|
2020-11-20 06:57:39 +00:00
|
|
|
sdk_trace "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ProjectID string
|
|
|
|
MetricPrefix string
|
|
|
|
Fraction float64
|
|
|
|
}
|
|
|
|
|
|
|
|
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.NewExporter(texporter.WithProjectID(c.ProjectID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing.T = &Tracer{Tracer: *(otel.NewTracer(c.MetricPrefix, sampler, exporter))}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func envIsSet() bool {
|
|
|
|
gAuthCred := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
|
|
|
|
return strings.Contains(gAuthCred, ".json")
|
|
|
|
}
|