.NET Client library for Grafana. This is not official Grafana package.
Tested on Grafana 8.5
Supported of grafana apis
- folder get/crud
- dashboard get/crud
Package Manager : Install-Package GrafanaApiClient
CLI : dotnet add package GrafanaApiClient
Add using and IGrafanaControlService as a service in Program.cs
using GrafanaApiClient;
builder.Services.AddTransient<IGrafanaControlService, GrafanaControlService>();
builder.Services.AddHttpClient();
Add Grafana block to your appsettings.json
"Grafana": {
"Url": "http://YOUR_GRAFANA_URL:3000",
"ApiKey": "YOUR_API_KEY"
}
Inject service then use service as following:
// get folders
var result = await _grafanaControlService.FolderGetListAsync(limit);
// create folder
var requestData = new FolderCreateRequest()
{
Title = folderName,
};
try
{
var result = await _grafanaControlService.FolderCreateAsync(requestData);
}
catch(GrafanaRequestException ex)
{
Console.WriteLine(ex);
}
Full useage example in custom controlller where grafana apis are called
using System.ComponentModel.DataAnnotations;
using GrafanaApiClient;
using GrafanaApiClient.Models.Api.Folder;
using Microsoft.AspNetCore.Mvc;
namespace ExampleProject.MasterDataDao.Controllers.Grafana;
[Route("api/[controller]")]
[ApiController]
public class GrafanaFolderController : ControllerBase
{
private readonly IGrafanaControlService _grafanaControlService;
public GrafanaFolderController(IGrafanaControlService grafanaControlService)
{
_grafanaControlService = grafanaControlService;
}
[HttpGet("GetList")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<FolderResponse>> GetList(int? limit)
{
var result = await _grafanaControlService.FolderGetListAsync(limit);
return Ok(result);
}
[HttpGet("GetItem")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<FolderResponse>> GetItem([Required] string uid)
{
var result = await _grafanaControlService.FolderGetItemAsync(uid);
return Ok(result);
}
[HttpPost("Create")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<FolderResponse>> Create([FromBody, Required]string folderName)
{
var requestData = new FolderCreateRequest()
{
Title = folderName,
};
var result = await _grafanaControlService.FolderCreateAsync(requestData);
return Ok(result);
}
}