Skip to content

Commit

Permalink
fix race in test
Browse files Browse the repository at this point in the history
  • Loading branch information
hgaol committed Dec 8, 2024
1 parent 5e20e58 commit ee7cf01
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
55 changes: 32 additions & 23 deletions exporter/azuremonitorexporter/azuremonitor_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package azuremonitorexporter // import "github.com/open-telemetry/opentelemetry-

import (
"context"
"sync"
"time"

"github.com/microsoft/ApplicationInsights-Go/appinsights"
Expand All @@ -24,35 +25,41 @@ type azureMonitorExporter struct {
transportChannel appinsights.TelemetryChannel
logger *zap.Logger
packer *metricPacker
startOnce sync.Once
endOnce sync.Once
}

func (exporter *azureMonitorExporter) Start(_ context.Context, _ component.Host) error {
connectionVars, err := parseConnectionString(exporter.config)
if err != nil {
return err
}
func (exporter *azureMonitorExporter) Start(_ context.Context, _ component.Host) (err error) {
exporter.startOnce.Do(func() {
connectionVars, err := parseConnectionString(exporter.config)
if err != nil {
return
}

exporter.config.InstrumentationKey = configopaque.String(connectionVars.InstrumentationKey)
exporter.config.Endpoint = connectionVars.IngestionURL
telemetryConfiguration := appinsights.NewTelemetryConfiguration(connectionVars.InstrumentationKey)
telemetryConfiguration.EndpointUrl = connectionVars.IngestionURL
telemetryConfiguration.MaxBatchSize = exporter.config.MaxBatchSize
telemetryConfiguration.MaxBatchInterval = exporter.config.MaxBatchInterval
exporter.config.InstrumentationKey = configopaque.String(connectionVars.InstrumentationKey)
exporter.config.Endpoint = connectionVars.IngestionURL
telemetryConfiguration := appinsights.NewTelemetryConfiguration(connectionVars.InstrumentationKey)
telemetryConfiguration.EndpointUrl = connectionVars.IngestionURL
telemetryConfiguration.MaxBatchSize = exporter.config.MaxBatchSize
telemetryConfiguration.MaxBatchInterval = exporter.config.MaxBatchInterval

telemetryClient := appinsights.NewTelemetryClientFromConfig(telemetryConfiguration)
exporter.transportChannel = telemetryClient.Channel()
telemetryClient := appinsights.NewTelemetryClientFromConfig(telemetryConfiguration)
exporter.transportChannel = telemetryClient.Channel()
})

return nil
}

func (exporter *azureMonitorExporter) Shutdown(_ context.Context) error {
if exporter.transportChannel != nil {
select {
case <-exporter.transportChannel.Close(exporter.config.ShutdownTimeout):
case <-time.After(exporter.config.ShutdownTimeout):
exporter.transportChannel.Stop()
func (exporter *azureMonitorExporter) Shutdown(_ context.Context) (err error) {
exporter.startOnce.Do(func() {
if exporter.transportChannel != nil {
select {
case <-exporter.transportChannel.Close(exporter.config.ShutdownTimeout):
case <-time.After(exporter.config.ShutdownTimeout):
exporter.transportChannel.Stop()
}
}
}
})

return nil
}
Expand Down Expand Up @@ -149,8 +156,10 @@ func (exporter *azureMonitorExporter) consumeTraces(_ context.Context, traceData
// Returns a new instance of the log exporter
func newAzureMonitorExporter(config *Config, set exporter.Settings) AzureMonitorExporter {
return &azureMonitorExporter{
config: config,
logger: set.Logger,
packer: newMetricPacker(set.Logger),
config: config,
logger: set.Logger,
packer: newMetricPacker(set.Logger),
startOnce: sync.Once{},
endOnce: sync.Once{},
}
}
38 changes: 38 additions & 0 deletions exporter/azuremonitorexporter/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions exporter/azuremonitorexporter/logexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Contains tests for logexporter.go and log_to_envelope.go

import (
"context"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -185,6 +186,8 @@ func getLogsExporter(config *Config, transportChannel appinsights.TelemetryChann
transportChannel,
zap.NewNop(),
newMetricPacker(zap.NewNop()),
sync.Once{},
sync.Once{},
}
}

Expand Down
4 changes: 1 addition & 3 deletions exporter/azuremonitorexporter/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ tests:
instrumentation_key: b1cd0778-85fc-4677-a3fa-79d3c23e0efd
expect_consumer_error: true
goleak:
skip: true
# todo: update to pass race in start and shutdown
skip_lifecycle: true
skip: true
3 changes: 3 additions & 0 deletions exporter/azuremonitorexporter/metricexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Contains tests for metricexporter.go and metric_to_envelopes.go

import (
"context"
"sync"
"testing"

"github.com/microsoft/ApplicationInsights-Go/appinsights"
Expand Down Expand Up @@ -144,6 +145,8 @@ func getAzureMonitorExporter(config *Config, transportChannel appinsights.Teleme
transportChannel,
zap.NewNop(),
newMetricPacker(zap.NewNop()),
sync.Once{},
sync.Once{},
}
}

Expand Down
3 changes: 3 additions & 0 deletions exporter/azuremonitorexporter/traceexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package azuremonitorexporter

import (
"sync"
"testing"

"github.com/microsoft/ApplicationInsights-Go/appinsights"
Expand Down Expand Up @@ -127,5 +128,7 @@ func getExporter(config *Config, transportChannel appinsights.TelemetryChannel)
transportChannel,
zap.NewNop(),
newMetricPacker(zap.NewNop()),
sync.Once{},
sync.Once{},
}
}

0 comments on commit ee7cf01

Please sign in to comment.