-
-
Notifications
You must be signed in to change notification settings - Fork 342
feat: Add Grafana module #1509
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
HofmeisterAn
merged 8 commits into
testcontainers:develop
from
thomhurst:feature/grafana-dotnet
Nov 8, 2025
Merged
feat: Add Grafana module #1509
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
29415ee
feat: Add Grafana module
thomhurst 66c10ec
Merge branch 'develop' into feature/grafana-dotnet
HofmeisterAn 9b2967e
Merge remote-tracking branch 'origin/develop' into fork/thomhurst/fea…
HofmeisterAn c16798b
fix: Add .runs-on config file
HofmeisterAn 6ed1670
Merge branch 'develop' into feature/grafana-dotnet
HofmeisterAn 1c14781
chore: Align with repo standards
HofmeisterAn 68a29bf
docs: Add Grafana module docs
HofmeisterAn 7767047
core: Apply code review feedback
HofmeisterAn 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Grafana | ||
|
|
||
| [Grafana](https://grafana.com/) is an open-source platform for monitoring, visualization, and analytics. It allows you to query, visualize, alert on, and explore metrics, logs, and traces from various data sources through customizable dashboards. | ||
|
|
||
| Add the following dependency to your project file: | ||
|
|
||
| ```shell title="NuGet" | ||
| dotnet add package Testcontainers.Grafana | ||
| ``` | ||
|
|
||
| You can start a Grafana container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations. | ||
|
|
||
| === "Create Container Instance" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Grafana.Tests/GrafanaContainerTest.cs:CreateGrafanaContainer" | ||
| ``` | ||
|
|
||
| This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method. | ||
|
|
||
| === "Usage Example" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Grafana.Tests/GrafanaContainerTest.cs:UseGrafanaContainer" | ||
| ``` | ||
|
|
||
| The test example queries the Grafana API endpoint `GET /api/org/` to retrieve the current organization. This API endpoint requires authentication, so the HTTP client's authorization header is set with Basic authentication using the username and password configured through the Grafana builder. The default configuration uses the username `admin` and password `admin`. | ||
|
|
||
| The test example uses the following NuGet dependencies: | ||
|
|
||
| === "Package References" | ||
| ```xml | ||
| --8<-- "tests/Testcontainers.Grafana.Tests/Testcontainers.Grafana.Tests.csproj:PackageReferences" | ||
| ``` | ||
|
|
||
| To execute the tests, use the command `dotnet test` from a terminal. | ||
|
|
||
| --8<-- "docs/modules/_call_out_test_projects.txt" | ||
|
|
||
| ## Enable anonymous access | ||
|
|
||
| Developers can enable anonymous access using the Grafana builder API `WithAnonymousAccessEnabled()`. This will enable anonymous access and no authentication is necessary to access Grafana: | ||
|
|
||
| ```csharp | ||
| GrafanaContainer _grafanaContainer = new GrafanaBuilder().WithAnonymousAccessEnabled().Build(); | ||
| ``` |
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
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 @@ | ||
| root = true |
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,105 @@ | ||
| namespace Testcontainers.Grafana; | ||
|
|
||
| /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
| [PublicAPI] | ||
| public sealed class GrafanaBuilder : ContainerBuilder<GrafanaBuilder, GrafanaContainer, GrafanaConfiguration> | ||
| { | ||
| public const string GrafanaImage = "grafana/grafana:12.2"; | ||
|
|
||
| public const ushort GrafanaPort = 3000; | ||
|
|
||
| public const string DefaultUsername = "admin"; | ||
|
|
||
| public const string DefaultPassword = "admin"; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaBuilder" /> class. | ||
| /// </summary> | ||
| public GrafanaBuilder() | ||
| : this(new GrafanaConfiguration()) | ||
| { | ||
| DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| private GrafanaBuilder(GrafanaConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| DockerResourceConfiguration = resourceConfiguration; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override GrafanaConfiguration DockerResourceConfiguration { get; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the Grafana username. | ||
| /// </summary> | ||
| /// <param name="username">The Grafana username.</param> | ||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||
| public GrafanaBuilder WithUsername(string username) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(username: username)) | ||
| .WithEnvironment("GF_SECURITY_ADMIN_USER", username); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the Grafana password. | ||
| /// </summary> | ||
| /// <param name="password">The Grafana password.</param> | ||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||
| public GrafanaBuilder WithPassword(string password) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(password: password)) | ||
| .WithEnvironment("GF_SECURITY_ADMIN_PASSWORD", password); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Enables the anonymous access. | ||
| /// </summary> | ||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||
| public GrafanaBuilder WithAnonymousAccessEnabled() | ||
| { | ||
| return WithEnvironment("GF_AUTH_ANONYMOUS_ENABLED", "true") | ||
| .WithEnvironment("GF_AUTH_ANONYMOUS_ORG_ROLE", "Admin"); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override GrafanaContainer Build() | ||
| { | ||
| Validate(); | ||
| return new GrafanaContainer(DockerResourceConfiguration); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override GrafanaBuilder Init() | ||
| { | ||
| return base.Init() | ||
| .WithImage(GrafanaImage) | ||
| .WithPortBinding(GrafanaPort, true) | ||
| .WithUsername(DefaultUsername) | ||
| .WithPassword(DefaultPassword) | ||
| .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => | ||
| request.ForPath("/api/health").ForPort(GrafanaPort))); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override GrafanaBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override GrafanaBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override GrafanaBuilder Merge(GrafanaConfiguration oldValue, GrafanaConfiguration newValue) | ||
| { | ||
| return new GrafanaBuilder(new GrafanaConfiguration(oldValue, newValue)); | ||
| } | ||
| } | ||
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,71 @@ | ||
| namespace Testcontainers.Grafana; | ||
|
|
||
| /// <inheritdoc cref="ContainerConfiguration" /> | ||
| [PublicAPI] | ||
| public sealed class GrafanaConfiguration : ContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="username">The Grafana username.</param> | ||
| /// <param name="password">The Grafana password.</param> | ||
| public GrafanaConfiguration( | ||
| string username = null, | ||
| string password = null) | ||
| { | ||
| Username = username; | ||
| Password = password; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public GrafanaConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public GrafanaConfiguration(IContainerConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public GrafanaConfiguration(GrafanaConfiguration resourceConfiguration) | ||
| : this(new GrafanaConfiguration(), resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="oldValue">The old Docker resource configuration.</param> | ||
| /// <param name="newValue">The new Docker resource configuration.</param> | ||
| public GrafanaConfiguration(GrafanaConfiguration oldValue, GrafanaConfiguration newValue) | ||
| : base(oldValue, newValue) | ||
| { | ||
| Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username); | ||
| Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana username. | ||
| /// </summary> | ||
| public string Username { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana password. | ||
| /// </summary> | ||
| public string Password { get; } | ||
| } |
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,27 @@ | ||
| namespace Testcontainers.Grafana; | ||
|
|
||
| /// <inheritdoc cref="DockerContainer" /> | ||
| [PublicAPI] | ||
| public sealed class GrafanaContainer : DockerContainer | ||
| { | ||
| private readonly GrafanaConfiguration _configuration; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaContainer" /> class. | ||
| /// </summary> | ||
| /// <param name="configuration">The container configuration.</param> | ||
| public GrafanaContainer(GrafanaConfiguration configuration) | ||
| : base(configuration) | ||
| { | ||
| _configuration = configuration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana base address. | ||
| /// </summary> | ||
| /// <returns>The Grafana base address.</returns> | ||
| public string GetBaseAddress() | ||
| { | ||
| return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(GrafanaBuilder.GrafanaPort)).ToString(); | ||
| } | ||
| } |
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,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0;net9.0;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
| <LangVersion>latest</LangVersion> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../Testcontainers/Testcontainers.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
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,6 @@ | ||
| global using System; | ||
| global using Docker.DotNet.Models; | ||
| global using DotNet.Testcontainers.Builders; | ||
| global using DotNet.Testcontainers.Configurations; | ||
| global using DotNet.Testcontainers.Containers; | ||
| global using JetBrains.Annotations; |
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 @@ | ||
| root = true |
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 @@ | ||
| ubuntu-24.04 |
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.