Skip to content

Commit

Permalink
cleanup [goreleaser]
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Sep 11, 2024
1 parent d5e69f7 commit 4c6825a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions core/metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The additional environment variables to note are:
| `OTEL_EXPORTER_OTLP_TRANSPORT_3` | The transport protocol for the third additional OTLP exporter | `http` |
| ... | Additional transports can be specified by incrementing the number | `http` |

<!-- TODO: fully document before pr merged-->

Note: The OTLP exporter endpoints and transports can be specified for multiple exporters by using incrementing numbers (1, 2, 3, etc.) in the environment variable names. This allows for configuration of multiple OTLP exporters. The primary exporter uses the base names without numbers.


Expand Down
42 changes: 32 additions & 10 deletions core/metrics/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"context"
"crypto/tls"
"fmt"
"google.golang.org/grpc/credentials"
"strings"
Expand Down Expand Up @@ -115,7 +116,7 @@ func handleShutdown(ctx context.Context, provider *tracesdk.TracerProvider) {
const (
otelEndpointEnv = "OTEL_EXPORTER_OTLP_ENDPOINT"
otelTransportEnv = "OTEL_EXPORTER_OTLP_TRANSPORT"
otelInsecureEvn = "OTEL_EXPORTER_OTLP_INSECURE_MODE"
otelInsecureEvn = "OTEL_EXPORTER_OTLP_SECURE_MODE"
otelHeadersEnv = "OTEL_EXPORTER_OTLP_HEADERS"
)

Expand All @@ -129,26 +130,42 @@ const (

// getEnvSuffix returns the value of an environment variable with a suffix.
func getEnvSuffix(env, suffix, defaultRet string) string {
newEnv := env + suffix
return core.GetEnv(newEnv, defaultRet)
return core.GetEnv(makeEnv(env, suffix), defaultRet)
}

func makeEnv(env, suffix string) string {
return env + suffix
}

// makeOTLPTrace creates a new OTLP client based on the transport type and url.
func makeOTLPExporter(ctx context.Context, envSuffix string) (*otlptrace.Exporter, error) {
transport := transportFromString(getEnvSuffix(otelTransportEnv, envSuffix, otlpTransportGRPC.String()))
url := getEnvSuffix(otelEndpointEnv, envSuffix, "")
insecure := getEnvSuffix(otelInsecureEvn, envSuffix, "false")
secure := core.GetEnvBool(makeEnv(otelInsecureEvn, envSuffix), false)
headers := getEnvSuffix(otelHeadersEnv, envSuffix, "")

isCorrect := envSuffix != ""

if isCorrect != secure {
return nil, fmt.Errorf("could not create exporter: secure mode is not set correctly")
}

if url == "" {
return nil, fmt.Errorf("could not create exporter: url is empty")
}

// I spent about 2 hours trying to figure out why this was failing to no avail. I'm going to leave it as is for now.
// My best guess is the issue is around the tsl config.
// Should you attempt to fix this and fail, please increment the counter above, although I send my umost encouragement.
if secure && transport == otlpTransportHTTP {
return nil, fmt.Errorf("could not create exporter: http transport does not support secure mode")
}

oteltraceClient, err := buildClientFromTransport(
transport,
WithURL(url),
// defaults to true
WithInsecure(insecure == "false"),
WithSecure(secure),
WithHeaders(headers),
)
if err != nil {
Expand Down Expand Up @@ -203,15 +220,20 @@ func WithURL(url string) Option {
}
}

func WithInsecure(isInsecure bool) Option {
func WithSecure(secure bool) Option {
return func(o *transportOptions) error {
if isInsecure {
o.httpOptions = append(o.httpOptions, otlptracehttp.WithInsecure())
o.grpcOptions = append(o.grpcOptions, otlptracegrpc.WithInsecure())
} else {
if secure {
tlsCreds := credentials.NewClientTLSFromCert(nil, "")
// note: you do not need to specify the tls creds for http, this happens automatically when https:// is used as the protocol scheme.
o.grpcOptions = append(o.grpcOptions, otlptracegrpc.WithTLSCredentials(tlsCreds))

tlsConfig := &tls.Config{
// RootCAs is nil, which means the default system root CAs are used
}
o.httpOptions = append(o.httpOptions, otlptracehttp.WithTLSClientConfig(tlsConfig))
} else {
o.httpOptions = append(o.httpOptions, otlptracehttp.WithInsecure())
o.grpcOptions = append(o.grpcOptions, otlptracegrpc.WithInsecure())
}

return nil
Expand Down

0 comments on commit 4c6825a

Please sign in to comment.