-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add opentelemetry metrics reporting (#117)
- Loading branch information
Showing
6 changed files
with
190 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { AxiosResponse } from "axios"; | ||
import { Attributes } from "@opentelemetry/api"; | ||
import { SEMATTRS_HTTP_HOST, SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_STATUS_CODE } from "@opentelemetry/semantic-conventions"; | ||
import { AuthCredentialsConfig, CredentialsMethod } from "./credentials/types"; | ||
|
||
/** | ||
* Builds an object of attributes that can be used to report alongside an OpenTelemetry metric event. | ||
* | ||
* @param response - The Axios response object, used to add data like HTTP status, host, method, and headers. | ||
* @param credentials - The credentials object, used to add data like the ClientID when using ClientCredentials. | ||
* @param methodAttributes - Extra attributes that the method (i.e. check, listObjects) wishes to have included. Any custom attributes should use the common names. | ||
* @returns {Attributes} | ||
*/ | ||
|
||
export const buildAttributes = function buildAttributes(response: AxiosResponse<unknown, any> | undefined, credentials: AuthCredentialsConfig, methodAttributes: Record<string, any> = {}): Attributes { | ||
const attributes: Attributes = { | ||
...methodAttributes, | ||
}; | ||
|
||
if (response?.status) { | ||
attributes[SEMATTRS_HTTP_STATUS_CODE] = response.status; | ||
} | ||
|
||
if (response?.request) { | ||
attributes[SEMATTRS_HTTP_METHOD] = response.request.method; | ||
attributes[SEMATTRS_HTTP_HOST] = response.request.host; | ||
} | ||
|
||
if (response?.headers) { | ||
const modelId = response.headers["openfga-authorization-model-id"]; | ||
if (modelId !== undefined) { | ||
attributes[attributeNames.responseModelId] = modelId; | ||
} | ||
} | ||
|
||
if (credentials?.method === CredentialsMethod.ClientCredentials) { | ||
attributes[attributeNames.requestClientId] = credentials.config.clientId; | ||
} | ||
|
||
return attributes; | ||
}; | ||
/** | ||
* Common attribute names | ||
*/ | ||
|
||
export const attributeNames = { | ||
// Attributes associated with the request made | ||
requestModelId: "fga-client.request.model_id", | ||
requestMethod: "fga-client.request.method", | ||
requestStoreId: "fga-client.request.store_id", | ||
requestClientId: "fga-client.request.client_id", | ||
|
||
// Attributes associated with the response | ||
responseModelId: "fga-client.response.model_id", | ||
|
||
// Attributes associated with specific actions | ||
user: "fga-client.user" | ||
}; |