Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SLT-141] feat(metrics): multiple exports #3099

Merged
merged 33 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7ba8a43
initial
golangisfun123 Sep 3, 2024
9632578
done
golangisfun123 Sep 4, 2024
c762c4c
add lint
golangisfun123 Sep 4, 2024
3c15801
Merge branch 'master' into multiple-exports
trajan0x Sep 5, 2024
829812b
[goreleaser]
trajan0x Sep 5, 2024
e831fe9
different environment variables
golangisfun123 Sep 5, 2024
68def3c
rework logic, make primary/secondary optional, make code clearer, upd…
golangisfun123 Sep 5, 2024
51284b9
clearer code
golangisfun123 Sep 5, 2024
6bcd3ab
interfacify it better
golangisfun123 Sep 5, 2024
38e924b
lint
golangisfun123 Sep 5, 2024
f8a2048
[goreleaser]
golangisfun123 Sep 5, 2024
c163704
add test
trajan0x Sep 10, 2024
89e996b
cleanup
trajan0x Sep 10, 2024
780b4c5
actually suport multi exports
trajan0x Sep 10, 2024
1d82704
address https://github.com/synapsecns/sanguine/pull/3099/files#r17513…
trajan0x Sep 10, 2024
08fa2a4
fix function call [goreleaser]
golangisfun123 Sep 10, 2024
1c63c19
warnings and uneeded err check
golangisfun123 Sep 10, 2024
d08315e
more accurate comment
golangisfun123 Sep 10, 2024
1d670a0
[goreleaser]
golangisfun123 Sep 10, 2024
d6ee535
cleanup
trajan0x Sep 11, 2024
48f2afc
resolve merge conflicts
trajan0x Sep 11, 2024
4db816f
header bump [goreleaser]
trajan0x Sep 11, 2024
3923d42
Merge branch 'master' into multiple-exports
trajan0x Sep 11, 2024
bad4071
insecure mode [goreleaser]
trajan0x Sep 11, 2024
f8e240d
secure [goreleaser]
trajan0x Sep 11, 2024
a19300c
set default exporter to otlpgrpc again
trajan0x Sep 11, 2024
bfd1d7b
test
trajan0x Sep 11, 2024
d5e69f7
Revert "test"
trajan0x Sep 11, 2024
ceeddbc
cleanup
trajan0x Sep 11, 2024
4c6825a
cleanup [goreleaser]
trajan0x Sep 11, 2024
732f5fa
Merge branch 'multiple-exports' of https://github.com/synapsecns/sang…
trajan0x Sep 11, 2024
8a04f9e
paralellize exports [goreleaser]
trajan0x Sep 11, 2024
0e3a5ae
url
trajan0x Sep 11, 2024
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
2 changes: 1 addition & 1 deletion core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.8.0
google.golang.org/grpc v1.64.0
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.10
k8s.io/apimachinery v0.29.3
Expand Down Expand Up @@ -189,7 +190,6 @@ require (
golang.org/x/tools v0.24.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
20 changes: 19 additions & 1 deletion core/metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ There's also a `NAME_PREFIX` environment variable that will prefix all the metri

## OTLP

We do our best to support enviornment variables specified in the [Otel Spec](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/) and have added a few of our own. Key ones to note are:
We do our best to support enviornment variables specified in the [Otel Spec](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/) and the [OTLP Spec](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/) and have added a few of our own. This was to allow for multiple exporter backends for traces, as otel clients only allow for one URL. The relevant multi exporter code is in `multiexporter.go`, and simply wraps multiple otel clients.

The additional environment variables to note are:
| Enviornment Variable | Description | Default |
|------------------------------------------|-------------------------------------------|---------|
| `OTEL_EXPORTER_OTLP_ENDPOINT` | The endpoint for the primary OTLP exporter | None |
| `OTEL_EXPORTER_OTLP_ENDPOINT_1` | The endpoint for the first additional OTLP exporter | None |
| `OTEL_EXPORTER_OTLP_ENDPOINT_2` | The endpoint for the second additional OTLP exporter | None |
| `OTEL_EXPORTER_OTLP_ENDPOINT_3` | The endpoint for the third additional OTLP exporter | None |
| ... | Additional endpoints can be added by incrementing the number | None |
| `OTEL_EXPORTER_OTLP_TRANSPORT` | The transport protocol for the primary OTLP exporter | `http` |
| `OTEL_EXPORTER_OTLP_TRANSPORT_1` | The transport protocol for the first additional OTLP exporter | `http` |
| `OTEL_EXPORTER_OTLP_TRANSPORT_2` | The transport protocol for the second additional OTLP exporter | `http` |
| `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-->

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address the TODO comment.

Please ensure that the documentation is fully updated before merging the PR, as indicated by the TODO comment.

If you need any assistance with completing the documentation, feel free to let me know.

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.


## Jaeger
Expand Down
6 changes: 6 additions & 0 deletions core/metrics/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package metrics

// HeadersToMap converts a string of headers to a map.
func HeadersToMap(val string) map[string]string {
return headersToMap(val)
}
57 changes: 57 additions & 0 deletions core/metrics/multiexporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package metrics

import (
"context"
"fmt"

"go.opentelemetry.io/otel/sdk/trace"

Check failure on line 7 in core/metrics/multiexporter.go

View workflow job for this annotation

GitHub Actions / Lint (core)

ST1019: package "go.opentelemetry.io/otel/sdk/trace" is being imported more than once (stylecheck)
tracesdk "go.opentelemetry.io/otel/sdk/trace"

Check failure on line 8 in core/metrics/multiexporter.go

View workflow job for this annotation

GitHub Actions / Lint (core)

ST1019(related information): other import of "go.opentelemetry.io/otel/sdk/trace" (stylecheck)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the duplicate import of go.opentelemetry.io/otel/sdk/trace.

The go.opentelemetry.io/otel/sdk/trace package is being imported twice, once as trace and once as tracesdk. This is unnecessary and can cause confusion.

Apply this diff to remove the duplicate import:

 import (
 	"context"
 	"fmt"
 
-	"go.opentelemetry.io/otel/sdk/trace"
 	tracesdk "go.opentelemetry.io/otel/sdk/trace"
 )
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"go.opentelemetry.io/otel/sdk/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
Tools
GitHub Check: Lint (core)

[failure] 7-7:
ST1019: package "go.opentelemetry.io/otel/sdk/trace" is being imported more than once (stylecheck)


[failure] 8-8:
ST1019(related information): other import of "go.opentelemetry.io/otel/sdk/trace" (stylecheck)

)

// MultiExporter is an interface that allows exporting spans to multiple OTLP trace exporters.
type MultiExporter interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should have a test

trace.SpanExporter
AddExporter(exporter trace.SpanExporter)
}

type multiExporter struct {
exporters []trace.SpanExporter
}

// NewMultiExporter creates a new multi exporter that forwards spans to multiple OTLP trace exporters.
// It takes in one or more trace.SpanExporter instances and ensures that spans are sent to all of them.
// This is useful when you need to send trace data to multiple backends or endpoints.
func NewMultiExporter(exporters ...trace.SpanExporter) MultiExporter {
return &multiExporter{
exporters: exporters,
}
}

// ExportSpans exports a batch of spans.
func (m *multiExporter) ExportSpans(ctx context.Context, ss []trace.ReadOnlySpan) error {
for _, exporter := range m.exporters {
err := exporter.ExportSpans(ctx, ss)
if err != nil {
return fmt.Errorf("could not export spans: %w", err)
}

Check warning on line 36 in core/metrics/multiexporter.go

View check run for this annotation

Codecov / codecov/patch

core/metrics/multiexporter.go#L32-L36

Added lines #L32 - L36 were not covered by tests
}
return nil

Check warning on line 38 in core/metrics/multiexporter.go

View check run for this annotation

Codecov / codecov/patch

core/metrics/multiexporter.go#L38

Added line #L38 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider collecting errors from all exporters before returning.

The current implementation of ExportSpans returns immediately if an error occurs while exporting to one of the exporters. This means that if one exporter fails, the remaining exporters will not receive the spans.

It would be better to collect errors from all exporters and return a combined error. This way, all exporters will have a chance to receive the spans, and the caller will be informed of all the errors that occurred.

Apply this diff to collect errors from all exporters:

 func (m *multiExporter) ExportSpans(ctx context.Context, ss []tracesdk.ReadOnlySpan) error {
+	var errs []error
 	for _, exporter := range m.exporters {
 		err := exporter.ExportSpans(ctx, ss)
 		if err != nil {
-			return fmt.Errorf("could not export spans: %w", err)
+			errs = append(errs, err)
 		}
 	}
+	if len(errs) > 0 {
+		return fmt.Errorf("could not export spans to some exporters: %v", errs)
+	}
 	return nil
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (m *multiExporter) ExportSpans(ctx context.Context, ss []trace.ReadOnlySpan) error {
for _, exporter := range m.exporters {
err := exporter.ExportSpans(ctx, ss)
if err != nil {
return fmt.Errorf("could not export spans: %w", err)
}
}
return nil
func (m *multiExporter) ExportSpans(ctx context.Context, ss []trace.ReadOnlySpan) error {
var errs []error
for _, exporter := range m.exporters {
err := exporter.ExportSpans(ctx, ss)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return fmt.Errorf("could not export spans to some exporters: %v", errs)
}
return nil
}
Tools
GitHub Check: codecov/patch

[warning] 32-36: core/metrics/multiexporter.go#L32-L36
Added lines #L32 - L36 were not covered by tests


[warning] 38-38: core/metrics/multiexporter.go#L38
Added line #L38 was not covered by tests

}

// Shutdown notifies the exporter of a pending halt to operations.
func (m *multiExporter) Shutdown(ctx context.Context) error {
for _, exporter := range m.exporters {
err := exporter.Shutdown(ctx)
if err != nil {
return fmt.Errorf("could not stop exporter: %w", err)
}

Check warning on line 47 in core/metrics/multiexporter.go

View check run for this annotation

Codecov / codecov/patch

core/metrics/multiexporter.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method Implementation Review: Shutdown

Similar to ExportSpans, the Shutdown method stops at the first error encountered. This could potentially leave some exporters running if one fails to shut down properly.

Consider implementing a similar error aggregation strategy as suggested for ExportSpans to ensure all exporters are attempted to be shut down before returning an error.

-		if err != nil {
-			return fmt.Errorf("could not stop exporter: %w", err)
-		}
+		if err != nil {
+			// Collect errors from all exporters
+			allErrors = append(allErrors, err)
+		}
+	}
+	if len(allErrors) > 0 {
+		return fmt.Errorf("could not stop some exporters: %v", allErrors)
+	}

Committable suggestion was skipped due to low confidence.

Tools
GitHub Check: codecov/patch

[warning] 46-47: core/metrics/multiexporter.go#L46-L47
Added lines #L46 - L47 were not covered by tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider collecting errors from all exporters before returning.

Similar to the ExportSpans method, the current implementation of Shutdown returns immediately if an error occurs while shutting down one of the exporters. This means that if one exporter fails to shut down, the remaining exporters will not be shut down.

As suggested in the existing review comments, it would be better to collect errors from all exporters and return a combined error. This way, all exporters will be attempted to be shut down, and the caller will be informed of all the errors that occurred.

Apply this diff to collect errors from all exporters:

 func (m *multiExporter) Shutdown(ctx context.Context) error {
+	var errs []error
 	for _, exporter := range m.exporters {
 		err := exporter.Shutdown(ctx)
 		if err != nil {
-			return fmt.Errorf("could not stop exporter: %w", err)
+			errs = append(errs, err)
 		}
 	}
+	if len(errs) > 0 {
+		return fmt.Errorf("could not stop some exporters: %v", errs)
+	}
 	return nil
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (m *multiExporter) Shutdown(ctx context.Context) error {
for _, exporter := range m.exporters {
err := exporter.Shutdown(ctx)
if err != nil {
return fmt.Errorf("could not stop exporter: %w", err)
}
}
return nil
}
func (m *multiExporter) Shutdown(ctx context.Context) error {
var errs []error
for _, exporter := range m.exporters {
err := exporter.Shutdown(ctx)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return fmt.Errorf("could not stop some exporters: %v", errs)
}
return nil
}


// AddExporter adds an exporter to the multi exporter.
func (m *multiExporter) AddExporter(exporter trace.SpanExporter) {
m.exporters = append(m.exporters, exporter)
}

var _ tracesdk.SpanExporter = &multiExporter{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure test coverage for multi-exporter logic.

The introduction of the multi-exporter logic is a significant enhancement. However, static analysis indicates that several lines in the ExportSpans and Shutdown methods are not covered by tests.

It's crucial to ensure that the multi-exporter functionality is covered by unit tests to verify its behavior under various scenarios, especially error handling.

Consider adding unit tests to cover these scenarios to ensure robust functionality verification.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 32-36: core/metrics/multiexporter.go#L32-L36
Added lines #L32 - L36 were not covered by tests


[warning] 38-38: core/metrics/multiexporter.go#L38
Added line #L38 was not covered by tests


[warning] 46-47: core/metrics/multiexporter.go#L46-L47
Added lines #L46 - L47 were not covered by tests

45 changes: 45 additions & 0 deletions core/metrics/multiexporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package metrics_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/synapsecns/sanguine/core/metrics"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)

func TestMultiExporter(t *testing.T) {
// Create in-memory exporters
exporter1 := tracetest.NewInMemoryExporter()
exporter2 := tracetest.NewInMemoryExporter()

// Create multi-exporter
multiExporter := metrics.NewMultiExporter(exporter1, exporter2)

// Create test spans
spans := []sdktrace.ReadOnlySpan{
tracetest.SpanStub{}.Snapshot(),
tracetest.SpanStub{}.Snapshot(),
}

// Test ExportSpans
err := multiExporter.ExportSpans(context.Background(), spans)
require.NoError(t, err)

// Verify that spans were exported to both exporters
assert.Equal(t, 2, len(exporter1.GetSpans()))
assert.Equal(t, 2, len(exporter2.GetSpans()))

// Test Shutdown
err = multiExporter.Shutdown(context.Background())
require.NoError(t, err)

// Verify that both exporters were shut down
// Note: InMemoryExporter doesn't have a Stopped() method, so we can't check this directly
// Instead, we can try to export spans again and check for an error
err = multiExporter.ExportSpans(context.Background(), spans)
assert.NoError(t, err, "Expected no error after shutdown")
}
Loading
Loading