-
Notifications
You must be signed in to change notification settings - Fork 777
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
Azure Function example #1672
Azure Function example #1672
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<AzureFunctionsVersion>v3</AzureFunctionsVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" /> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" /> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Http\OpenTelemetry.Instrumentation.Http.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="host.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// <copyright file="MyFunction.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Diagnostics; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.Extensions.Http; | ||
|
||
namespace Examples.AzureFunction | ||
{ | ||
public class MyFunction | ||
{ | ||
private static ActivitySource activitySource = new ActivitySource("MyFunction"); | ||
|
||
[FunctionName("MyFunction")] | ||
public async Task<IActionResult> Run( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, | ||
ExecutionContext context) | ||
{ | ||
using var activity = activitySource.StartActivity("MyFunction", ActivityKind.Server); | ||
activity?.SetTag("faas.trigger", "http"); | ||
|
||
// TODO: Consider adding other relevant tags to the activity demonstrating the FAAS semantic conventions: | ||
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/faas.md | ||
|
||
// TODO: Figure out why this HttpClient invocation does not generate an activity. | ||
using var client = new HttpClient(); | ||
var response = await client.GetStringAsync("https://opentelemetry.io/"); | ||
return new OkObjectResult(response); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// <copyright file="Startup.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using Microsoft.Azure.Functions.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
[assembly: FunctionsStartup(typeof(Examples.AzureFunction.Startup))] | ||
|
||
namespace Examples.AzureFunction | ||
{ | ||
public class Startup : FunctionsStartup | ||
{ | ||
private static TracerProvider tracerProvider; | ||
|
||
public override void Configure(IFunctionsHostBuilder builder) | ||
{ | ||
// TODO: It does not appear that the OpenTelemetry.Extensions.Hosting package is compatible with Azure Functions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea. its a known limitation in Function. We can try to change the usage of our hosted service to something else. Perhaps |
||
// Using it causes the following error: | ||
// [Invalid] ServiceType: Microsoft.Extensions.Hosting.IHostedService, Lifetime: Singleton, ImplementationType: OpenTelemetry.Extensions.Hosting.Implementation.TelemetryHostedService, OpenTelemetry.Extensions.Hosting, Version = 0.2.0.909, Culture = neutral, PublicKeyToken = 7bd6737fe5b67e3c. | ||
// builder.Services.AddOpenTelemetryTracing(builder => | ||
// { | ||
// builder | ||
// .AddSource("MyFunction") | ||
// .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyFunction")) | ||
// .AddHttpClientInstrumentation() | ||
// .AddConsoleExporter(); | ||
// }); | ||
|
||
tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.AddSource("MyFunction") | ||
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("MyFunction")) | ||
.AddHttpClientInstrumentation() | ||
.AddConsoleExporter() | ||
.Build(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"version": "2.0", | ||
"logging": { | ||
"fileLoggingMode": "always", | ||
"logLevel": { | ||
"Default": "Debug" | ||
}, | ||
"applicationInsights": { | ||
"samplingExcludedTypes": "Request", | ||
"samplingSettings": { | ||
"isEnabled": true | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed the same. The DiagnosticSource events are not triggered. I suspect this is something to do with how Functions run in some special environment? I'll try to find more on this.