Skip to content

Commit

Permalink
Moved telemetryInitializer shutdown out of service shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Corbin Phelps <[email protected]>
  • Loading branch information
Corbin Phelps committed Oct 25, 2022
1 parent fd75fc3 commit b7435d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
15 changes: 13 additions & 2 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,16 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
}

if err = col.service.Start(ctx); err != nil {
errs := err
if shutdownErr := col.service.Shutdown(ctx); shutdownErr != nil {
return multierr.Append(err, fmt.Errorf("failed to shutdown service after error: %w", shutdownErr))
errs = multierr.Append(err, fmt.Errorf("failed to shutdown service after error: %w", shutdownErr))
}
return err

// TODO: Move this as part of the service shutdown.
if shutdownErr := col.service.telemetryInitializer.shutdown(); shutdownErr != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown collector telemetry: %w", shutdownErr))
}
return errs
}
col.setCollectorState(Running)
return nil
Expand Down Expand Up @@ -222,6 +228,11 @@ func (col *Collector) shutdown(ctx context.Context) error {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown service: %w", err))
}

// TODO: Move this as part of the service shutdown.
if err := col.service.telemetryInitializer.shutdown(); err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown collector telemetry: %w", err))
}

col.setCollectorState(Closed)

return errs
Expand Down
3 changes: 0 additions & 3 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ func (srv *service) Shutdown(ctx context.Context) error {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown telemetry: %w", err))
}

if err := srv.telemetryInitializer.shutdown(); err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown telemetry initializer: %w", err))
}
// TODO: Shutdown MeterProvider.
return errs
}
Expand Down
20 changes: 20 additions & 0 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package service

import (
"context"
"fmt"
"net/http"
"path/filepath"
"testing"

Expand Down Expand Up @@ -152,6 +154,7 @@ func TestServiceTelemetryReusable(t *testing.T) {

// Create a service
telemetry := newColTelemetry(featuregate.NewRegistry())

srvOne, err := newService(&settings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Expand All @@ -160,9 +163,20 @@ func TestServiceTelemetryReusable(t *testing.T) {
})
require.NoError(t, err)

// Setup tu curl the telemetry URL to ensure it works
telemetryURL := fmt.Sprintf("http://%s/metrics", telemetry.server.Addr)

// Start the service
require.NoError(t, srvOne.Start(context.Background()))

// check telemetry server to ensure we get a response
var resp *http.Response

// #nosec G107
resp, err = http.Get(telemetryURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// Shutdown the service
require.NoError(t, srvOne.Shutdown(context.Background()))

Expand All @@ -185,6 +199,12 @@ func TestServiceTelemetryReusable(t *testing.T) {
// Start the new service
require.NoError(t, srvTwo.Start(context.Background()))

// check telemetry server to ensure we get a response
// #nosec G107
resp, err = http.Get(telemetryURL)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// Shutdown the new service
require.NoError(t, srvTwo.Shutdown(context.Background()))
}
Expand Down

0 comments on commit b7435d5

Please sign in to comment.