Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 86 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/github/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ describe('Type converters', () => {
created_at: new Date('2023-01-01T00:00:00Z'),
run_attempt: 1,
html_url: 'https://github.com/test/repo/actions/runs/12345',
actor: null,
event: null,
head_branch: null,
base_branch: null,
repository: {
full_name: 'test-owner/test-repo'
}
Expand Down
8 changes: 8 additions & 0 deletions src/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,11 @@ export const getLatestCompletedAt = (jobs: WorkflowJob[]): Date => {
const maxDateNumber = Math.max(...jobCompletedAtDates.map(Number))
return new Date(maxDateNumber)
}

export const getEarliestStartedAt = (jobs: WorkflowJob[]): Date => {
if (jobs.length === 0)
throw new Error('no jobs found to get earliest started_at date.')
const jobStartedAtDates = jobs.map(job => job.started_at)
const minDateNumber = Math.min(...jobStartedAtDates.map(Number))
return new Date(minDateNumber)
}
8 changes: 8 additions & 0 deletions src/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export type Workflow = {
readonly created_at: Date
readonly run_attempt: number
readonly html_url: string
readonly actor: string | null
readonly event: string | null
readonly head_branch: string | null
readonly base_branch: string | null
readonly repository: {
readonly full_name: string
}
Expand Down Expand Up @@ -159,6 +163,10 @@ export const toWorkflowRun = (workflowRun: WorkflowResponse): Workflow => {
created_at: new Date(workflowRun.created_at),
run_attempt: workflowRun.run_attempt,
html_url: workflowRun.html_url,
actor: workflowRun.actor?.login || null,
event: workflowRun.event || null,
head_branch: workflowRun.head_branch || null,
base_branch: workflowRun.pull_requests?.[0]?.base?.ref || null,
repository: {
full_name: workflowRun.repository.full_name
}
Expand Down
43 changes: 35 additions & 8 deletions src/instrumentation/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { detectResources, envDetector } from '@opentelemetry/resources'
import {
detectResources,
envDetector,
resourceFromAttributes
} from '@opentelemetry/resources'
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto'
import {
MeterProvider,
Expand All @@ -19,19 +23,31 @@ let meterProvider: MeterProvider

export const initialize = (
meterExporter?: PushMetricExporter,
spanExporter?: SpanExporter
spanExporter?: SpanExporter,
additionalResourceAttributes?: Record<string, string>
): void => {
if (settings.logeLevel === 'debug')
opentelemetry.diag.setLogger(
new opentelemetry.DiagConsoleLogger(),
opentelemetry.DiagLogLevel.DEBUG
)
initializeMeter(meterExporter)
initializeTracer(spanExporter)
initializeMeter(meterExporter, additionalResourceAttributes)
initializeTracer(spanExporter, additionalResourceAttributes)
}

const initializeMeter = (exporter?: PushMetricExporter): void => {
const initializeMeter = (
exporter?: PushMetricExporter,
additionalResourceAttributes?: Record<string, string>
): void => {
if (settings.FeatureFlagMetrics) {
// Detect base resource from environment
const baseResource = detectResources({ detectors: [envDetector] })

// Merge additional workflow attributes into resource
const resource = additionalResourceAttributes
? baseResource.merge(resourceFromAttributes(additionalResourceAttributes))
: baseResource

meterProvider = new MeterProvider({
readers: [
new PeriodicExportingMetricReader({
Expand All @@ -41,7 +57,7 @@ const initializeMeter = (exporter?: PushMetricExporter): void => {
exportIntervalMillis: 24 * 60 * 60 * 1000 // 24 hours
})
],
resource: detectResources({ detectors: [envDetector] })
resource
})
} else {
meterProvider = new MeterProvider()
Expand All @@ -54,10 +70,21 @@ const initializeMeter = (exporter?: PushMetricExporter): void => {
}
}

const initializeTracer = (exporter?: SpanExporter): void => {
const initializeTracer = (
exporter?: SpanExporter,
additionalResourceAttributes?: Record<string, string>
): void => {
if (settings.FeatureFlagTrace) {
// Detect base resource from environment
const baseResource = detectResources({ detectors: [envDetector] })

// Merge additional workflow attributes into resource
const resource = additionalResourceAttributes
? baseResource.merge(resourceFromAttributes(additionalResourceAttributes))
: baseResource

traceProvider = new BasicTracerProvider({
resource: detectResources({ detectors: [envDetector] }),
resource,
spanProcessors: [
new BatchSpanProcessor(exporter || new OTLPTraceExporter({}))
]
Expand Down
33 changes: 26 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,38 @@ import { settings } from './settings.js'
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
// required: run initialize() first.
// usually use --required runtime option for first reading.
// for simple use this action, this is satisfied on here.
initialize()

let exitCode = 0

try {
// Create Octokit client and workflow context
// Fetch workflow data FIRST before initializing SDK
// This allows us to set workflow attributes as resource attributes
const octokit = createOctokitClient()
const workflowContext = getWorkflowContext(github.context, settings)

const results = await fetchWorkflowResults(octokit, workflowContext)

// Initialize SDK with workflow data as resource attributes
// This ensures they become Prometheus labels
const workflowResourceAttributes: Record<string, string> = {}
if (results.workflow.actor) {
workflowResourceAttributes['workflow.actor'] = results.workflow.actor
}
if (results.workflow.event) {
workflowResourceAttributes['workflow.event'] = results.workflow.event
}
if (results.workflow.head_branch) {
workflowResourceAttributes['workflow.head_branch'] =
results.workflow.head_branch
}
if (results.workflow.base_branch) {
workflowResourceAttributes['workflow.base_branch'] =
results.workflow.base_branch
}

core.info(
`Setting workflow resource attributes: ${JSON.stringify(workflowResourceAttributes)}`
)
initialize(undefined, undefined, workflowResourceAttributes)

await createMetrics(results)
const traceId = await createTrace(results)
await writeSummaryIfNeeded(traceId)
Expand Down
Loading