2023-05-30 09:31:34 +00:00
|
|
|
package tracing
|
|
|
|
|
2024-09-04 11:42:22 +00:00
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
)
|
2023-05-30 09:31:34 +00:00
|
|
|
|
|
|
|
// Exporter is type of tracing target.
|
|
|
|
type Exporter string
|
|
|
|
|
|
|
|
const (
|
|
|
|
StdoutExporter Exporter = "stdout"
|
|
|
|
OTLPgRPCExporter Exporter = "otlp_grpc"
|
|
|
|
NoOpExporter Exporter = "noop"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
// Enabled is true, if tracing enabled.
|
|
|
|
Enabled bool
|
|
|
|
// Exporter is collector type.
|
|
|
|
Exporter Exporter
|
|
|
|
// Endpoint is collector endpoint for OTLP exporters.
|
|
|
|
Endpoint string
|
2024-09-04 11:42:22 +00:00
|
|
|
// ServerCaCertPool is cert pool of the remote server CA certificate. Use for TLS setup.
|
|
|
|
ServerCaCertPool *x509.CertPool
|
2023-05-30 09:31:34 +00:00
|
|
|
|
|
|
|
// Service is service name that will be used in tracing.
|
|
|
|
// Mandatory.
|
|
|
|
Service string
|
|
|
|
// InstanceID is identity of service instance.
|
|
|
|
// Optional.
|
|
|
|
InstanceID string
|
|
|
|
// Version is version of service instance.
|
|
|
|
// Optional.
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validate() error {
|
|
|
|
if !c.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Exporter != StdoutExporter && c.Exporter != OTLPgRPCExporter && c.Exporter != NoOpExporter {
|
|
|
|
return fmt.Errorf("tracing config error: unknown exporter '%s', valid values are %v",
|
|
|
|
c.Exporter, []string{string(StdoutExporter), string(OTLPgRPCExporter), string(NoOpExporter)})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Service) == 0 {
|
|
|
|
return fmt.Errorf("tracing config error: service name must be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Exporter == OTLPgRPCExporter && len(c.Endpoint) == 0 {
|
|
|
|
return fmt.Errorf("tracing config error: exporter '%s' requires endpoint", c.Exporter)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) hasChange(other *Config) bool {
|
|
|
|
if !c.Enabled && !other.Enabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Enabled != other.Enabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c.Exporter == StdoutExporter && other.Exporter == StdoutExporter) ||
|
|
|
|
(c.Exporter == NoOpExporter && other.Exporter == NoOpExporter) {
|
|
|
|
return !c.serviceInfoEqual(other)
|
|
|
|
}
|
|
|
|
|
2024-09-04 11:42:22 +00:00
|
|
|
if other.Exporter == OTLPgRPCExporter && !c.ServerCaCertPool.Equal(other.ServerCaCertPool) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-30 09:31:34 +00:00
|
|
|
return c.Exporter != other.Exporter ||
|
|
|
|
c.Endpoint != other.Endpoint ||
|
|
|
|
!c.serviceInfoEqual(other)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) serviceInfoEqual(other *Config) bool {
|
|
|
|
return c.Service == other.Service &&
|
|
|
|
c.InstanceID == other.InstanceID &&
|
|
|
|
c.Version == other.Version
|
|
|
|
}
|