Skip to content

Commit

Permalink
Configured Project ID takes precedence over Project ID of project in …
Browse files Browse the repository at this point in the history
…which the code is running for using as log writing target.
  • Loading branch information
amanda-tarafa committed Mar 6, 2020
1 parent ba6f275 commit 528388f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ public async Task UnknownPlatform()
Assert.Equal("global", r.Type);
Assert.Equal(1, r.Labels.Count);
Assert.Equal(s_projectId, r.Labels["project_id"]);
Assert.Equal(s_projectId, uploadedEntries[0].LogNameAsLogName.ProjectId);
}

[Fact]
Expand All @@ -516,7 +517,7 @@ public async Task GcePlatform()
const string json = @"
{
'project': {
'projectId': '" + s_projectId + @"'
'projectId': 'gce_project_id'
},
'instance': {
'id': 'FakeInstanceId',
Expand All @@ -535,15 +536,19 @@ public async Task GcePlatform()
var r = uploadedEntries[0].Resource;
Assert.Equal("gce_instance", r.Type);
Assert.Equal(3, r.Labels.Count);
Assert.Equal(s_projectId, r.Labels["project_id"]);
// This is the monitored resource project ID.
Assert.Equal("gce_project_id", r.Labels["project_id"]);
Assert.Equal("FakeInstanceId", r.Labels["instance_id"]);
Assert.Equal("FakeZone", r.Labels["zone"]);
// If the project ID is configured, it is used as the target for writing
// logs, even if the code is running on GCP.
Assert.Equal(s_projectId, uploadedEntries[0].LogNameAsLogName.ProjectId);
}

[Fact]
public async Task GaePlatform()
{
var platform = new Platform(new GaePlatformDetails(s_projectId, "FakeInstanceId", "FakeService", "FakeVersion"));
var platform = new Platform(new GaePlatformDetails("gae_project_id", "FakeInstanceId", "FakeService", "FakeVersion"));
var uploadedEntries = await RunTestWorkingServer(
googleTarget =>
{
Expand All @@ -554,9 +559,36 @@ public async Task GaePlatform()
var r = uploadedEntries[0].Resource;
Assert.Equal("gae_app", r.Type);
Assert.Equal(3, r.Labels.Count);
Assert.Equal(s_projectId, r.Labels["project_id"]);
// This is the monitored resource project ID.
Assert.Equal("gae_project_id", r.Labels["project_id"]);
Assert.Equal("FakeService", r.Labels["module_id"]);
Assert.Equal("FakeVersion", r.Labels["version_id"]);
// If the project ID is configured, it is used as the target for writing
// logs, even if the code is running on GCP.
Assert.Equal(s_projectId, uploadedEntries[0].LogNameAsLogName.ProjectId);
}

[Fact]
public async Task GaePlatform_NoConfiguredProjectId()
{
var platform = new Platform(new GaePlatformDetails("gae_project_id", "FakeInstanceId", "FakeService", "FakeVersion"));
var uploadedEntries = await RunTestWorkingServer(
googleTarget =>
{
LogInfo("Message0");
return Task.FromResult(0);
}, platform: platform, enableResourceTypeDetection: true, configFn: target => target.ProjectId = null);
Assert.Equal(1, uploadedEntries.Count);
var r = uploadedEntries[0].Resource;
Assert.Equal("gae_app", r.Type);
Assert.Equal(3, r.Labels.Count);
// This is the monitored resource project ID.
Assert.Equal("gae_project_id", r.Labels["project_id"]);
Assert.Equal("FakeService", r.Labels["module_id"]);
Assert.Equal("FakeVersion", r.Labels["version_id"]);
// If the project ID is not configured, then the target will be the project where
// the monitored resource is at.
Assert.Equal("gae_project_id", uploadedEntries[0].LogNameAsLogName.ProjectId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,27 @@ private GoogleCredential GetCredentialFromConfiguration()

private void ActivateLogIdAndResource(string logId)
{
string projectId = null;
// Project ID of the project where the resource that is being monitored lives.
string monitoredProjectId = null;
// Project ID of the project where the entries are to be written.
string targetProjectId = null;
MonitoredResource resource = null;

if (!DisableResourceTypeDetection)
{
resource = MonitoredResourceBuilder.FromPlatform(_platform);
resource.Labels.TryGetValue("project_id", out projectId);
resource.Labels.TryGetValue("project_id", out monitoredProjectId);
}
targetProjectId = ProjectId?.Render(LogEventInfo.CreateNullEvent());

if (projectId == null)
if (monitoredProjectId == null)
{
// Either platform detection is disabled, or it detected an unknown platform.
// So use the manually configured projectId and override the resource.
projectId = GaxPreconditions.CheckNotNull(ProjectId?.Render(LogEventInfo.CreateNullEvent()), nameof(ProjectId));
monitoredProjectId = GaxPreconditions.CheckNotNull(targetProjectId, nameof(ProjectId));
if (ResourceType == null)
{
resource = new MonitoredResource { Type = "global", Labels = { { "project_id", projectId } } };
resource = new MonitoredResource { Type = "global", Labels = { { "project_id", monitoredProjectId } } };
}
else
{
Expand All @@ -312,8 +317,14 @@ private void ActivateLogIdAndResource(string logId)
}
}

if (targetProjectId == null)
{
// If there was no configured projectId, then use the same as the monitored resoure.
targetProjectId = monitoredProjectId;
}

_resource = resource;
var logName = new LogName(projectId, logId);
var logName = new LogName(targetProjectId, logId);
_logName = logName.ToString();
_logNameToWrite = logName;
}
Expand Down

0 comments on commit 528388f

Please sign in to comment.