InstrumentMetrics starts reporting OpenTelemetry Metrics. Based on https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-metrics.md
(rdb redis.UniversalClient, opts ...MetricsOption)
| 25 | // |
| 26 | // Based on https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-metrics.md |
| 27 | func InstrumentMetrics(rdb redis.UniversalClient, opts ...MetricsOption) error { |
| 28 | baseOpts := make([]baseOption, len(opts)) |
| 29 | for i, opt := range opts { |
| 30 | baseOpts[i] = opt |
| 31 | } |
| 32 | conf := newConfig(baseOpts...) |
| 33 | |
| 34 | if conf.meter == nil { |
| 35 | conf.meter = conf.mp.Meter( |
| 36 | instrumName, |
| 37 | metric.WithInstrumentationVersion("semver:"+redis.Version()), |
| 38 | ) |
| 39 | } |
| 40 | if conf.poolName == "" { |
| 41 | switch rdb := rdb.(type) { |
| 42 | case *redis.Client: |
| 43 | conf.poolName = rdb.Options().Addr |
| 44 | case *redis.ClusterClient: |
| 45 | for _, addr := range rdb.Options().Addrs { |
| 46 | conf.poolName = addr |
| 47 | break |
| 48 | } |
| 49 | case *redis.Ring: |
| 50 | for _, addr := range rdb.Options().Addrs { |
| 51 | conf.poolName = addr |
| 52 | break |
| 53 | } |
| 54 | default: |
| 55 | return fmt.Errorf("redisotel: %T not supported", rdb) |
| 56 | } |
| 57 | } |
| 58 | conf.attrs = append(conf.attrs, attribute.String("pool.name", conf.poolName)) |
| 59 | |
| 60 | var state *metricsState |
| 61 | if conf.closeChan != nil { |
| 62 | state = &metricsState{ |
| 63 | registrations: make([]metric.Registration, 0), |
| 64 | closed: false, |
| 65 | mutex: sync.Mutex{}, |
| 66 | } |
| 67 | |
| 68 | go func() { |
| 69 | <-conf.closeChan |
| 70 | |
| 71 | state.mutex.Lock() |
| 72 | state.closed = true |
| 73 | |
| 74 | for _, registration := range state.registrations { |
| 75 | if err := registration.Unregister(); err != nil { |
| 76 | otel.Handle(err) |
| 77 | } |
| 78 | } |
| 79 | state.mutex.Unlock() |
| 80 | }() |
| 81 | } |
| 82 | |
| 83 | switch rdb := rdb.(type) { |
| 84 | case *redis.Client: |
nothing calls this directly
no test coverage detected