mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-18 10:57:35 -06:00
[chore] Simplify the OTEL setup (#4110)
# Description This simplifies our OTEL setup by: * Getting rid of some deprecated things. * Using `autoexport` and letting things get configured by the `OTEL_` environment variables. * Removing all the unnecessary config options. ## Checklist Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]` If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want). - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [x] I/we have performed a self-review of added code. - [x] I/we have written code that is legible and maintainable by others. - [ ] I/we have commented the added code, particularly in hard-to-understand areas. - [x] I/we have made any necessary changes to documentation. - [ ] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [x] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4110 Reviewed-by: tobi <kipvandenbos@noreply.codeberg.org> Co-authored-by: Daenney <daenney@noreply.codeberg.org> Co-committed-by: Daenney <daenney@noreply.codeberg.org>
This commit is contained in:
parent
ad71066973
commit
ecbdc4227b
145 changed files with 21740 additions and 1319 deletions
201
vendor/go.opentelemetry.io/contrib/exporters/autoexport/LICENSE
generated
vendored
Normal file
201
vendor/go.opentelemetry.io/contrib/exporters/autoexport/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
7
vendor/go.opentelemetry.io/contrib/exporters/autoexport/doc.go
generated
vendored
Normal file
7
vendor/go.opentelemetry.io/contrib/exporters/autoexport/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package autoexport provides OpenTelemetry exporter factory functions
|
||||
// with defaults and environment variable support as defined by the
|
||||
// OpenTelemetry specification.
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
93
vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go
generated
vendored
Normal file
93
vendor/go.opentelemetry.io/contrib/exporters/autoexport/logs.go
generated
vendored
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
|
||||
"go.opentelemetry.io/otel/sdk/log"
|
||||
)
|
||||
|
||||
const otelExporterOTLPLogsProtoEnvKey = "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL"
|
||||
|
||||
// LogOption applies an autoexport configuration option.
|
||||
type LogOption = option[log.Exporter]
|
||||
|
||||
var logsSignal = newSignal[log.Exporter]("OTEL_LOGS_EXPORTER")
|
||||
|
||||
// WithFallbackLogExporter sets the fallback exporter to use when no exporter
|
||||
// is configured through the OTEL_LOGS_EXPORTER environment variable.
|
||||
func WithFallbackLogExporter(logExporterFactory func(ctx context.Context) (log.Exporter, error)) LogOption {
|
||||
return withFallbackFactory[log.Exporter](logExporterFactory)
|
||||
}
|
||||
|
||||
// NewLogExporter returns a configured [go.opentelemetry.io/otel/sdk/log.Exporter]
|
||||
// defined using the environment variables described below.
|
||||
//
|
||||
// OTEL_LOGS_EXPORTER defines the logs exporter; supported values:
|
||||
// - "none" - "no operation" exporter
|
||||
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlplog]
|
||||
// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutlog]
|
||||
//
|
||||
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
|
||||
// supported values:
|
||||
// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection;
|
||||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp]
|
||||
// - "grpc" - gRPC with protobuf-encoded data over HTTP/2 connection;
|
||||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc]
|
||||
//
|
||||
// OTEL_EXPORTER_OTLP_LOGS_PROTOCOL defines OTLP exporter's transport protocol for the logs signal;
|
||||
// supported values are the same as OTEL_EXPORTER_OTLP_PROTOCOL.
|
||||
//
|
||||
// An error is returned if an environment value is set to an unhandled value.
|
||||
//
|
||||
// Use [RegisterLogExporter] to handle more values of OTEL_LOGS_EXPORTER.
|
||||
//
|
||||
// Use [WithFallbackLogExporter] option to change the returned exporter
|
||||
// when OTEL_LOGS_EXPORTER is unset or empty.
|
||||
//
|
||||
// Use [IsNoneLogExporter] to check if the returned exporter is a "no operation" exporter.
|
||||
func NewLogExporter(ctx context.Context, opts ...LogOption) (log.Exporter, error) {
|
||||
return logsSignal.create(ctx, opts...)
|
||||
}
|
||||
|
||||
// RegisterLogExporter sets the log.Exporter factory to be used when the
|
||||
// OTEL_LOGS_EXPORTER environment variable contains the exporter name.
|
||||
// This will panic if name has already been registered.
|
||||
func RegisterLogExporter(name string, factory func(context.Context) (log.Exporter, error)) {
|
||||
must(logsSignal.registry.store(name, factory))
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterLogExporter("otlp", func(ctx context.Context) (log.Exporter, error) {
|
||||
proto := os.Getenv(otelExporterOTLPLogsProtoEnvKey)
|
||||
if proto == "" {
|
||||
proto = os.Getenv(otelExporterOTLPProtoEnvKey)
|
||||
}
|
||||
|
||||
// Fallback to default, http/protobuf.
|
||||
if proto == "" {
|
||||
proto = "http/protobuf"
|
||||
}
|
||||
|
||||
switch proto {
|
||||
case "grpc":
|
||||
return otlploggrpc.New(ctx)
|
||||
case "http/protobuf":
|
||||
return otlploghttp.New(ctx)
|
||||
default:
|
||||
return nil, errInvalidOTLPProtocol
|
||||
}
|
||||
})
|
||||
RegisterLogExporter("console", func(ctx context.Context) (log.Exporter, error) {
|
||||
return stdoutlog.New()
|
||||
})
|
||||
RegisterLogExporter("none", func(ctx context.Context) (log.Exporter, error) {
|
||||
return noopLogExporter{}, nil
|
||||
})
|
||||
}
|
||||
299
vendor/go.opentelemetry.io/contrib/exporters/autoexport/metrics.go
generated
vendored
Normal file
299
vendor/go.opentelemetry.io/contrib/exporters/autoexport/metrics.go
generated
vendored
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
prometheusbridge "go.opentelemetry.io/contrib/bridges/prometheus"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
|
||||
promexporter "go.opentelemetry.io/otel/exporters/prometheus"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
|
||||
"go.opentelemetry.io/otel/sdk/metric"
|
||||
)
|
||||
|
||||
const otelExporterOTLPMetricsProtoEnvKey = "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL"
|
||||
|
||||
// MetricOption applies an autoexport configuration option.
|
||||
type MetricOption = option[metric.Reader]
|
||||
|
||||
// WithFallbackMetricReader sets the fallback exporter to use when no exporter
|
||||
// is configured through the OTEL_METRICS_EXPORTER environment variable.
|
||||
func WithFallbackMetricReader(metricReaderFactory func(ctx context.Context) (metric.Reader, error)) MetricOption {
|
||||
return withFallbackFactory[metric.Reader](metricReaderFactory)
|
||||
}
|
||||
|
||||
// NewMetricReader returns a configured [go.opentelemetry.io/otel/sdk/metric.Reader]
|
||||
// defined using the environment variables described below.
|
||||
//
|
||||
// OTEL_METRICS_EXPORTER defines the metrics exporter; supported values:
|
||||
// - "none" - "no operation" exporter
|
||||
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlpmetric]
|
||||
// - "prometheus" - Prometheus exporter + HTTP server; see [go.opentelemetry.io/otel/exporters/prometheus]
|
||||
// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutmetric]
|
||||
//
|
||||
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
|
||||
// supported values:
|
||||
// - "grpc" - protobuf-encoded data using gRPC wire format over HTTP/2 connection;
|
||||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc]
|
||||
// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection;
|
||||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp]
|
||||
//
|
||||
// OTEL_EXPORTER_OTLP_METRICS_PROTOCOL defines OTLP exporter's transport protocol for the metrics signal;
|
||||
// supported values are the same as OTEL_EXPORTER_OTLP_PROTOCOL.
|
||||
//
|
||||
// OTEL_EXPORTER_PROMETHEUS_HOST (defaulting to "localhost") and
|
||||
// OTEL_EXPORTER_PROMETHEUS_PORT (defaulting to 9464) define the host and port for the
|
||||
// Prometheus exporter's HTTP server.
|
||||
//
|
||||
// Experimental: OTEL_METRICS_PRODUCERS can be used to configure metric producers.
|
||||
// supported values: prometheus, none. Multiple values can be specified separated by commas.
|
||||
//
|
||||
// An error is returned if an environment value is set to an unhandled value.
|
||||
//
|
||||
// Use [RegisterMetricReader] to handle more values of OTEL_METRICS_EXPORTER.
|
||||
// Use [RegisterMetricProducer] to handle more values of OTEL_METRICS_PRODUCERS.
|
||||
//
|
||||
// Use [WithFallbackMetricReader] option to change the returned exporter
|
||||
// when OTEL_METRICS_EXPORTER is unset or empty.
|
||||
//
|
||||
// Use [IsNoneMetricReader] to check if the returned exporter is a "no operation" exporter.
|
||||
func NewMetricReader(ctx context.Context, opts ...MetricOption) (metric.Reader, error) {
|
||||
return metricsSignal.create(ctx, opts...)
|
||||
}
|
||||
|
||||
// RegisterMetricReader sets the MetricReader factory to be used when the
|
||||
// OTEL_METRICS_EXPORTERS environment variable contains the exporter name. This
|
||||
// will panic if name has already been registered.
|
||||
func RegisterMetricReader(name string, factory func(context.Context) (metric.Reader, error)) {
|
||||
must(metricsSignal.registry.store(name, factory))
|
||||
}
|
||||
|
||||
// RegisterMetricProducer sets the MetricReader factory to be used when the
|
||||
// OTEL_METRICS_PRODUCERS environment variable contains the producer name. This
|
||||
// will panic if name has already been registered.
|
||||
func RegisterMetricProducer(name string, factory func(context.Context) (metric.Producer, error)) {
|
||||
must(metricsProducers.registry.store(name, factory))
|
||||
}
|
||||
|
||||
// WithFallbackMetricProducer sets the fallback producer to use when no producer
|
||||
// is configured through the OTEL_METRICS_PRODUCERS environment variable.
|
||||
func WithFallbackMetricProducer(producerFactory func(ctx context.Context) (metric.Producer, error)) {
|
||||
metricsProducers.fallbackProducer = producerFactory
|
||||
}
|
||||
|
||||
var (
|
||||
metricsSignal = newSignal[metric.Reader]("OTEL_METRICS_EXPORTER")
|
||||
metricsProducers = newProducerRegistry("OTEL_METRICS_PRODUCERS")
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterMetricReader("otlp", func(ctx context.Context) (metric.Reader, error) {
|
||||
producers, err := metricsProducers.create(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
readerOpts := []metric.PeriodicReaderOption{}
|
||||
for _, producer := range producers {
|
||||
readerOpts = append(readerOpts, metric.WithProducer(producer))
|
||||
}
|
||||
|
||||
proto := os.Getenv(otelExporterOTLPMetricsProtoEnvKey)
|
||||
if proto == "" {
|
||||
proto = os.Getenv(otelExporterOTLPProtoEnvKey)
|
||||
}
|
||||
|
||||
// Fallback to default, http/protobuf.
|
||||
if proto == "" {
|
||||
proto = "http/protobuf"
|
||||
}
|
||||
|
||||
switch proto {
|
||||
case "grpc":
|
||||
r, err := otlpmetricgrpc.New(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return metric.NewPeriodicReader(r, readerOpts...), nil
|
||||
case "http/protobuf":
|
||||
r, err := otlpmetrichttp.New(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return metric.NewPeriodicReader(r, readerOpts...), nil
|
||||
default:
|
||||
return nil, errInvalidOTLPProtocol
|
||||
}
|
||||
})
|
||||
RegisterMetricReader("console", func(ctx context.Context) (metric.Reader, error) {
|
||||
producers, err := metricsProducers.create(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
readerOpts := []metric.PeriodicReaderOption{}
|
||||
for _, producer := range producers {
|
||||
readerOpts = append(readerOpts, metric.WithProducer(producer))
|
||||
}
|
||||
|
||||
r, err := stdoutmetric.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return metric.NewPeriodicReader(r, readerOpts...), nil
|
||||
})
|
||||
RegisterMetricReader("none", func(ctx context.Context) (metric.Reader, error) {
|
||||
return newNoopMetricReader(), nil
|
||||
})
|
||||
RegisterMetricReader("prometheus", func(ctx context.Context) (metric.Reader, error) {
|
||||
// create an isolated registry instead of using the global registry --
|
||||
// the user might not want to mix OTel with non-OTel metrics.
|
||||
// Those that want to comingle metrics from global registry can use
|
||||
// OTEL_METRICS_PRODUCERS=prometheus
|
||||
reg := prometheus.NewRegistry()
|
||||
|
||||
exporterOpts := []promexporter.Option{promexporter.WithRegisterer(reg)}
|
||||
|
||||
producers, err := metricsProducers.create(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, producer := range producers {
|
||||
exporterOpts = append(exporterOpts, promexporter.WithProducer(producer))
|
||||
}
|
||||
|
||||
reader, err := promexporter.New(exporterOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
|
||||
server := http.Server{
|
||||
// Timeouts are necessary to make a server resilient to attacks, but ListenAndServe doesn't set any.
|
||||
// We use values from this example: https://blog.cloudflare.com/exposing-go-on-the-internet/#:~:text=There%20are%20three%20main%20timeouts
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
// environment variable names and defaults specified at https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#prometheus-exporter
|
||||
host := getenv("OTEL_EXPORTER_PROMETHEUS_HOST", "localhost")
|
||||
port := getenv("OTEL_EXPORTER_PROMETHEUS_PORT", "9464")
|
||||
addr := host + ":" + port
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, errors.Join(
|
||||
fmt.Errorf("binding address %s for Prometheus exporter: %w", addr, err),
|
||||
reader.Shutdown(ctx),
|
||||
)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := server.Serve(lis); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
otel.Handle(fmt.Errorf("the Prometheus HTTP server exited unexpectedly: %w", err))
|
||||
}
|
||||
}()
|
||||
|
||||
return readerWithServer{lis.Addr(), reader, &server}, nil
|
||||
})
|
||||
|
||||
RegisterMetricProducer("prometheus", func(ctx context.Context) (metric.Producer, error) {
|
||||
return prometheusbridge.NewMetricProducer(), nil
|
||||
})
|
||||
RegisterMetricProducer("none", func(ctx context.Context) (metric.Producer, error) {
|
||||
return newNoopMetricProducer(), nil
|
||||
})
|
||||
}
|
||||
|
||||
type readerWithServer struct {
|
||||
addr net.Addr
|
||||
metric.Reader
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func (rws readerWithServer) Shutdown(ctx context.Context) error {
|
||||
return errors.Join(
|
||||
rws.Reader.Shutdown(ctx),
|
||||
rws.server.Shutdown(ctx),
|
||||
)
|
||||
}
|
||||
|
||||
func getenv(key, fallback string) string {
|
||||
result, ok := os.LookupEnv(key)
|
||||
if !ok {
|
||||
return fallback
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type producerRegistry struct {
|
||||
envKey string
|
||||
fallbackProducer func(context.Context) (metric.Producer, error)
|
||||
registry *registry[metric.Producer]
|
||||
}
|
||||
|
||||
func newProducerRegistry(envKey string) producerRegistry {
|
||||
return producerRegistry{
|
||||
envKey: envKey,
|
||||
registry: ®istry[metric.Producer]{
|
||||
names: make(map[string]func(context.Context) (metric.Producer, error)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (pr producerRegistry) create(ctx context.Context) ([]metric.Producer, error) {
|
||||
expType := os.Getenv(pr.envKey)
|
||||
if expType == "" {
|
||||
if pr.fallbackProducer != nil {
|
||||
producer, err := pr.fallbackProducer(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []metric.Producer{producer}, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
producers := dedupedMetricProducers(expType)
|
||||
metricProducers := make([]metric.Producer, 0, len(producers))
|
||||
for _, producer := range producers {
|
||||
producer, err := pr.registry.load(ctx, producer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
metricProducers = append(metricProducers, producer)
|
||||
}
|
||||
|
||||
return metricProducers, nil
|
||||
}
|
||||
|
||||
func dedupedMetricProducers(envValue string) []string {
|
||||
producers := make(map[string]struct{})
|
||||
for _, producer := range strings.Split(envValue, ",") {
|
||||
producers[producer] = struct{}{}
|
||||
}
|
||||
|
||||
result := make([]string, 0, len(producers))
|
||||
for producer := range producers {
|
||||
result = append(result, producer)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
87
vendor/go.opentelemetry.io/contrib/exporters/autoexport/noop.go
generated
vendored
Normal file
87
vendor/go.opentelemetry.io/contrib/exporters/autoexport/noop.go
generated
vendored
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/sdk/log"
|
||||
"go.opentelemetry.io/otel/sdk/metric"
|
||||
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
// noopSpanExporter is an implementation of trace.SpanExporter that performs no operations.
|
||||
type noopSpanExporter struct{}
|
||||
|
||||
var _ trace.SpanExporter = noopSpanExporter{}
|
||||
|
||||
// ExportSpans is part of trace.SpanExporter interface.
|
||||
func (e noopSpanExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown is part of trace.SpanExporter interface.
|
||||
func (e noopSpanExporter) Shutdown(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsNoneSpanExporter returns true for the exporter returned by [NewSpanExporter]
|
||||
// when OTEL_TRACES_EXPORTER environment variable is set to "none".
|
||||
func IsNoneSpanExporter(e trace.SpanExporter) bool {
|
||||
_, ok := e.(noopSpanExporter)
|
||||
return ok
|
||||
}
|
||||
|
||||
type noopMetricReader struct {
|
||||
*metric.ManualReader
|
||||
}
|
||||
|
||||
func newNoopMetricReader() noopMetricReader {
|
||||
return noopMetricReader{metric.NewManualReader()}
|
||||
}
|
||||
|
||||
// IsNoneMetricReader returns true for the exporter returned by [NewMetricReader]
|
||||
// when OTEL_METRICS_EXPORTER environment variable is set to "none".
|
||||
func IsNoneMetricReader(e metric.Reader) bool {
|
||||
_, ok := e.(noopMetricReader)
|
||||
return ok
|
||||
}
|
||||
|
||||
type noopMetricProducer struct{}
|
||||
|
||||
func (e noopMetricProducer) Produce(ctx context.Context) ([]metricdata.ScopeMetrics, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func newNoopMetricProducer() noopMetricProducer {
|
||||
return noopMetricProducer{}
|
||||
}
|
||||
|
||||
// noopLogExporter is an implementation of log.SpanExporter that performs no operations.
|
||||
type noopLogExporter struct{}
|
||||
|
||||
var _ log.Exporter = noopLogExporter{}
|
||||
|
||||
// ExportSpans is part of log.Exporter interface.
|
||||
func (e noopLogExporter) Export(ctx context.Context, records []log.Record) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown is part of log.Exporter interface.
|
||||
func (e noopLogExporter) Shutdown(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ForceFlush is part of log.Exporter interface.
|
||||
func (e noopLogExporter) ForceFlush(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsNoneLogExporter returns true for the exporter returned by [NewLogExporter]
|
||||
// when OTEL_LOGSS_EXPORTER environment variable is set to "none".
|
||||
func IsNoneLogExporter(e log.Exporter) bool {
|
||||
_, ok := e.(noopLogExporter)
|
||||
return ok
|
||||
}
|
||||
67
vendor/go.opentelemetry.io/contrib/exporters/autoexport/registry.go
generated
vendored
Normal file
67
vendor/go.opentelemetry.io/contrib/exporters/autoexport/registry.go
generated
vendored
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const otelExporterOTLPProtoEnvKey = "OTEL_EXPORTER_OTLP_PROTOCOL"
|
||||
|
||||
// registry maintains a map of exporter names to exporter factories
|
||||
// func(context.Context) (T, error) that is safe for concurrent use by multiple
|
||||
// goroutines without additional locking or coordination.
|
||||
type registry[T any] struct {
|
||||
mu sync.Mutex
|
||||
names map[string]func(context.Context) (T, error)
|
||||
}
|
||||
|
||||
var (
|
||||
// errUnknownExporterProducer is returned when an unknown exporter name is used in
|
||||
// the OTEL_*_EXPORTER or OTEL_METRICS_PRODUCERS environment variables.
|
||||
errUnknownExporterProducer = errors.New("unknown exporter or metrics producer")
|
||||
|
||||
// errInvalidOTLPProtocol is returned when an invalid protocol is used in
|
||||
// the OTEL_EXPORTER_OTLP_PROTOCOL environment variable.
|
||||
errInvalidOTLPProtocol = errors.New("invalid OTLP protocol - should be one of ['grpc', 'http/protobuf']")
|
||||
|
||||
// errDuplicateRegistration is returned when an duplicate registration is detected.
|
||||
errDuplicateRegistration = errors.New("duplicate registration")
|
||||
)
|
||||
|
||||
// load returns tries to find the exporter factory with the key and
|
||||
// then execute the factory, returning the created SpanExporter.
|
||||
// errUnknownExporterProducer is returned if the registration is missing and the error from
|
||||
// executing the factory if not nil.
|
||||
func (r *registry[T]) load(ctx context.Context, key string) (T, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
factory, ok := r.names[key]
|
||||
if !ok {
|
||||
var zero T
|
||||
return zero, errUnknownExporterProducer
|
||||
}
|
||||
return factory(ctx)
|
||||
}
|
||||
|
||||
// store sets the factory for a key if is not already in the registry. errDuplicateRegistration
|
||||
// is returned if the registry already contains key.
|
||||
func (r *registry[T]) store(key string, factory func(context.Context) (T, error)) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if _, ok := r.names[key]; ok {
|
||||
return fmt.Errorf("%w: %q", errDuplicateRegistration, key)
|
||||
}
|
||||
r.names[key] = factory
|
||||
return nil
|
||||
}
|
||||
|
||||
func must(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
61
vendor/go.opentelemetry.io/contrib/exporters/autoexport/signal.go
generated
vendored
Normal file
61
vendor/go.opentelemetry.io/contrib/exporters/autoexport/signal.go
generated
vendored
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
)
|
||||
|
||||
type signal[T any] struct {
|
||||
envKey string
|
||||
registry *registry[T]
|
||||
}
|
||||
|
||||
func newSignal[T any](envKey string) signal[T] {
|
||||
return signal[T]{
|
||||
envKey: envKey,
|
||||
registry: ®istry[T]{
|
||||
names: make(map[string]func(context.Context) (T, error)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s signal[T]) create(ctx context.Context, opts ...option[T]) (T, error) {
|
||||
var cfg config[T]
|
||||
for _, opt := range opts {
|
||||
opt.apply(&cfg)
|
||||
}
|
||||
|
||||
expType := os.Getenv(s.envKey)
|
||||
if expType == "" {
|
||||
if cfg.fallbackFactory != nil {
|
||||
return cfg.fallbackFactory(ctx)
|
||||
}
|
||||
expType = "otlp"
|
||||
}
|
||||
|
||||
return s.registry.load(ctx, expType)
|
||||
}
|
||||
|
||||
type config[T any] struct {
|
||||
fallbackFactory func(ctx context.Context) (T, error)
|
||||
}
|
||||
|
||||
type option[T any] interface {
|
||||
apply(cfg *config[T])
|
||||
}
|
||||
|
||||
type optionFunc[T any] func(cfg *config[T])
|
||||
|
||||
//lint:ignore U1000 https://github.com/dominikh/go-tools/issues/1440
|
||||
func (fn optionFunc[T]) apply(cfg *config[T]) {
|
||||
fn(cfg)
|
||||
}
|
||||
|
||||
func withFallbackFactory[T any](fallbackFactory func(ctx context.Context) (T, error)) option[T] {
|
||||
return optionFunc[T](func(cfg *config[T]) {
|
||||
cfg.fallbackFactory = fallbackFactory
|
||||
})
|
||||
}
|
||||
98
vendor/go.opentelemetry.io/contrib/exporters/autoexport/spans.go
generated
vendored
Normal file
98
vendor/go.opentelemetry.io/contrib/exporters/autoexport/spans.go
generated
vendored
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
const otelExporterOTLPTracesProtoEnvKey = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
|
||||
|
||||
// SpanOption applies an autoexport configuration option.
|
||||
type SpanOption = option[trace.SpanExporter]
|
||||
|
||||
// Option applies an autoexport configuration option.
|
||||
//
|
||||
// Deprecated: Use SpanOption.
|
||||
type Option = SpanOption
|
||||
|
||||
// WithFallbackSpanExporter sets the fallback exporter to use when no exporter
|
||||
// is configured through the OTEL_TRACES_EXPORTER environment variable.
|
||||
func WithFallbackSpanExporter(spanExporterFactory func(ctx context.Context) (trace.SpanExporter, error)) SpanOption {
|
||||
return withFallbackFactory[trace.SpanExporter](spanExporterFactory)
|
||||
}
|
||||
|
||||
// NewSpanExporter returns a configured [go.opentelemetry.io/otel/sdk/trace.SpanExporter]
|
||||
// defined using the environment variables described below.
|
||||
//
|
||||
// OTEL_TRACES_EXPORTER defines the traces exporter; supported values:
|
||||
// - "none" - "no operation" exporter
|
||||
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlptrace]
|
||||
// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdouttrace]
|
||||
//
|
||||
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
|
||||
// supported values:
|
||||
// - "grpc" - protobuf-encoded data using gRPC wire format over HTTP/2 connection;
|
||||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc]
|
||||
// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection;
|
||||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp]
|
||||
//
|
||||
// OTEL_EXPORTER_OTLP_TRACES_PROTOCOL defines OTLP exporter's transport protocol for the traces signal;
|
||||
// supported values are the same as OTEL_EXPORTER_OTLP_PROTOCOL.
|
||||
//
|
||||
// An error is returned if an environment value is set to an unhandled value.
|
||||
//
|
||||
// Use [RegisterSpanExporter] to handle more values of OTEL_TRACES_EXPORTER.
|
||||
//
|
||||
// Use [WithFallbackSpanExporter] option to change the returned exporter
|
||||
// when OTEL_TRACES_EXPORTER is unset or empty.
|
||||
//
|
||||
// Use [IsNoneSpanExporter] to check if the returned exporter is a "no operation" exporter.
|
||||
func NewSpanExporter(ctx context.Context, opts ...SpanOption) (trace.SpanExporter, error) {
|
||||
return tracesSignal.create(ctx, opts...)
|
||||
}
|
||||
|
||||
// RegisterSpanExporter sets the SpanExporter factory to be used when the
|
||||
// OTEL_TRACES_EXPORTER environment variable contains the exporter name. This
|
||||
// will panic if name has already been registered.
|
||||
func RegisterSpanExporter(name string, factory func(context.Context) (trace.SpanExporter, error)) {
|
||||
must(tracesSignal.registry.store(name, factory))
|
||||
}
|
||||
|
||||
var tracesSignal = newSignal[trace.SpanExporter]("OTEL_TRACES_EXPORTER")
|
||||
|
||||
func init() {
|
||||
RegisterSpanExporter("otlp", func(ctx context.Context) (trace.SpanExporter, error) {
|
||||
proto := os.Getenv(otelExporterOTLPTracesProtoEnvKey)
|
||||
if proto == "" {
|
||||
proto = os.Getenv(otelExporterOTLPProtoEnvKey)
|
||||
}
|
||||
|
||||
// Fallback to default, http/protobuf.
|
||||
if proto == "" {
|
||||
proto = "http/protobuf"
|
||||
}
|
||||
|
||||
switch proto {
|
||||
case "grpc":
|
||||
return otlptracegrpc.New(ctx)
|
||||
case "http/protobuf":
|
||||
return otlptracehttp.New(ctx)
|
||||
default:
|
||||
return nil, errInvalidOTLPProtocol
|
||||
}
|
||||
})
|
||||
RegisterSpanExporter("console", func(ctx context.Context) (trace.SpanExporter, error) {
|
||||
return stdouttrace.New()
|
||||
})
|
||||
RegisterSpanExporter("none", func(ctx context.Context) (trace.SpanExporter, error) {
|
||||
return noopSpanExporter{}, nil
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue