//
// Copyright The OpenTelemetry Authors
//
// 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.
//
using System;
using OpenTelemetry.Exporter.Prometheus;
using OpenTelemetry.Metrics;
namespace OpenTelemetry.Exporter
{
///
/// Exporter of OpenTelemetry metrics to Prometheus.
///
[ExportModes(ExportModes.Pull)]
public class PrometheusExporter : BaseExporter, IPullMetricExporter
{
internal const string HttpListenerStartFailureExceptionMessage = "PrometheusExporter http listener could not be started.";
internal readonly PrometheusExporterOptions Options;
private readonly PrometheusExporterHttpServer metricsHttpServer;
private Func funcCollect;
private Func, ExportResult> funcExport;
private bool disposed;
///
/// Initializes a new instance of the class.
///
/// Options for the exporter.
public PrometheusExporter(PrometheusExporterOptions options)
{
this.Options = options;
if (options.StartHttpListener)
{
try
{
this.metricsHttpServer = new PrometheusExporterHttpServer(this);
this.metricsHttpServer.Start();
}
catch (Exception ex)
{
throw new InvalidOperationException(HttpListenerStartFailureExceptionMessage, ex);
}
}
this.CollectionManager = new PrometheusCollectionManager(this);
}
public Func Collect
{
get => this.funcCollect;
set => this.funcCollect = value;
}
internal Func, ExportResult> OnExport
{
get => this.funcExport;
set => this.funcExport = value;
}
internal PrometheusCollectionManager CollectionManager { get; }
public override ExportResult Export(in Batch metrics)
{
return this.OnExport(metrics);
}
protected override void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
this.metricsHttpServer?.Dispose();
}
this.disposed = true;
}
base.Dispose(disposing);
}
}
}