@@ -7,16 +7,12 @@ import (
77 "context"
88 "errors"
99 "net/http"
10- "runtime"
11- "strings"
12- "sync"
1310 "testing"
1411 "time"
1512
1613 "github.com/stretchr/testify/assert"
1714 "github.com/stretchr/testify/require"
1815 otelconf "go.opentelemetry.io/contrib/otelconf/v0.3.0"
19- apimetric "go.opentelemetry.io/otel/metric"
2016 noopmetric "go.opentelemetry.io/otel/metric/noop"
2117 "go.opentelemetry.io/otel/sdk/metric"
2218 "go.opentelemetry.io/otel/sdk/metric/metricdata"
@@ -886,142 +882,3 @@ func TestRegisterProcessMetrics_SupportedOS_RegisterFails_ReturnsError(t *testin
886882 require .ErrorIs (t , err , wantErr )
887883 require .Contains (t , err .Error (), "failed to register process metrics" )
888884}
889-
890- func TestNew_ProcessMetricsRegistrationFailure_PoisonInject (t * testing.T ) {
891- if runtime .GOOS == "windows" {
892- t .Skip ("Skipping on Windows: avoids OS-specific flakiness in metric registration tests." )
893- }
894-
895- baseSettings := newNopSettings ()
896- baseFactory := baseSettings .TelemetryFactory
897-
898- poisonMP := & poisonMeterProvider {
899- MeterProvider : noopmetric .NewMeterProvider (),
900- }
901-
902- mockFactory := & mockTelemetryFactory {
903- Factory : baseFactory ,
904- mp : poisonMP ,
905- }
906-
907- set := newNopSettings ()
908- set .TelemetryFactory = mockFactory
909- cfg := newNopConfig ()
910-
911- _ , err := New (context .Background (), set , cfg )
912-
913- require .Error (t , err )
914- require .ErrorIs (t , err , errForcedMetric )
915- require .Contains (t , err .Error (), "failed to register process metrics" )
916- }
917-
918- var errForcedMetric = errors .New ("forced metric error" )
919-
920- type mockTelemetryFactory struct {
921- telemetry.Factory
922- mp telemetry.MeterProvider
923- }
924-
925- func (f * mockTelemetryFactory ) CreateMeterProvider (_ context.Context , _ telemetry.MeterSettings , _ component.Config ) (telemetry.MeterProvider , error ) {
926- return f .mp , nil
927- }
928-
929- type poisonMeterProvider struct {
930- apimetric.MeterProvider
931- }
932-
933- func (p * poisonMeterProvider ) Shutdown (_ context.Context ) error { return nil }
934-
935- func (p * poisonMeterProvider ) Meter (name string , opts ... apimetric.MeterOption ) apimetric.Meter {
936- base := p .MeterProvider .Meter (name , opts ... )
937- return & poisonMeter {
938- Meter : base ,
939- processObs : make (map [apimetric.Observable ]struct {}),
940- }
941- }
942-
943- type poisonMeter struct {
944- apimetric.Meter
945-
946- mu sync.Mutex
947- processObs map [apimetric.Observable ]struct {}
948- }
949-
950- func shouldPoisonMetricName (instrumentName string ) bool {
951- return strings .HasPrefix (instrumentName , "otelcol_process_" )
952- }
953-
954- func (m * poisonMeter ) rememberIfProcess (name string , obs apimetric.Observable ) {
955- if ! shouldPoisonMetricName (name ) {
956- return
957- }
958- m .mu .Lock ()
959- m .processObs [obs ] = struct {}{}
960- m .mu .Unlock ()
961- }
962-
963- func (m * poisonMeter ) hasProcessObservable (instruments []apimetric.Observable ) bool {
964- m .mu .Lock ()
965- defer m .mu .Unlock ()
966- for _ , inst := range instruments {
967- if _ , ok := m .processObs [inst ]; ok {
968- return true
969- }
970- }
971- return false
972- }
973-
974- func (m * poisonMeter ) RegisterCallback (cb apimetric.Callback , instruments ... apimetric.Observable ) (apimetric.Registration , error ) {
975- if m .hasProcessObservable (instruments ) {
976- return nil , errForcedMetric
977- }
978- return m .Meter .RegisterCallback (cb , instruments ... )
979- }
980-
981- func (m * poisonMeter ) Int64ObservableGauge (name string , opts ... apimetric.Int64ObservableGaugeOption ) (apimetric.Int64ObservableGauge , error ) {
982- g , err := m .Meter .Int64ObservableGauge (name , opts ... )
983- if err == nil {
984- m .rememberIfProcess (name , g )
985- }
986- return g , err
987- }
988-
989- func (m * poisonMeter ) Float64ObservableGauge (name string , opts ... apimetric.Float64ObservableGaugeOption ) (apimetric.Float64ObservableGauge , error ) {
990- g , err := m .Meter .Float64ObservableGauge (name , opts ... )
991- if err == nil {
992- m .rememberIfProcess (name , g )
993- }
994- return g , err
995- }
996-
997- func (m * poisonMeter ) Int64ObservableCounter (name string , opts ... apimetric.Int64ObservableCounterOption ) (apimetric.Int64ObservableCounter , error ) {
998- c , err := m .Meter .Int64ObservableCounter (name , opts ... )
999- if err == nil {
1000- m .rememberIfProcess (name , c )
1001- }
1002- return c , err
1003- }
1004-
1005- func (m * poisonMeter ) Float64ObservableCounter (name string , opts ... apimetric.Float64ObservableCounterOption ) (apimetric.Float64ObservableCounter , error ) {
1006- c , err := m .Meter .Float64ObservableCounter (name , opts ... )
1007- if err == nil {
1008- m .rememberIfProcess (name , c )
1009- }
1010- return c , err
1011- }
1012-
1013- func (m * poisonMeter ) Int64ObservableUpDownCounter (name string , opts ... apimetric.Int64ObservableUpDownCounterOption ) (apimetric.Int64ObservableUpDownCounter , error ) {
1014- c , err := m .Meter .Int64ObservableUpDownCounter (name , opts ... )
1015- if err == nil {
1016- m .rememberIfProcess (name , c )
1017- }
1018- return c , err
1019- }
1020-
1021- func (m * poisonMeter ) Float64ObservableUpDownCounter (name string , opts ... apimetric.Float64ObservableUpDownCounterOption ) (apimetric.Float64ObservableUpDownCounter , error ) {
1022- c , err := m .Meter .Float64ObservableUpDownCounter (name , opts ... )
1023- if err == nil {
1024- m .rememberIfProcess (name , c )
1025- }
1026- return c , err
1027- }
0 commit comments