1.9 KiB
		
	
	
	
	
	
			
		
		
	
	Managing Go Modules in Grafana
Creating a New Module
Best Practices for Module Creation
Create a new module when:
- 
The code has a distinct responsibility or domain 
- 
The code needs its own dependency management 
- 
The code might be used independently of Grafana 
- 
The code has complex dependencies that should be isolated 
- 
The code needs to be versioned independently 
- 
Initialize the module: 
cd pkg/your/new/module
go mod init github.com/grafana/grafana/pkg/your/new/module
- Update the workspace:
make update-workspace
- 
Add module reference to go.workfile
- 
Update module consumers If other modules depend on the new module you are introducing, you migh need to temporarily add a replace directive to these consumer modules, while the newly introduced module is not published / in the main branch. You can add a replace directive like this: 
// In your module's go.mod
replace github.com/grafana/grafana/pkg/<my-module> => ../../../<my-module>
- Upadte Dockerfileto include the new module Example:
# Dockerfile
COPY pkg/your/new/module ./pkg/your/new/module
- Add module to dependabot.ymlfor dependency updates
Example:
# .github/dependabot.yml
updates:
  - package-ecosystem: 'github-actions'
    directory: '/'
    schedule:
      interval: 'daily'
  - package-ecosystem: 'gomod'
    directories:
      - '/'
      - '/pkg/your/new/module' # Add your new module here
[!IMPORTANT]
When running the command above, you may notice there will be some invalid revision version errors. This is because the module doesn't exist in the main branch yet and the root go.mod can't find a reference to the new module yet.
- Second PR (after first PR is merged):
- Run make update-workspaceagain
- This will update the module versions in the root go.mod
- The module will now be properly referenced from main
- You can now remove replace directives from your module
 
- Run