Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/metric/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package metric
import (
"context"
"fmt"
"net/url"
"strings"

"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
Expand Down Expand Up @@ -31,13 +33,22 @@ func NewExporter(p *NewExporterParams) (sdkmetric.Exporter, error) {
exporter, err := otlpmetricgrpc.New(context.Background(), options...)
return exporter, err
case "http":
if !strings.Contains(p.OTLPEndpoint, "://") {
p.OTLPEndpoint = "https://" + p.OTLPEndpoint
}
u, err := url.Parse(p.OTLPEndpoint)
if err != nil {
return nil, fmt.Errorf("invalid OTLP endpoint URL: %w", err)
}

options := []otlpmetrichttp.Option{
otlpmetrichttp.WithEndpoint(p.OTLPEndpoint),
otlpmetrichttp.WithEndpoint(u.Host),
otlpmetrichttp.WithURLPath(u.Path),
}
if len(p.OTLPHeaders) > 0 {
options = append(options, otlpmetrichttp.WithHeaders(p.OTLPHeaders))
}
if p.OTLPInsecure {
if p.OTLPInsecure || u.Scheme == "http" {
options = append(options, otlpmetrichttp.WithInsecure())
}
exporter, err := otlpmetrichttp.New(context.Background(), options...)
Expand Down
24 changes: 24 additions & 0 deletions internal/metric/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ func TestNewExporter(t *testing.T) {
// TODO: test with dummy server
})

t.Run("returns otlpmetrichttp.Exporter (with scheme and custom path)", func(t *testing.T) {
t.Parallel()
params := &NewExporterParams{
OTLPProtocol: "http",
OTLPEndpoint: "http://localhost:4318/custom/path/v1/metrics",
}

got, err := NewExporter(params)
assert.NoError(t, err)
assert.IsType(t, got, &otlpmetrichttp.Exporter{})
})

t.Run("returns otlpmetrichttp.Exporter (no scheme, with custom path)", func(t *testing.T) {
t.Parallel()
params := &NewExporterParams{
OTLPProtocol: "http",
OTLPEndpoint: "localhost:4318/custom/path/v1/metrics",
}

got, err := NewExporter(params)
assert.NoError(t, err)
assert.IsType(t, got, &otlpmetrichttp.Exporter{})
})

t.Run("returns error when unexpected protocol is passed", func(t *testing.T) {
t.Parallel()
params := &NewExporterParams{
Expand Down