Skip to content

Commit 63ca8d9

Browse files
Vikash Prem SharmaVikash Prem Sharma
authored andcommitted
feat: expand support for both HTTP and gRPC protocols in meter/logging/tracing exporters
1 parent 09ab66e commit 63ca8d9

8 files changed

Lines changed: 161 additions & 73 deletions

File tree

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type (
9595
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
9696
URLPath string `mapstructure:"path"` // Path for the log exporter, if not defined /v1/logs will be used
9797
Headers []string `mapstructure:"headers"`
98+
Protocol string `mapstructure:"protocol"` // Protocol for the log exporter, e.g., http, grpc
9899
}
99100

100101
// Tracer contains configuration for distributed tracing.
@@ -105,6 +106,7 @@ type (
105106
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
106107
URLPath string `mapstructure:"path"` // Path for the tracing exporter, if not defined /v1/trace will be used
107108
Headers []string `mapstructure:"headers"`
109+
Protocol string `mapstructure:"protocol"` // Protocol for the tracing exporter, e.g., http, grpc
108110
}
109111

110112
// Meter contains configuration for metrics collection and reporting.
@@ -116,6 +118,7 @@ type (
116118
URLPath string `mapstructure:"path"` // Path for the metrics exporter, if not defined /v1/metrics will be used
117119
Headers []string `mapstructure:"headers"`
118120
Interval int `mapstructure:"interval"`
121+
Protocol string `mapstructure:"protocol"` // Protocol for the metrics exporter, e.g., http, grpc
119122
}
120123

121124
// Service contains configuration for various service-level features.

pkg/cmd/serve.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
200200
cfg.Log.Insecure,
201201
cfg.Log.URLPath,
202202
headers,
203+
cfg.Log.Protocol,
203204
)
204205
if err != nil {
205206
return errors.New("invalid logger exporter")
@@ -293,6 +294,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
293294
cfg.Tracer.Insecure,
294295
cfg.Tracer.URLPath,
295296
headers,
297+
cfg.Tracer.Protocol,
296298
)
297299
if err != nil {
298300
slog.Error(err.Error())
@@ -344,6 +346,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
344346
cfg.Meter.Insecure,
345347
cfg.Meter.URLPath,
346348
headers,
349+
cfg.Meter.Protocol,
347350
)
348351

349352
if err != nil {

pkg/telemetry/logexporters/factory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
)
88

99
// ExporterFactory - Create log exporter according to given params
10-
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string) (*otlplogs.Exporter, error) {
10+
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (*otlplogs.Exporter, error) {
1111
switch name {
1212
case "otlp", "otlp-http":
13-
return NewOTLP(endpoint, insecure, urlpath, headers)
13+
return NewOTLP(endpoint, insecure, urlpath, headers, "http")
1414
case "otlp-grpc":
15-
return nil, fmt.Errorf("%s log exporter is unsupported", name)
15+
return NewOTLP(endpoint, insecure, urlpath, headers, "grpc")
1616
default:
1717
return nil, fmt.Errorf("%s log exporter is unsupported", name)
1818
}

pkg/telemetry/logexporters/otlp.go

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,58 @@ package logexporters
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs"
78
"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogshttp"
9+
"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogsgrpc"
810
)
911

10-
// NewOTLP - Creates new OTLP exporter using HTTP protocol.
11-
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (*otlplogs.Exporter, error) {
12-
options := []otlplogshttp.Option{
13-
otlplogshttp.WithCompression(otlplogshttp.GzipCompression),
14-
otlplogshttp.WithEndpoint(endpoint),
15-
}
12+
// NewOTLP - Creates new OTLP exporter based on protocol.
13+
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (*otlplogs.Exporter, error) {
14+
switch protocol {
15+
case "http":
16+
options := []otlplogshttp.Option{
17+
otlplogshttp.WithCompression(otlplogshttp.GzipCompression),
18+
otlplogshttp.WithEndpoint(endpoint),
19+
}
1620

17-
if urlpath != "" {
18-
options = append(options, otlplogshttp.WithURLPath(urlpath))
19-
}
21+
if urlpath != "" {
22+
options = append(options, otlplogshttp.WithURLPath(urlpath))
23+
}
2024

21-
if insecure {
22-
options = append(options, otlplogshttp.WithInsecure())
23-
}
25+
if insecure {
26+
options = append(options, otlplogshttp.WithInsecure())
27+
}
2428

25-
exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
26-
otlplogshttp.NewClient(options...),
27-
))
28-
if err != nil {
29-
return nil, err
30-
}
29+
exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
30+
otlplogshttp.NewClient(options...),
31+
))
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
return exporter, nil
3137

