Skip to content
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

Include umbraco-package.json manifests in telemetry data #16430

Merged
merged 4 commits into from
May 31, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Umbraco.Cms.Api.Management.ViewModels.Manifest;
using Umbraco.Cms.Api.Management.ViewModels.Manifest;
using Umbraco.Cms.Core.Manifest;
using Umbraco.Cms.Core.Mapping;

Expand All @@ -13,6 +13,7 @@ public void DefineMaps(IUmbracoMapper mapper)
private static void Map(PackageManifest source, ManifestResponseModel target, MapperContext context)
{
target.Name = source.Name;
target.Id = source.Id;
target.Version = source.Version;
target.Extensions = source.Extensions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;

namespace Umbraco.Cms.Api.Management.ViewModels.Manifest;

Expand All @@ -7,6 +7,8 @@ public class ManifestResponseModel
[Required]
public string Name { get; set; } = string.Empty;

public string? Id { get; set; }

public string? Version { get; set; }

public object[] Extensions { get; set; } = Array.Empty<object>();
Expand Down
4 changes: 3 additions & 1 deletion src/Umbraco.Core/Manifest/PackageManifest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Umbraco.Cms.Core.Manifest;
namespace Umbraco.Cms.Core.Manifest;

public class PackageManifest
{
public required string Name { get; set; }

public string? Id { get; set; }

public string? Version { get; set; }

public bool AllowPublicAccess { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions src/Umbraco.Core/Services/IPackagingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@ public interface IPackagingService : IService
/// Returns the advertised installed packages
/// </summary>
/// <returns></returns>
[Obsolete("Use GetAllInstalledPackagesAsync instead. Scheduled for removal in Umbraco 15.")]
IEnumerable<InstalledPackage> GetAllInstalledPackages();

/// <summary>
/// Returns the advertised installed packages
/// </summary>
/// <returns></returns>
Task<IEnumerable<InstalledPackage>> GetAllInstalledPackagesAsync()
#pragma warning disable CS0618 // Type or member is obsolete
=> Task.FromResult(GetAllInstalledPackages());
#pragma warning restore CS0618 // Type or member is obsolete

/// <summary>
/// Returns installed packages collected from the package migration plans.
/// </summary>
Expand Down
10 changes: 3 additions & 7 deletions src/Umbraco.Core/Telemetry/TelemetryService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.

using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Manifest;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Packaging;
using Umbraco.Cms.Core.Services;
Expand Down Expand Up @@ -43,6 +40,7 @@ public TelemetryService(
public bool TryGetTelemetryReportData(out TelemetryReportData? telemetryReportData)
{
telemetryReportData = GetTelemetryReportDataAsync().GetAwaiter().GetResult();

return telemetryReportData != null;
}

Expand All @@ -58,7 +56,7 @@ public bool TryGetTelemetryReportData(out TelemetryReportData? telemetryReportDa
{
Id = telemetryId,
Version = GetVersion(),
Packages = await GetPackageTelemetryAsync(),
Packages = await GetPackageTelemetryAsync().ConfigureAwait(false),
Detailed = _usageInformationService.GetDetailed(),
};
}
Expand All @@ -67,8 +65,6 @@ public bool TryGetTelemetryReportData(out TelemetryReportData? telemetryReportDa
? null
: _umbracoVersion.SemanticVersion.ToSemanticStringWithoutBuild();



private async Task<IEnumerable<PackageTelemetry>?> GetPackageTelemetryAsync()
{
if (_metricsConsentService.GetConsentLevel() == TelemetryLevel.Minimal)
Expand All @@ -77,7 +73,7 @@ public bool TryGetTelemetryReportData(out TelemetryReportData? telemetryReportDa
}

List<PackageTelemetry> packages = new();
IEnumerable<InstalledPackage> installedPackages = _packagingService.GetAllInstalledPackages();
IEnumerable<InstalledPackage> installedPackages = await _packagingService.GetAllInstalledPackagesAsync().ConfigureAwait(false);

foreach (InstalledPackage installedPackage in installedPackages)
{
Expand Down
57 changes: 34 additions & 23 deletions src/Umbraco.Infrastructure/BackgroundJobs/Jobs/ReportSiteJob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Core.Telemetry;
Expand All @@ -9,7 +11,6 @@ namespace Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs;

public class ReportSiteJob : IRecurringBackgroundJob
{

public TimeSpan Period => TimeSpan.FromDays(1);

public TimeSpan Delay => TimeSpan.FromMinutes(5);
Expand All @@ -19,25 +20,37 @@ public class ReportSiteJob : IRecurringBackgroundJob
// No-op event as the period never changes on this job
public event EventHandler PeriodChanged
{
add { } remove { }
add { }
remove { }
}

private static HttpClient _httpClient = new();

private readonly ILogger<ReportSiteJob> _logger;

private readonly ITelemetryService _telemetryService;
private readonly IJsonSerializer _jsonSerializer;
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClientFactory _httpClientFactory;

[Obsolete("Use the constructor with IHttpClientFactory instead.")]
public ReportSiteJob(
ILogger<ReportSiteJob> logger,
ITelemetryService telemetryService,
IJsonSerializer jsonSerializer)
: this(
logger,
telemetryService,
jsonSerializer,
StaticServiceProvider.Instance.GetRequiredService<IHttpClientFactory>())
{ }

public ReportSiteJob(
ILogger<ReportSiteJob> logger,
ITelemetryService telemetryService,
IJsonSerializer jsonSerializer,
IHttpClientFactory httpClientFactory)
{
_logger = logger;
_telemetryService = telemetryService;
_jsonSerializer = jsonSerializer;
_httpClient = new HttpClient();
_httpClientFactory = httpClientFactory;
}

/// <summary>
Expand All @@ -46,7 +59,8 @@ public ReportSiteJob(
/// </summary>
public async Task RunJobAsync()
{
if (_telemetryService.TryGetTelemetryReportData(out TelemetryReportData? telemetryReportData) is false)
TelemetryReportData? telemetryReportData = await _telemetryService.GetTelemetryReportDataAsync().ConfigureAwait(false);
if (telemetryReportData is null)
{
_logger.LogWarning("No telemetry marker found");

Expand All @@ -55,31 +69,28 @@ public async Task RunJobAsync()

try
{
if (_httpClient.BaseAddress is null)
HttpClient httpClient = _httpClientFactory.CreateClient();
if (httpClient.BaseAddress is null)
{
// Send data to LIVE telemetry
_httpClient.BaseAddress = new Uri("https://telemetry.umbraco.com/");
httpClient.BaseAddress = new Uri("https://telemetry.umbraco.com/");

#if DEBUG
// Send data to DEBUG telemetry service
_httpClient.BaseAddress = new Uri("https://telemetry.rainbowsrock.net/");
httpClient.BaseAddress = new Uri("https://telemetry.rainbowsrock.net/");
#endif
}

_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

using (var request = new HttpRequestMessage(HttpMethod.Post, "installs/"))
{
request.Content = new StringContent(_jsonSerializer.Serialize(telemetryReportData), Encoding.UTF8,
"application/json");
using var request = new HttpRequestMessage(HttpMethod.Post, "installs/");
request.Content = new StringContent(_jsonSerializer.Serialize(telemetryReportData), Encoding.UTF8, "application/json");

// Make a HTTP Post to telemetry service
// https://telemetry.umbraco.com/installs/
// Fire & Forget, do not need to know if its a 200, 500 etc
using (await _httpClient.SendAsync(request))
{
}
}
// Make a HTTP Post to telemetry service
// https://telemetry.umbraco.com/installs/
// Fire & Forget, do not need to know if its a 200, 500 etc
using (await httpClient.SendAsync(request))
{ }
}
catch
{
Expand Down
Loading
Loading