-
-
Notifications
You must be signed in to change notification settings - Fork 892
feat(webapp): add ecs otel resource attributes #2454
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
263 changes: 263 additions & 0 deletions
263
apps/webapp/app/v3/telemetry/asyncResourceAttributes.server.ts
This file contains hidden or 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,263 @@ | ||
| import { z } from "zod"; | ||
| import type { ResourceAttributes } from "@opentelemetry/resources"; | ||
| import { | ||
| SEMRESATTRS_AWS_ECS_CLUSTER_ARN, | ||
| SEMRESATTRS_AWS_ECS_CONTAINER_ARN, | ||
| SEMRESATTRS_AWS_ECS_LAUNCHTYPE, | ||
| SEMRESATTRS_AWS_ECS_TASK_ARN, | ||
| SEMRESATTRS_AWS_ECS_TASK_FAMILY, | ||
| SEMRESATTRS_AWS_ECS_TASK_REVISION, | ||
| SEMRESATTRS_AWS_LOG_GROUP_NAMES, | ||
| SEMRESATTRS_AWS_LOG_STREAM_NAMES, | ||
| SEMRESATTRS_CLOUD_AVAILABILITY_ZONE, | ||
| SEMRESATTRS_CLOUD_PLATFORM, | ||
| SEMRESATTRS_CLOUD_PROVIDER, | ||
| SEMRESATTRS_CLOUD_REGION, | ||
| SEMRESATTRS_CONTAINER_NAME, | ||
| SEMRESATTRS_CONTAINER_ID, | ||
| SEMRESATTRS_CONTAINER_IMAGE_NAME, | ||
| SEMRESATTRS_CONTAINER_IMAGE_TAG, | ||
| CLOUDPLATFORMVALUES_AWS_ECS, | ||
| CLOUDPROVIDERVALUES_AWS, | ||
| } from "@opentelemetry/semantic-conventions"; | ||
| import { tryCatch } from "@trigger.dev/core/utils"; | ||
| import { logger } from "~/services/logger.server"; | ||
|
|
||
| // Minimal schema for ECS task metadata | ||
| const ECSTaskMetadataSchema = z.object({ | ||
| Cluster: z.string().optional(), | ||
| TaskARN: z.string().optional(), | ||
| Family: z.string().optional(), | ||
| Revision: z.string().optional(), | ||
| AvailabilityZone: z.string().optional(), | ||
| LaunchType: z.string().optional(), | ||
| ServiceName: z.string().optional(), | ||
| }); | ||
|
|
||
| const ECSLogOptions = z.object({ | ||
| "awslogs-group": z.string().optional(), | ||
| "awslogs-region": z.string().optional(), | ||
| "awslogs-stream": z.string().optional(), | ||
| mode: z.string().optional(), | ||
| }); | ||
|
|
||
| // Minimal schema for container metadata | ||
| const ECSContainerMetadataSchema = z.object({ | ||
| DockerId: z.string().optional(), | ||
| Name: z.string().optional(), | ||
| Image: z.string().optional(), | ||
| ImageID: z.string().optional(), | ||
| ContainerARN: z.string().optional(), | ||
| LogOptions: ECSLogOptions.optional(), | ||
| }); | ||
|
|
||
| // Cache for ECS metadata to avoid repeated fetches | ||
| let ecsMetadataCache: ResourceAttributes | null = null; | ||
|
|
||
| /** | ||
| * Fetches ECS task metadata from the v4 endpoint | ||
| */ | ||
| async function fetchECSTaskMetadata(metadataUri: string): Promise<ResourceAttributes> { | ||
| const [error, response] = await tryCatch( | ||
| fetch(`${metadataUri}/task`, { | ||
| signal: AbortSignal.timeout(5000), | ||
| }) | ||
| ); | ||
|
|
||
| if (error) { | ||
| logger.warn("Failed to fetch ECS task metadata", { error }); | ||
| return {}; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| logger.warn("ECS task metadata fetch failed", { status: response.status }); | ||
| return {}; | ||
| } | ||
|
|
||
| const [jsonError, taskJson] = await tryCatch(response.json()); | ||
| if (jsonError) { | ||
| logger.warn("Failed to parse ECS task metadata", { error: jsonError }); | ||
| return {}; | ||
| } | ||
|
|
||
| const parseResult = ECSTaskMetadataSchema.safeParse(taskJson); | ||
| if (!parseResult.success) { | ||
| logger.warn("ECS task metadata validation issues", { issues: parseResult.error.issues }); | ||
| return {}; | ||
| } | ||
|
|
||
| const taskData = parseResult.data; | ||
| const attributes: ResourceAttributes = {}; | ||
|
|
||
| if (taskData.TaskARN) { | ||
| attributes[SEMRESATTRS_AWS_ECS_TASK_ARN] = taskData.TaskARN; | ||
| } | ||
|
|
||
| if (taskData.Cluster) { | ||
| attributes[SEMRESATTRS_AWS_ECS_CLUSTER_ARN] = taskData.Cluster; | ||
| } | ||
|
|
||
| if (taskData.LaunchType) { | ||
| attributes[SEMRESATTRS_AWS_ECS_LAUNCHTYPE] = taskData.LaunchType; | ||
| } | ||
|
|
||
| if (taskData.Family) { | ||
| attributes[SEMRESATTRS_AWS_ECS_TASK_FAMILY] = taskData.Family; | ||
| } | ||
|
|
||
| if (taskData.Revision) { | ||
| attributes[SEMRESATTRS_AWS_ECS_TASK_REVISION] = taskData.Revision; | ||
| } | ||
|
|
||
| if (taskData.AvailabilityZone) { | ||
| attributes[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE] = taskData.AvailabilityZone; | ||
| } | ||
|
|
||
| if (taskData.ServiceName) { | ||
| // Custom attribute for ECS service name | ||
| attributes["aws.ecs.service.name"] = taskData.ServiceName; | ||
| } | ||
|
|
||
| return attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches ECS container metadata from the v4 endpoint | ||
| */ | ||
| async function fetchECSContainerMetadata(metadataUri: string): Promise<ResourceAttributes> { | ||
| const [error, response] = await tryCatch( | ||
| fetch(metadataUri, { | ||
| signal: AbortSignal.timeout(5000), | ||
| }) | ||
| ); | ||
|
|
||
| if (error) { | ||
| logger.warn("Failed to fetch ECS container metadata", { error }); | ||
| return {}; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| logger.warn("ECS container metadata fetch failed", { status: response.status }); | ||
| return {}; | ||
| } | ||
|
|
||
| const [jsonError, containerJson] = await tryCatch(response.json()); | ||
| if (jsonError) { | ||
| logger.warn("Failed to parse ECS container metadata", { error: jsonError }); | ||
| return {}; | ||
| } | ||
|
|
||
| const parseResult = ECSContainerMetadataSchema.safeParse(containerJson); | ||
| if (!parseResult.success) { | ||
| logger.warn("ECS container metadata validation issues", { issues: parseResult.error.issues }); | ||
| return {}; | ||
| } | ||
|
|
||
| const containerData = parseResult.data; | ||
| const attributes: ResourceAttributes = {}; | ||
|
|
||
| if (containerData.Name) { | ||
| attributes[SEMRESATTRS_CONTAINER_NAME] = containerData.Name; | ||
| } | ||
|
|
||
| if (containerData.DockerId) { | ||
| attributes[SEMRESATTRS_CONTAINER_ID] = containerData.DockerId; | ||
| } | ||
|
|
||
| if (containerData.Image) { | ||
| const [name, tag] = containerData.Image.split(":"); | ||
|
|
||
| if (name) { | ||
| attributes[SEMRESATTRS_CONTAINER_IMAGE_NAME] = name; | ||
| } | ||
|
|
||
| if (tag) { | ||
| attributes[SEMRESATTRS_CONTAINER_IMAGE_TAG] = tag; | ||
| } | ||
| } | ||
|
|
||
| if (containerData.ImageID) { | ||
| // Custom attribute for image ID | ||
| attributes["container.image.id"] = containerData.ImageID; | ||
| } | ||
|
|
||
| if (containerData.ContainerARN) { | ||
| attributes[SEMRESATTRS_AWS_ECS_CONTAINER_ARN] = containerData.ContainerARN; | ||
| } | ||
|
|
||
| const logOptions = containerData.LogOptions; | ||
| if (logOptions?.["awslogs-group"]) { | ||
| attributes[SEMRESATTRS_AWS_LOG_GROUP_NAMES] = [logOptions["awslogs-group"]]; | ||
| } | ||
| if (logOptions?.["awslogs-stream"]) { | ||
| attributes[SEMRESATTRS_AWS_LOG_STREAM_NAMES] = [logOptions["awslogs-stream"]]; | ||
| } | ||
| if (logOptions?.mode) { | ||
| // Custom attribute for log mode | ||
| attributes["aws.log.mode"] = [logOptions.mode]; | ||
| } | ||
|
|
||
| return attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches ECS metadata from the Task Metadata Endpoint V4 | ||
| * Returns resource attributes for OpenTelemetry | ||
| */ | ||
| async function fetchECSMetadata(metadataUri: string): Promise<ResourceAttributes> { | ||
| // Return cached metadata if available | ||
| if (ecsMetadataCache !== null) { | ||
| return ecsMetadataCache; | ||
| } | ||
|
|
||
| if (!metadataUri) { | ||
| // Not running in ECS | ||
| ecsMetadataCache = {}; | ||
| return ecsMetadataCache; | ||
| } | ||
|
|
||
| // Fetch task metadata and CloudWatch logs config in parallel | ||
| const [taskAttributes, containerAttributes] = await Promise.all([ | ||
| fetchECSTaskMetadata(metadataUri), | ||
| fetchECSContainerMetadata(metadataUri), | ||
| ]); | ||
|
|
||
| const attributes: ResourceAttributes = { | ||
| [SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AWS, | ||
| [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AWS_ECS, | ||
| ...taskAttributes, | ||
| ...containerAttributes, | ||
| }; | ||
|
|
||
| const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION; | ||
| if (region) { | ||
| attributes[SEMRESATTRS_CLOUD_REGION] = region; | ||
| } | ||
|
|
||
| logger.info("🔦 Fetched ECS metadata", { attributes }); | ||
|
|
||
| ecsMetadataCache = attributes; | ||
| return attributes; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches async resource attributes | ||
| * Designed to be used with the Resource constructor's asyncAttributesPromise parameter | ||
| * | ||
| * Usage: | ||
| * ``` | ||
| * new Resource( | ||
| * { [SEMRESATTRS_SERVICE_NAME]: 'my-service' }, | ||
| * getAsyncResourceAttributes() | ||
| * ) | ||
| * ``` | ||
| */ | ||
| export async function getAsyncResourceAttributes(): Promise<ResourceAttributes> { | ||
| const metadataUri = process.env.ECS_CONTAINER_METADATA_URI_V4; | ||
|
|
||
| if (!metadataUri) { | ||
| return {}; | ||
| } | ||
|
|
||
| return fetchECSMetadata(metadataUri); | ||
| } | ||
nicktrn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.