-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
271 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
with-expecter: true | ||
disable-version-string: true | ||
inpackage: false | ||
mockname: "{{.InterfaceName}}" | ||
filename: "{{.InterfaceNameSnake}}.go" | ||
outpkg: "mocks" | ||
dir: "{{.InterfaceDirRelative}}/mocks" | ||
|
||
packages: | ||
"flamingo.me/opentelemetry": | ||
interfaces: | ||
Shutdowner: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package opentelemetry | ||
|
||
//go:generate go run github.com/vektra/mockery/[email protected] | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
@@ -69,6 +71,8 @@ func (m *Module) Inject( | |
func (m *Module) Configure(injector *dingo.Injector) { | ||
http.DefaultTransport = &correlationIDInjector{next: otelhttp.NewTransport(http.DefaultTransport)} | ||
|
||
flamingo.BindEventSubscriber(injector).To(new(Listener)) | ||
|
||
m.initTraces() | ||
m.initMetrics(injector) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package opentelemetry_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"flamingo.me/dingo" | ||
"flamingo.me/flamingo/v3/framework/config" | ||
"flamingo.me/flamingo/v3/framework/flamingo" | ||
|
||
"flamingo.me/opentelemetry" | ||
) | ||
|
||
type ( | ||
loggerModule struct{} | ||
) | ||
|
||
// Configure DI | ||
func (m *loggerModule) Configure(injector *dingo.Injector) { | ||
injector.Bind(new(flamingo.Logger)).To(new(flamingo.NullLogger)) | ||
} | ||
|
||
func TestModule_Configure(t *testing.T) { | ||
t.Parallel() | ||
|
||
if err := config.TryModules(nil, new(loggerModule), new(opentelemetry.Module)); err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package opentelemetry | ||
|
||
import ( | ||
"context" | ||
|
||
"flamingo.me/flamingo/v3/framework/flamingo" | ||
"go.opentelemetry.io/otel" | ||
) | ||
|
||
type ( | ||
Shutdowner interface { | ||
Shutdown(ctx context.Context) error | ||
} | ||
|
||
Listener struct { | ||
logger flamingo.Logger | ||
} | ||
) | ||
|
||
// Inject dependencies | ||
func (l *Listener) Inject( | ||
logger flamingo.Logger, | ||
) *Listener { | ||
l.logger = logger | ||
|
||
return l | ||
} | ||
|
||
func (l *Listener) Notify(ctx context.Context, event flamingo.Event) { | ||
if _, ok := event.(*flamingo.ShutdownEvent); ok { | ||
tp := otel.GetTracerProvider() | ||
if s, ok := tp.(Shutdowner); ok { | ||
l.shutdown(ctx, s) | ||
} | ||
|
||
mp := otel.GetMeterProvider() | ||
if s, ok := mp.(Shutdowner); ok { | ||
l.shutdown(ctx, s) | ||
} | ||
} | ||
} | ||
|
||
func (l *Listener) shutdown(ctx context.Context, s Shutdowner) { | ||
l.log().Debugf("Shutdown OpenTelemetry: %T", s) | ||
|
||
err := s.Shutdown(ctx) | ||
if err != nil { | ||
l.log().Error("", err) | ||
} | ||
} | ||
|
||
func (l *Listener) log() flamingo.Logger { | ||
return l.logger. | ||
WithField(flamingo.LogKeyModule, "opentelemetry"). | ||
WithField(flamingo.LogKeyCategory, "Shutdown") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package opentelemetry_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"flamingo.me/flamingo/v3/framework/flamingo" | ||
"go.opentelemetry.io/otel" | ||
noopMetric "go.opentelemetry.io/otel/metric/noop" | ||
noopTrace "go.opentelemetry.io/otel/trace/noop" | ||
|
||
"flamingo.me/opentelemetry" | ||
"flamingo.me/opentelemetry/mocks" | ||
) | ||
|
||
type ( | ||
tracerProvider struct { | ||
noopTrace.TracerProvider | ||
mocks.Shutdowner | ||
} | ||
|
||
meterProvider struct { | ||
noopMetric.MeterProvider | ||
mocks.Shutdowner | ||
} | ||
) | ||
|
||
var errShutdown = errors.New("shutdown error") | ||
|
||
func TestListener_Notify(t *testing.T) { //nolint:tparallel // no parallel subtests possible because of global state manipulation | ||
t.Parallel() | ||
|
||
type args struct { | ||
event flamingo.Event | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
traceShutdownError error | ||
meterShutdownError error | ||
}{ | ||
{ | ||
name: "shutdown meter and tracer successfully", | ||
args: args{ | ||
event: new(flamingo.ShutdownEvent), | ||
}, | ||
traceShutdownError: nil, | ||
meterShutdownError: nil, | ||
}, | ||
{ | ||
name: "error on shutdown meter and tracer", | ||
args: args{ | ||
event: new(flamingo.ShutdownEvent), | ||
}, | ||
traceShutdownError: errShutdown, | ||
meterShutdownError: errShutdown, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { //nolint:paralleltest // no parallel test possible because of global state manipulation | ||
t.Run(tt.name, func(t *testing.T) { | ||
tp := new(tracerProvider) | ||
tp.Shutdowner.EXPECT().Shutdown(context.Background()).Once().Return(tt.traceShutdownError) | ||
otel.SetTracerProvider(tp) | ||
|
||
mp := new(meterProvider) | ||
mp.Shutdowner.EXPECT().Shutdown(context.Background()).Once().Return(tt.meterShutdownError) | ||
otel.SetMeterProvider(mp) | ||
|
||
l := new(opentelemetry.Listener).Inject(new(flamingo.NullLogger)) | ||
|
||
l.Notify(context.Background(), tt.args.event) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.