Skip to content

Commit

Permalink
feat: expand support for both HTTP and gRPC protocols in meter/loggin…
Browse files Browse the repository at this point in the history
…g/tracing exporters
  • Loading branch information
Vikash Prem Sharma authored and Vikash Prem Sharma committed Aug 24, 2024
1 parent 09ab66e commit 63ca8d9
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 73 deletions.
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type (
Insecure bool `mapstructure:"insecure"` // Connect to the collector using the HTTP scheme, instead of HTTPS.
URLPath string `mapstructure:"path"` // Path for the log exporter, if not defined /v1/logs will be used
Headers []string `mapstructure:"headers"`
Protocol string `mapstructure:"protocol"` // Protocol for the log exporter, e.g., http, grpc
}

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

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

// Service contains configuration for various service-level features.
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Log.Insecure,
cfg.Log.URLPath,
headers,
cfg.Log.Protocol,
)
if err != nil {
return errors.New("invalid logger exporter")
Expand Down Expand Up @@ -293,6 +294,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Tracer.Insecure,
cfg.Tracer.URLPath,
headers,
cfg.Tracer.Protocol,
)
if err != nil {
slog.Error(err.Error())
Expand Down Expand Up @@ -344,6 +346,7 @@ func serve() func(cmd *cobra.Command, args []string) error {
cfg.Meter.Insecure,
cfg.Meter.URLPath,
headers,
cfg.Meter.Protocol,
)

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/telemetry/logexporters/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
)

// ExporterFactory - Create log exporter according to given params
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string) (*otlplogs.Exporter, error) {
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (*otlplogs.Exporter, error) {
switch name {
case "otlp", "otlp-http":
return NewOTLP(endpoint, insecure, urlpath, headers)
return NewOTLP(endpoint, insecure, urlpath, headers, "http")
case "otlp-grpc":
return nil, fmt.Errorf("%s log exporter is unsupported", name)
return NewOTLP(endpoint, insecure, urlpath, headers, "grpc")
default:
return nil, fmt.Errorf("%s log exporter is unsupported", name)
}
Expand Down
64 changes: 45 additions & 19 deletions pkg/telemetry/logexporters/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,58 @@ package logexporters

import (
"context"
"fmt"

"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs"
"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogshttp"
"github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogsgrpc"
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (*otlplogs.Exporter, error) {
options := []otlplogshttp.Option{
otlplogshttp.WithCompression(otlplogshttp.GzipCompression),
otlplogshttp.WithEndpoint(endpoint),
}
// NewOTLP - Creates new OTLP exporter based on protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (*otlplogs.Exporter, error) {
switch protocol {
case "http":
options := []otlplogshttp.Option{
otlplogshttp.WithCompression(otlplogshttp.GzipCompression),
otlplogshttp.WithEndpoint(endpoint),
}

if urlpath != "" {
options = append(options, otlplogshttp.WithURLPath(urlpath))
}
if urlpath != "" {
options = append(options, otlplogshttp.WithURLPath(urlpath))
}

if insecure {
options = append(options, otlplogshttp.WithInsecure())
}
if insecure {
options = append(options, otlplogshttp.WithInsecure())
}

exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
otlplogshttp.NewClient(options...),
))
if err != nil {
return nil, err
}
exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
otlplogshttp.NewClient(options...),
))
if err != nil {
return nil, err
}

return exporter, nil

return exporter, nil
case "grpc":
options := []otlplogsgrpc.Option{
otlplogsgrpc.WithEndpoint(endpoint),
}

if insecure {
options = append(options, otlplogsgrpc.WithInsecure())
}

exporter, err := otlplogs.NewExporter(context.Background(), otlplogs.WithClient(
otlplogsgrpc.NewClient(options...),
))
if err != nil {
return nil, err
}

return exporter, nil

default:
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
}
}
6 changes: 3 additions & 3 deletions pkg/telemetry/meterexporters/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
)

