forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
123 lines (104 loc) · 4.12 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package coralogixexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/coralogixexporter"
import (
"fmt"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/pcommon"
)
const (
cxAppNameAttrName = "cx.application.name"
cxSubsystemNameAttrName = "cx.subsystem.name"
)
// Config defines by Coralogix.
type Config struct {
exporterhelper.QueueSettings `mapstructure:"sending_queue"`
configretry.BackOffConfig `mapstructure:"retry_on_failure"`
exporterhelper.TimeoutSettings `mapstructure:",squash"`
// Coralogix domain
Domain string `mapstructure:"domain"`
// GRPC Settings used with Domain
DomainSettings configgrpc.ClientConfig `mapstructure:"domain_settings"`
// Deprecated: [v0.60.0] Coralogix jaeger based trace endpoint
// will be removed in the next version
// Please use OTLP endpoint using traces.endpoint
configgrpc.ClientConfig `mapstructure:",squash"`
// Coralogix traces ingress endpoint
Traces configgrpc.ClientConfig `mapstructure:"traces"`
// The Coralogix metrics ingress endpoint
Metrics configgrpc.ClientConfig `mapstructure:"metrics"`
// The Coralogix logs ingress endpoint
Logs configgrpc.ClientConfig `mapstructure:"logs"`
// Your Coralogix private key (sensitive) for authentication
PrivateKey configopaque.String `mapstructure:"private_key"`
// Ordered list of Resource attributes that are used for Coralogix
// AppName and SubSystem values. The first non-empty Resource attribute is used.
// Example: AppNameAttributes: ["k8s.namespace.name", "service.namespace"]
// Example: SubSystemAttributes: ["k8s.deployment.name", "k8s.daemonset.name", "service.name"]
AppNameAttributes []string `mapstructure:"application_name_attributes"`
SubSystemAttributes []string `mapstructure:"subsystem_name_attributes"`
// Default Coralogix application and subsystem name values.
AppName string `mapstructure:"application_name"`
SubSystem string `mapstructure:"subsystem_name"`
}
func isEmpty(endpoint string) bool {
if endpoint == "" || endpoint == "https://" || endpoint == "http:https://" {
return true
}
return false
}
func (c *Config) Validate() error {
// validate that at least one endpoint is set up correctly
if isEmpty(c.Domain) &&
isEmpty(c.Traces.Endpoint) &&
isEmpty(c.Metrics.Endpoint) &&
isEmpty(c.Logs.Endpoint) {
return fmt.Errorf("`domain` or `traces.endpoint` or `metrics.endpoint` or `logs.endpoint` not specified, please fix the configuration")
}
if c.PrivateKey == "" {
return fmt.Errorf("`private_key` not specified, please fix the configuration")
}
if c.AppName == "" {
return fmt.Errorf("`application_name` not specified, please fix the configuration")
}
// check if headers exists
if len(c.ClientConfig.Headers) == 0 {
c.ClientConfig.Headers = make(map[string]configopaque.String)
}
c.ClientConfig.Headers["ACCESS_TOKEN"] = c.PrivateKey
c.ClientConfig.Headers["appName"] = configopaque.String(c.AppName)
return nil
}
func (c *Config) getMetadataFromResource(res pcommon.Resource) (appName, subsystem string) {
// Example application name attributes: service.namespace, k8s.namespace.name
for _, appNameAttribute := range c.AppNameAttributes {
attr, ok := res.Attributes().Get(appNameAttribute)
if ok && attr.AsString() != "" {
appName = attr.AsString()
break
}
}
// Example subsystem name attributes: service.name, k8s.deployment.name, k8s.statefulset.name
for _, subSystemNameAttribute := range c.SubSystemAttributes {
attr, ok := res.Attributes().Get(subSystemNameAttribute)
if ok && attr.AsString() != "" {
subsystem = attr.AsString()
break
}
}
if appName == "" {
appName = c.AppName
}
if subsystem == "" {
subsystem = c.SubSystem
}
return appName, subsystem
}
func (c *Config) getDomainGrpcSettings() *configgrpc.ClientConfig {
settings := c.DomainSettings
settings.Endpoint = fmt.Sprintf("ingress.%s:443", c.Domain)
return &settings
}