Skip to content

Commit

Permalink
Initialize buildInfo directly instead of waiting for createDefaultBui…
Browse files Browse the repository at this point in the history
…ldInfo()
  • Loading branch information
arendjr committed Nov 3, 2023
1 parent e14b3ac commit 1f08774
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
23 changes: 5 additions & 18 deletions packages/autometrics/src/buildInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export type BuildInfo = {
*/
const buildInfo: BuildInfo = {
[AUTOMETRICS_VERSION_LABEL]: "1.0.0",
[BRANCH_LABEL]: "",
[COMMIT_LABEL]: "",
[REPOSITORY_PROVIDER_LABEL]: "",
[REPOSITORY_URL_LABEL]: "",
[VERSION_LABEL]: "",
[BRANCH_LABEL]: getBranch() ?? "",
[COMMIT_LABEL]: getCommit() ?? "",
[REPOSITORY_PROVIDER_LABEL]: getRepositoryProvider() ?? "",
[REPOSITORY_URL_LABEL]: getRepositoryUrl() ?? "",
[VERSION_LABEL]: getVersion() ?? "",
clearmode: "",
};

Expand Down Expand Up @@ -136,16 +136,3 @@ export function recordBuildInfo(info: BuildInfo) {

buildInfoGauge.add(1, buildInfo);
}

/**
* Creates the default `BuildInfo` based on environment variables.
*/
export function createDefaultBuildInfo(): BuildInfo {
return {
[VERSION_LABEL]: getVersion(),
[COMMIT_LABEL]: getCommit(),
[BRANCH_LABEL]: getBranch(),
[REPOSITORY_URL_LABEL]: getRepositoryUrl(),
[REPOSITORY_PROVIDER_LABEL]: getRepositoryProvider(),
};
}
11 changes: 3 additions & 8 deletions packages/autometrics/src/exporter-otlp-http/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {
} from "$otel/exporter-metrics-otlp-http";
import { PeriodicExportingMetricReader } from "$otel/sdk-metrics";

import {
BuildInfo,
amLogger,
createDefaultBuildInfo,
recordBuildInfo,
} from "../../mod.ts";
import { BuildInfo, amLogger, recordBuildInfo } from "../../mod.ts";
import { registerExporterInternal } from "./registerExporterInternal.ts";

export { AggregationTemporalityPreference };
Expand Down Expand Up @@ -75,7 +70,7 @@ export function init({
concurrencyLimit,
timeout = 1000,
temporalityPreference = AggregationTemporalityPreference.CUMULATIVE,
buildInfo,
buildInfo = {},
}: InitOptions) {
const defaultPath = "/v1/metrics" as const;
const defaultPort = "4317" as const;
Expand Down Expand Up @@ -136,5 +131,5 @@ export function init({
throw new Error(`Invalid pushInterval: ${pushInterval}`);
}

recordBuildInfo(buildInfo ?? createDefaultBuildInfo());
recordBuildInfo(buildInfo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { PeriodicExportingMetricReader } from "$otel/sdk-metrics";
import {
BuildInfo,
amLogger,
createDefaultBuildInfo,
recordBuildInfo,
registerExporter,
} from "../../mod.ts";
Expand Down Expand Up @@ -61,7 +60,7 @@ export function init({
pushInterval = 5000,
concurrencyLimit,
timeout = 1000,
buildInfo,
buildInfo = {},
}: InitOptions) {
if (typeof fetch === "undefined") {
amLogger.warn(
Expand Down Expand Up @@ -99,5 +98,5 @@ export function init({
throw new Error(`Invalid pushInterval: ${pushInterval}`);
}

recordBuildInfo(buildInfo ?? createDefaultBuildInfo());
recordBuildInfo(buildInfo);
}
5 changes: 2 additions & 3 deletions packages/autometrics/src/exporter-prometheus/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { MetricReader } from "$otel/sdk-metrics";
import {
BuildInfo,
amLogger,
createDefaultBuildInfo,
recordBuildInfo,
registerExporter,
} from "../../mod.ts";
Expand Down Expand Up @@ -37,7 +36,7 @@ export type InitOptions = {
* Prometheus.
*/
export function init({
buildInfo,
buildInfo = {},
hostname = "0.0.0.0",
port = 9464,
}: InitOptions = {}) {
Expand All @@ -53,7 +52,7 @@ export function init({
metricReader = new PrometheusExporter({ host: hostname, port });
registerExporter({ metricReader });

recordBuildInfo(buildInfo ?? createDefaultBuildInfo());
recordBuildInfo(buildInfo);
}

/**
Expand Down

0 comments on commit 1f08774

Please sign in to comment.