// ExporterFactory - Create meter exporter according to given params
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
func ExporterFactory(name, endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (metric.Exporter, error) {
switch name {
case "otlp", "otlp-http":
return NewOTLP(endpoint, insecure, urlpath, headers)
return NewOTLP(endpoint, insecure, urlpath, headers, "http")
case "otlp-grpc":
return NewOTLPGrpc(endpoint, insecure, headers)
return NewOTLP(endpoint, insecure, urlpath, headers, "grpc")
default:
return nil, fmt.Errorf("%s meter exporter is unsupported", name)
}
Expand Down
76 changes: 52 additions & 24 deletions pkg/telemetry/meterexporters/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,62 @@ package meterexporters

import (
"context"
"fmt"

"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/sdk/metric"
"google.golang.org/grpc/credentials"
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (metric.Exporter, error) {
options := []otlpmetrichttp.Option{
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
otlpmetrichttp.WithEndpoint(endpoint),
// NewOTLP - Creates new OTLP exporter based on protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (metric.Exporter, error) {
switch protocol {
case "http":
options := []otlpmetrichttp.Option{
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
otlpmetrichttp.WithEndpoint(endpoint),
}

if len(headers) > 0 {
options = append(options, otlpmetrichttp.WithHeaders(headers))
}

if urlpath != "" {
options = append(options, otlpmetrichttp.WithURLPath(urlpath))
}

if insecure {
options = append(options, otlpmetrichttp.WithInsecure())
}

exporter, err := otlpmetrichttp.New(context.Background(), options...)
if err != nil {
return nil, err
}

return exporter, nil

case "grpc":
options := []otlpmetricgrpc.Option{
otlpmetricgrpc.WithEndpoint(endpoint),
otlpmetricgrpc.WithHeaders(headers),
}

if insecure {
options = append(options, otlpmetricgrpc.WithInsecure())
} else {
options = append(options, otlpmetricgrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")))
}

exporter, err := otlpmetricgrpc.New(context.Background(), options...)
if err != nil {
return nil, err
}

return exporter, nil

default:
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
}

if len(headers) > 0 {
options = append(options, otlpmetrichttp.WithHeaders(headers))
}

if urlpath != "" {
options = append(options, otlpmetrichttp.WithURLPath(urlpath))
}

if insecure {
options = append(options, otlpmetrichttp.WithInsecure())
}

exporter, err := otlpmetrichttp.New(context.Background(), options...)
if err != nil {
return nil, err
}

return exporter, nil
}
6 changes: 3 additions & 3 deletions pkg/telemetry/tracerexporters/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

// ExporterFactory - Create tracer exporter according to given params
func ExporterFactory(name, url string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
func ExporterFactory(name, url string, insecure bool, urlpath string, headers map[string]string, protocol string) (trace.SpanExporter, error) {
switch name {
case "zipkin":
return NewZipkin(url)
case "jaeger":
return NewJaegar(url)
case "otlp", "otlp-http":
return NewOTLP(url, insecure, urlpath, headers)
return NewOTLP(url, insecure, urlpath, headers, "http")
case "otlp-grpc":
return NewOTLPGrpc(url, insecure, headers)
return NewOTLP(url, insecure, urlpath, headers, "grpc")
case "signoz":
return NewSigNoz(url, insecure, headers)
default:
Expand Down
70 changes: 49 additions & 21 deletions pkg/telemetry/tracerexporters/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,64 @@ package tracerexporters

import (
"context"
"fmt"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/grpc/credentials"
)

// NewOTLP - Creates new OTLP exporter using HTTP protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string) (trace.SpanExporter, error) {
var exporter trace.SpanExporter
var err error
// NewOTLP - Creates new OTLP exporter based on protocol.
func NewOTLP(endpoint string, insecure bool, urlpath string, headers map[string]string, protocol string) (trace.SpanExporter, error) {
switch protocol {
case "http":
opts := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(endpoint),
}

opts := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(endpoint),
}
if len(headers) > 0 {
opts = append(opts, otlptracehttp.WithHeaders(headers))
}

if len(headers) > 0 {
opts = append(opts, otlptracehttp.WithHeaders(headers))
}
if urlpath != "" {
opts = append(opts, otlptracehttp.WithURLPath(urlpath))
}

if urlpath != "" {
opts = append(opts, otlptracehttp.WithURLPath(urlpath))
}
if insecure {
opts = append(opts, otlptracehttp.WithInsecure())
}

if insecure {
opts = append(opts, otlptracehttp.WithInsecure())
}
exporter, err := otlptracehttp.New(context.Background(), opts...)
if err != nil {
return nil, err
}

exporter, err = otlptracehttp.New(context.Background(), opts...)
if err != nil {
return nil, err
}
return exporter, nil

case "grpc":
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(endpoint),
}

return exporter, nil
if len(headers) > 0 {
opts = append(opts, otlptracegrpc.WithHeaders(headers))
}

if insecure {
opts = append(opts, otlptracegrpc.WithInsecure())
} else {
opts = append(opts, otlptracegrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")))
}

exporter, err := otlptracegrpc.New(context.Background(), opts...)
if err != nil {
return nil, err
}

return exporter, nil

default:
return nil, fmt.Errorf("unsupported protocol: %s", protocol)
}
}

0 comments on commit 63ca8d9

Please sign in to comment.