Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on a potentially incorrect HTTP OTLP endpoint #126

Merged
merged 10 commits into from
Sep 25, 2023
3 changes: 1 addition & 2 deletions examples/express/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib"
}

2 changes: 1 addition & 1 deletion examples/express/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const API_SLO: Objective = {
latency: [ObjectiveLatency.Ms250, ObjectivePercentile.P99],
};

@Autometrics({objective: API_SLO})
@Autometrics({ objective: API_SLO })
export class DatabaseClient {
public users: User[];
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion examples/express/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from "node-fetch";

export function shouldError() {
if ((Math.floor(Math.random() * 100) + 1) === 1) {
if (Math.floor(Math.random() * 100) + 1 === 1) {
throw new Error("Error occurred");
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/fastify/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib"
}

22 changes: 11 additions & 11 deletions examples/nestjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parser: "@typescript-eslint/parser",
parserOptions: {
project: 'tsconfig.json',
project: "tsconfig.json",
tsconfigRootDir: __dirname,
sourceType: 'module',
sourceType: "module",
},
plugins: ['@typescript-eslint/eslint-plugin'],
plugins: ["@typescript-eslint/eslint-plugin"],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: [".eslintrc.js"],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
},
};
3 changes: 1 addition & 2 deletions examples/nestjs/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib"
}

4 changes: 2 additions & 2 deletions examples/nestjs/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { Controller, Get } from "@nestjs/common";
import { AppService } from "./app.service";

@Controller()
export class AppController {
Expand Down
6 changes: 3 additions & 3 deletions examples/nestjs/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";

@Module({
imports: [],
Expand Down
6 changes: 3 additions & 3 deletions examples/nestjs/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Autometrics } from '@autometrics/autometrics';
import { Injectable } from '@nestjs/common';
import { Autometrics } from "@autometrics/autometrics";
import { Injectable } from "@nestjs/common";

@Injectable()
@Autometrics()
export class AppService {
getHello(): string {
return 'Hello World!';
return "Hello World!";
}
}
12 changes: 6 additions & 6 deletions examples/react-app-experimental/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
);
27 changes: 26 additions & 1 deletion packages/exporter-otlp-http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const MAX_SAFE_INTERVAL = 2 ** 31 - 1;

export type InitOptions = {
/**
* URL of the OpenTelemetry Collector to push metrics to.
* URL of the OpenTelemetry Collector to push metrics to. Should be a
* complete url with port and `/v1/metrics` endpoint:
* `http://localhost:4317/v1/metrics`.
*/
url: string;

Expand Down Expand Up @@ -75,6 +77,29 @@ export function init({
temporalityPreference = AggregationTemporalityPreference.CUMULATIVE,
buildInfo,
}: InitOptions) {
const defaultPath = "/v1/metrics" as const;
const defaultPort = "4317" as const;

const urlObj = new URL(url);

//`pathname` is not reliable - it will always return "/" even if no explicit
// path is specified (see: https://github.com/autometrics-dev/autometrics-ts/pull/126#discussion_r1335623052).
// If this variable is not an empty string we should only give a warning without substituting
const explicitPath = url.replace(urlObj.origin, "");

if (explicitPath !== "" && !explicitPath.endsWith(defaultPath)) {
amLogger.warn(
"Warning: The official OTLP/HTTP endpoint path for metrics is '/v1/metrics', your metrics data might not be submitted properly. See: https://opentelemetry.io/docs/specs/otel/protocol/exporter/#endpoint-urls-for-otlphttp",
);
}

if (explicitPath === "" && urlObj.port === defaultPort) {
amLogger.info(
"Appending default path /v1/metrics to OTLP/HTTP endpoint URL",
);
url = urlObj.origin + defaultPath;
}

amLogger.info(`Exporter will push to the OTLP/HTTP endpoint at ${url}`);

const exporter = new OTLPMetricExporter({
Expand Down
2 changes: 1 addition & 1 deletion packages/exporter-otlp-http/tests/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("init test", () => {
const foo = new Foo();
foo.bar(); // one before

init({ url: "/metrics", pushInterval: 5000 });
init({ url: "http://localhost:4317", pushInterval: 5000 });

foo.bar(); // one after

Expand Down
2 changes: 1 addition & 1 deletion packages/exporter-otlp-http/tests/on-demand-push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("temporality test with on-demand push", () => {
const pushInterval = 0;

init({
url: "/metrics",
url: "http://localhost:4317/",
pushInterval,
temporalityPreference: AggregationTemporality.DELTA,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/exporter-otlp-http/tests/temporality.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("temporality test with scheduled push", () => {
const timeout = 10; // must be smaller than `pushInterval`.

init({
url: "/metrics",
url: "http://localhost:4317/",
pushInterval,
timeout,
temporalityPreference: AggregationTemporality.DELTA,
Expand Down
Loading