32-
return exporter, nil
38+
case "grpc":
39+
options := []otlplogsgrpc.Option{
40+
otlplogsgrpc.WithEndpoint(endpoint),
41+
}
42+
43+
if insecure {
44+
options = append(options, otlplogsgrpc.WithInsecure())
45+
}
46+
47+
exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
48+
otlplogsgrpc.NewClient(options...),
49+
))
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
return exporter, nil
55+
56+
default:
57+
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
58+
}
3359
}

pkg/telemetry/meterexporters/factory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
)
88

99
// ExporterFactory - Create meter exporter according to given params
10-
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
10+
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (metric.Exporter, error) {
1111
switch name {
1212
case "otlp", "otlp-http":
13-
return NewOTLP(endpoint, insecure, urlpath, headers)
13+
return NewOTLP(endpoint, insecure, urlpath, headers, "http")
1414
case "otlp-grpc":
15-
return NewOTLPGrpc(endpoint, insecure, headers)
15+
return NewOTLP(endpoint, insecure, urlpath, headers, "grpc")
1616
default:
1717
return nil, fmt.Errorf("%s meter exporter is unsupported", name)
1818
}

pkg/telemetry/meterexporters/otlp.go

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,62 @@ package meterexporters
22

33
import (
44
"context"
5+
"fmt"
56

67
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
8+
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
79
"go.opentelemetry.io/otel/sdk/metric"
10+
"google.golang.org/grpc/credentials"
811
)
912

10-
// NewOTLP - Creates new OTLP exporter using HTTP protocol.
11-
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
12-
options := []otlpmetrichttp.Option{
13-
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
14-
otlpmetrichttp.WithEndpoint(endpoint),
13+
// NewOTLP - Creates new OTLP exporter based on protocol.
14+
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (metric.Exporter, error) {
15+
switch protocol {
16+
case "http":
17+
options := []otlpmetrichttp.Option{
18+
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
19+
otlpmetrichttp.WithEndpoint(endpoint),
20+
}
21+
22+
if len(headers) > 0 {
23+
options = append(options, otlpmetrichttp.WithHeaders(headers))
24+
}
25+
26+
if urlpath != "" {
27+
options = append(options, otlpmetrichttp.WithURLPath(urlpath))
28+
}
29+
30+
if insecure {
31+
options = append(options, otlpmetrichttp.WithInsecure())
32+
}
33+
34+
exporter, err := otlpmetrichttp.New(context.Background(), options...)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
return exporter, nil
40+
41+
case "grpc":
42+
options := []otlpmetricgrpc.Option{
43+
otlpmetricgrpc.WithEndpoint(endpoint),
44+
otlpmetricgrpc.WithHeaders(headers),
45+
}
46+
47+
if insecure {
48+
options = append(options, otlpmetricgrpc.WithInsecure())
49+
} else {
50+
options = append(options, otlpmetricgrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")))
51+
}
52+
53+
exporter, err := otlpmetricgrpc.New(context.Background(), options...)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
return exporter, nil
59+
60+
default:
61+
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
1562
}
16-
17-
if len(headers) > 0 {
18-
options = append(options, otlpmetrichttp.WithHeaders(headers))
19-
}
20-
21-
if urlpath != "" {
22-
options = append(options, otlpmetrichttp.WithURLPath(urlpath))
23-
}
24-
25-
if insecure {
26-
options = append(options, otlpmetrichttp.WithInsecure())
27-
}
28-
29-
exporter, err := otlpmetrichttp.New(context.Background(), options...)
30-
if err != nil {
31-
return nil, err
32-
}
33-
34-
return exporter, nil
3563
}

pkg/telemetry/tracerexporters/factory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
)
88

99
// ExporterFactory - Create tracer exporter according to given params
10-
func ExporterFactory(name, url string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
10+
func ExporterFactory(name, url string, insecure bool, urlpath string, headers map[string]string, protocol string) (trace.SpanExporter, error) {
1111
switch name {
1212
case "zipkin":
1313
return NewZipkin(url)
1414
case "jaeger":
1515
return NewJaegar(url)
1616
case "otlp", "otlp-http":
17-
return NewOTLP(url, insecure, urlpath, headers)
17+
return NewOTLP(url, insecure, urlpath, headers, "http")
1818
case "otlp-grpc":
19-
return NewOTLPGrpc(url, insecure, headers)
19+
return NewOTLP(url, insecure, urlpath, headers, "grpc")
2020
case "signoz":
2121
return NewSigNoz(url, insecure, headers)
2222
default:

pkg/telemetry/tracerexporters/otlp.go

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,64 @@ package tracerexporters
22

33
import (
44
"context"
5+
"fmt"
56

67
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
8+
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
79
"go.opentelemetry.io/otel/sdk/trace"
10+
"google.golang.org/grpc/credentials"
811
)
912

10-
// NewOTLP - Creates new OTLP exporter using HTTP protocol.
11-
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
12-
var exporter trace.SpanExporter
13-
var err error
13+
// NewOTLP - Creates new OTLP exporter based on protocol.
14+
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (trace.SpanExporter, error) {
15+
switch protocol {
16+
case "http":
17+
opts := []otlptracehttp.Option{
18+
otlptracehttp.WithEndpoint(endpoint),
19+
}
1420

15-
opts := []otlptracehttp.Option{
16-
otlptracehttp.WithEndpoint(endpoint),
17-
}
21+
if len(headers) > 0 {
22+
opts = append(opts, otlptracehttp.WithHeaders(headers))
23+
}
1824

19-
if len(headers) > 0 {
20-
opts = append(opts, otlptracehttp.WithHeaders(headers))
21-
}
25+
if urlpath != "" {
26+
opts = append(opts, otlptracehttp.WithURLPath(urlpath))
27+
}
2228

23-
if urlpath != "" {
24-
opts = append(opts, otlptracehttp.WithURLPath(urlpath))
25-
}
29+
if insecure {
30+
opts = append(opts, otlptracehttp.WithInsecure())
31+
}
2632

27-
if insecure {
28-
opts = append(opts, otlptracehttp.WithInsecure())
29-
}
33+
exporter, err := otlptracehttp.New(context.Background(), opts...)
34+
if err != nil {
35+
return nil, err
36+
}
3037

31-
exporter, err = otlptracehttp.New(context.Background(), opts...)
32-
if err != nil {
33-
return nil, err
34-
}
38+
return exporter, nil
39+
40+
case "grpc":
41+
opts := []otlptracegrpc.Option{
42+
otlptracegrpc.WithEndpoint(endpoint),
43+
}
3544

36-
return exporter, nil
45+
if len(headers) > 0 {
46+
opts = append(opts, otlptracegrpc.WithHeaders(headers))
47+
}
48+
49+
if insecure {
50+
opts = append(opts, otlptracegrpc.WithInsecure())
51+
} else {
52+
opts = append(opts, otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")))
53+
}
54+
55+
exporter, err := otlptracegrpc.New(context.Background(), opts...)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
return exporter, nil
61+
62+
default:
63+
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
64+
}
3765
}

0 commit comments

Comments
 (0)