|
4 | 4 | package config // import "go.opentelemetry.io/contrib/config"
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "net" |
| 12 | + "net/http" |
| 13 | + "net/url" |
| 14 | + "os" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/prometheus/client_golang/prometheus" |
| 18 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 19 | + |
| 20 | + "go.opentelemetry.io/otel" |
| 21 | + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" |
| 22 | + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 23 | + otelprom "go.opentelemetry.io/otel/exporters/prometheus" |
| 24 | + "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
7 | 25 | "go.opentelemetry.io/otel/metric"
|
8 | 26 | "go.opentelemetry.io/otel/metric/noop"
|
9 | 27 | sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
| 28 | + "go.opentelemetry.io/otel/sdk/resource" |
10 | 29 | )
|
11 | 30 |
|
12 |
| -func initMeterProvider(cfg configOptions) (metric.MeterProvider, shutdownFunc) { |
| 31 | +func meterProvider(cfg configOptions, res *resource.Resource) (metric.MeterProvider, shutdownFunc, error) { |
13 | 32 | if cfg.opentelemetryConfig.MeterProvider == nil {
|
14 |
| - return noop.NewMeterProvider(), noopShutdown |
| 33 | + return noop.NewMeterProvider(), noopShutdown, nil |
| 34 | + } |
| 35 | + opts := []sdkmetric.Option{ |
| 36 | + sdkmetric.WithResource(res), |
| 37 | + } |
| 38 | + |
| 39 | + var errs []error |
| 40 | + for _, reader := range cfg.opentelemetryConfig.MeterProvider.Readers { |
| 41 | + r, err := metricReader(cfg.ctx, reader) |
| 42 | + if err == nil { |
| 43 | + opts = append(opts, sdkmetric.WithReader(r)) |
| 44 | + } else { |
| 45 | + errs = append(errs, err) |
| 46 | + } |
| 47 | + } |
| 48 | + if len(errs) > 0 { |
| 49 | + return noop.NewMeterProvider(), noopShutdown, errors.Join(errs...) |
| 50 | + } |
| 51 | + |
| 52 | + mp := sdkmetric.NewMeterProvider(opts...) |
| 53 | + return mp, mp.Shutdown, nil |
| 54 | +} |
| 55 | + |
| 56 | +func metricReader(ctx context.Context, r MetricReader) (sdkmetric.Reader, error) { |
| 57 | + if r.Periodic != nil && r.Pull != nil { |
| 58 | + return nil, errors.New("must not specify multiple metric reader type") |
| 59 | + } |
| 60 | + |
| 61 | + if r.Periodic != nil { |
| 62 | + return periodicExporter(ctx, r.Periodic.Exporter) |
| 63 | + } |
| 64 | + |
| 65 | + if r.Pull != nil { |
| 66 | + return pullReader(ctx, r.Pull.Exporter) |
| 67 | + } |
| 68 | + return nil, errors.New("no valid metric reader") |
| 69 | +} |
| 70 | + |
| 71 | +func pullReader(ctx context.Context, exporter MetricExporter) (sdkmetric.Reader, error) { |
| 72 | + if exporter.Prometheus != nil { |
| 73 | + return prometheusReader(ctx, exporter.Prometheus) |
| 74 | + } |
| 75 | + return nil, errors.New("no valid metric exporter") |
| 76 | +} |
| 77 | + |
| 78 | +func periodicExporter(ctx context.Context, exporter MetricExporter, opts ...sdkmetric.PeriodicReaderOption) (sdkmetric.Reader, error) { |
| 79 | + if exporter.Console != nil && exporter.OTLP != nil { |
| 80 | + return nil, errors.New("must not specify multiple exporters") |
| 81 | + } |
| 82 | + if exporter.Console != nil { |
| 83 | + enc := json.NewEncoder(os.Stdout) |
| 84 | + enc.SetIndent("", " ") |
| 85 | + |
| 86 | + exp, err := stdoutmetric.New( |
| 87 | + stdoutmetric.WithEncoder(enc), |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + return sdkmetric.NewPeriodicReader(exp, opts...), nil |
| 93 | + } |
| 94 | + if exporter.OTLP != nil { |
| 95 | + var err error |
| 96 | + var exp sdkmetric.Exporter |
| 97 | + switch exporter.OTLP.Protocol { |
| 98 | + case protocolProtobufHTTP: |
| 99 | + exp, err = otlpHTTPMetricExporter(ctx, exporter.OTLP) |
| 100 | + case protocolProtobufGRPC: |
| 101 | + exp, err = otlpGRPCMetricExporter(ctx, exporter.OTLP) |
| 102 | + default: |
| 103 | + return nil, fmt.Errorf("unsupported protocol %q", exporter.OTLP.Protocol) |
| 104 | + } |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + return sdkmetric.NewPeriodicReader(exp, opts...), nil |
| 109 | + } |
| 110 | + return nil, errors.New("no valid metric exporter") |
| 111 | +} |
| 112 | + |
| 113 | +func otlpHTTPMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmetric.Exporter, error) { |
| 114 | + opts := []otlpmetrichttp.Option{} |
| 115 | + |
| 116 | + if len(otlpConfig.Endpoint) > 0 { |
| 117 | + u, err := url.ParseRequestURI(otlpConfig.Endpoint) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + opts = append(opts, otlpmetrichttp.WithEndpoint(u.Host)) |
| 122 | + |
| 123 | + if u.Scheme == "http" { |
| 124 | + opts = append(opts, otlpmetrichttp.WithInsecure()) |
| 125 | + } |
| 126 | + if len(u.Path) > 0 { |
| 127 | + opts = append(opts, otlpmetrichttp.WithURLPath(u.Path)) |
| 128 | + } |
| 129 | + } |
| 130 | + if otlpConfig.Compression != nil { |
| 131 | + switch *otlpConfig.Compression { |
| 132 | + case compressionGzip: |
| 133 | + opts = append(opts, otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression)) |
| 134 | + case compressionNone: |
| 135 | + opts = append(opts, otlpmetrichttp.WithCompression(otlpmetrichttp.NoCompression)) |
| 136 | + default: |
| 137 | + return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression) |
| 138 | + } |
| 139 | + } |
| 140 | + if otlpConfig.Timeout != nil { |
| 141 | + opts = append(opts, otlpmetrichttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
15 | 142 | }
|
16 |
| - mp := sdkmetric.NewMeterProvider() |
17 |
| - return mp, mp.Shutdown |
| 143 | + if len(otlpConfig.Headers) > 0 { |
| 144 | + opts = append(opts, otlpmetrichttp.WithHeaders(otlpConfig.Headers)) |
| 145 | + } |
| 146 | + |
| 147 | + return otlpmetrichttp.New(ctx, opts...) |
| 148 | +} |
| 149 | + |
| 150 | +func otlpGRPCMetricExporter(ctx context.Context, otlpConfig *OTLPMetric) (sdkmetric.Exporter, error) { |
| 151 | + opts := []otlpmetricgrpc.Option{} |
| 152 | + |
| 153 | + if len(otlpConfig.Endpoint) > 0 { |
| 154 | + u, err := url.ParseRequestURI(otlpConfig.Endpoint) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + // ParseRequestURI leaves the Host field empty when no |
| 159 | + // scheme is specified (i.e. localhost:4317). This check is |
| 160 | + // here to support the case where a user may not specify a |
| 161 | + // scheme. The code does its best effort here by using |
| 162 | + // otlpConfig.Endpoint as-is in that case |
| 163 | + if u.Host != "" { |
| 164 | + opts = append(opts, otlpmetricgrpc.WithEndpoint(u.Host)) |
| 165 | + } else { |
| 166 | + opts = append(opts, otlpmetricgrpc.WithEndpoint(otlpConfig.Endpoint)) |
| 167 | + } |
| 168 | + if u.Scheme == "http" { |
| 169 | + opts = append(opts, otlpmetricgrpc.WithInsecure()) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if otlpConfig.Compression != nil { |
| 174 | + switch *otlpConfig.Compression { |
| 175 | + case compressionGzip: |
| 176 | + opts = append(opts, otlpmetricgrpc.WithCompressor(*otlpConfig.Compression)) |
| 177 | + case compressionNone: |
| 178 | + // none requires no options |
| 179 | + default: |
| 180 | + return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression) |
| 181 | + } |
| 182 | + } |
| 183 | + if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 184 | + opts = append(opts, otlpmetricgrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 185 | + } |
| 186 | + if len(otlpConfig.Headers) > 0 { |
| 187 | + opts = append(opts, otlpmetricgrpc.WithHeaders(otlpConfig.Headers)) |
| 188 | + } |
| 189 | + |
| 190 | + return otlpmetricgrpc.New(ctx, opts...) |
| 191 | +} |
| 192 | + |
| 193 | +func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmetric.Reader, error) { |
| 194 | + var opts []otelprom.Option |
| 195 | + if prometheusConfig.Host == nil { |
| 196 | + return nil, fmt.Errorf("host must be specified") |
| 197 | + } |
| 198 | + if prometheusConfig.Port == nil { |
| 199 | + return nil, fmt.Errorf("port must be specified") |
| 200 | + } |
| 201 | + if prometheusConfig.WithoutScopeInfo != nil && *prometheusConfig.WithoutScopeInfo { |
| 202 | + opts = append(opts, otelprom.WithoutScopeInfo()) |
| 203 | + } |
| 204 | + if prometheusConfig.WithoutTypeSuffix != nil && *prometheusConfig.WithoutTypeSuffix { |
| 205 | + opts = append(opts, otelprom.WithoutCounterSuffixes()) |
| 206 | + } |
| 207 | + if prometheusConfig.WithoutUnits != nil && *prometheusConfig.WithoutUnits { |
| 208 | + opts = append(opts, otelprom.WithoutUnits()) |
| 209 | + } |
| 210 | + |
| 211 | + reg := prometheus.NewRegistry() |
| 212 | + opts = append(opts, otelprom.WithRegisterer(reg)) |
| 213 | + |
| 214 | + mux := http.NewServeMux() |
| 215 | + mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg})) |
| 216 | + server := http.Server{ |
| 217 | + // Timeouts are necessary to make a server resilent to attacks, but ListenAndServe doesn't set any. |
| 218 | + // We use values from this example: https://blog.cloudflare.com/exposing-go-on-the-internet/#:~:text=There%20are%20three%20main%20timeouts |
| 219 | + ReadTimeout: 5 * time.Second, |
| 220 | + WriteTimeout: 10 * time.Second, |
| 221 | + IdleTimeout: 120 * time.Second, |
| 222 | + Handler: mux, |
| 223 | + } |
| 224 | + addr := fmt.Sprintf("%s:%d", *prometheusConfig.Host, *prometheusConfig.Port) |
| 225 | + |
| 226 | + // TODO: add support for constant label filter |
| 227 | + // otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()), |
| 228 | + // ) |
| 229 | + reader, err := otelprom.New(opts...) |
| 230 | + if err != nil { |
| 231 | + return nil, fmt.Errorf("error creating otel prometheus exporter: %w", err) |
| 232 | + } |
| 233 | + lis, err := net.Listen("tcp", addr) |
| 234 | + if err != nil { |
| 235 | + return nil, errors.Join( |
| 236 | + fmt.Errorf("binding address %s for Prometheus exporter: %w", addr, err), |
| 237 | + reader.Shutdown(ctx), |
| 238 | + ) |
| 239 | + } |
| 240 | + |
| 241 | + go func() { |
| 242 | + if err := server.Serve(lis); err != nil && err != http.ErrServerClosed { |
| 243 | + otel.Handle(fmt.Errorf("the Prometheus HTTP server exited unexpectedly: %w", err)) |
| 244 | + } |
| 245 | + }() |
| 246 | + |
| 247 | + return readerWithServer{reader, &server}, nil |
| 248 | +} |
| 249 | + |
| 250 | +type readerWithServer struct { |
| 251 | + sdkmetric.Reader |
| 252 | + server *http.Server |
| 253 | +} |
| 254 | + |
| 255 | +func (rws readerWithServer) Shutdown(ctx context.Context) error { |
| 256 | + return errors.Join( |
| 257 | + rws.Reader.Shutdown(ctx), |
| 258 | + rws.server.Shutdown(ctx), |
| 259 | + ) |
18 | 260 | }
|
0 commit comments