Skip to content

[Builder] Add package option to support generating library (non-main) Go packages and improve build logging #13296

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tomatopunk
Copy link

Description

This PR adds a new package option to the builder configuration, allowing the generated OpenTelemetry Collector sources to use a custom package name instead of main. When the package is set to a value other than main, the build process is updated to recognize that the output is intended as a Go library for import by other Go modules, rather than an executable binary.

Logging is improved to provide clear messaging about this build mode.

Link to tracking issue

Fixes #

Testing

Documentation

Added documentation for the new package option in the example builder configuration and usage docs.
Clarified build output expectations when using non-main packages.

@tomatopunk tomatopunk requested a review from a team as a code owner June 30, 2025 07:02
@tomatopunk tomatopunk requested a review from atoulme June 30, 2025 07:02
@dmathieu
Copy link
Member

This should be tested.

@tomatopunk
Copy link
Author

This should be tested.

Added two improvements in the unit tests:

  1. enhanced config_test to validate the default config
  2. after running Generate, added a check to ensure the package statement in each generated Go file matches Distribution.Package

# Conflicts:
#	cmd/builder/internal/config/default.yaml
@@ -5,6 +5,9 @@ package builder

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit dubious about parsing Go code in a test to ensure it compiles. That seems a bit brittle.
How about doing something similar to the test folder that actually generates a collector and ensure it can run, to generate a library collector and ensure golangci-lint passes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem: the generated library collector cannot be run directly. Instead, it should be used as a referenced code library. Therefore, I only check whether the generated code meets our expectations, rather than whether it runs independently.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The library collector can be compiled, but it cannot run because it lacks a main package. That’s why, at https://github.com/open-telemetry/opentelemetry-collector/pull/13296/files#diff-117127e4cf9f6587102dc92a7240d1e2c00944ee47ce23e02731f3a41466bdb1R109-R112 in the compline function, I exit if the package is not main and do not proceed with compilation.

In my program, I manually create an export.go file, which defines an exportable function that calls the main method. This allows the upper layer to import and use it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create the main func in your test directory and call the library?

Copy link

codecov bot commented Jul 1, 2025

Codecov Report

Attention: Patch coverage is 37.50000% with 5 lines in your changes missing coverage. Please review.

Project coverage is 91.60%. Comparing base (18b3b57) to head (63e02a6).

Files with missing lines Patch % Lines
cmd/builder/internal/builder/main.go 28.57% 3 Missing and 2 partials ⚠️

❌ Your patch check has failed because the patch coverage (37.50%) is below the target coverage (95.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #13296      +/-   ##
==========================================
- Coverage   91.64%   91.60%   -0.05%     
==========================================
  Files         522      522              
  Lines       29183    29189       +6     
==========================================
- Hits        26746    26739       -7     
- Misses       1920     1929       +9     
- Partials      517      521       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants