-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds .Net support via dotnetspy (#117)
- Loading branch information
1 parent
812ca3f
commit 084e461
Showing
13 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,13 @@ | ||
FROM mcr.microsoft.com/dotnet/sdk:5.0 | ||
|
||
WORKDIR /dotnet | ||
|
||
COPY --from=pyroscope/pyroscope:dev /usr/bin/pyroscope /usr/bin/pyroscope | ||
ADD example . | ||
|
||
RUN dotnet publish -o . -r linux-x64 | ||
|
||
ENV PYROSCOPE_APPLICATION_NAME=simple.dotnet.app | ||
ENV PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040/ | ||
ENV PYROSCOPE_LOG_LEVEL=debug | ||
CMD ["pyroscope", "exec", "dotnet", "/dotnet/example"] |
This file contains 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,13 @@ | ||
--- | ||
version: "3.9" | ||
services: | ||
pyroscope: | ||
image: "pyroscope/pyroscope:latest" | ||
ports: | ||
- "4040:4040" | ||
command: | ||
- "server" | ||
app: | ||
environment: | ||
ASPNETCORE_URLS: http://*:5000 | ||
build: . |
This file contains 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,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<AssemblyName>example</AssemblyName> | ||
<OutputType>Exe</OutputType> | ||
<PackageId>example</PackageId> | ||
</PropertyGroup> | ||
</Project> |
This file contains 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,15 @@ | ||
@page | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Page Title</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="stylesheet" type="text/css" media="screen" href="main.css" /> | ||
<script src="main.js"></script> | ||
</head> | ||
<body> | ||
<h1>Hello</h1> | ||
</body> | ||
</html> |
This file contains 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,38 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace PracticalAspNetCore | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddRazorPages(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapRazorPages(); | ||
}); | ||
} | ||
} | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
webBuilder.UseStartup<Startup>() | ||
); | ||
} | ||
} |
This file contains 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 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 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 @@ | ||
// +build dotnetspy | ||
|
||
package dotnetspy | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type DotnetSpy struct { | ||
session *session | ||
m sync.Mutex | ||
reset bool | ||
} | ||
|
||
func Start(pid int) (*DotnetSpy, error) { | ||
s := newSession(pid) | ||
err := s.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DotnetSpy{session: s}, nil | ||
} | ||
|
||
func (s *DotnetSpy) Stop() error { | ||
return s.session.Stop() | ||
} | ||
|
||
func (s *DotnetSpy) Reset() { | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
s.reset = true | ||
} | ||
|
||
func (s *DotnetSpy) Snapshot(cb func([]byte, uint64, error)) { | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
if !s.reset { | ||
return | ||
} | ||
s.reset = false | ||
s.session.Flush(func(name []byte, v uint64) { | ||
cb(name, v, nil) | ||
}) | ||
} |
This file contains 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,3 @@ | ||
// +build !dotnetspy | ||
|
||
package dotnetspy |
This file contains 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,142 @@ | ||
// +build dotnetspy | ||
|
||
package dotnetspy | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pyroscope-io/dotnetdiag" | ||
"github.com/pyroscope-io/dotnetdiag/nettrace" | ||
"github.com/pyroscope-io/dotnetdiag/nettrace/profiler" | ||
) | ||
|
||
type session struct { | ||
client *dotnetdiag.Client | ||
config dotnetdiag.CollectTracingConfig | ||
session *dotnetdiag.Session | ||
|
||
ch chan line | ||
m sync.Mutex | ||
stop bool | ||
} | ||
|
||
type line struct { | ||
name []byte | ||
val int | ||
} | ||
|
||
func newSession(pid int) *session { | ||
return &session{ | ||
client: dotnetdiag.NewClient(dotnetdiag.DefaultServerAddress(pid)), | ||
config: dotnetdiag.CollectTracingConfig{ | ||
CircularBufferSizeMB: 10, | ||
Providers: []dotnetdiag.ProviderConfig{ | ||
{ | ||
Keywords: 0x0000F00000000000, | ||
LogLevel: 4, | ||
ProviderName: "Microsoft-DotNETCore-SampleProfiler", | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (s *session) Start() error { | ||
ns, err := s.client.CollectTracing(s.config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.session = ns | ||
stream := nettrace.NewStream(ns) | ||
trace, err := stream.Open() | ||
if err != nil { | ||
_ = ns.Close() | ||
return err | ||
} | ||
|
||
p := profiler.NewSampleProfiler(trace) | ||
stream.EventHandler = p.EventHandler | ||
stream.MetadataHandler = p.MetadataHandler | ||
stream.StackBlockHandler = p.StackBlockHandler | ||
stream.SequencePointBlockHandler = p.SequencePointBlockHandler | ||
|
||
s.ch = make(chan line) | ||
r := newRenderer(func(name []byte, val int) { | ||
s.ch <- line{ | ||
name: name, | ||
val: val, | ||
} | ||
}) | ||
|
||
go func() { | ||
defer close(s.ch) | ||
for { | ||
switch err = stream.Next(); err { | ||
default: | ||
case nil: | ||
continue | ||
case io.EOF: | ||
p.Walk(r.visitor) | ||
r.flush() | ||
} | ||
return | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (s *session) Flush(cb func([]byte, uint64)) error { | ||
s.session.Close() | ||
for v := range s.ch { | ||
cb(v.name, uint64(v.val)) | ||
} | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
if s.stop { | ||
return nil | ||
} | ||
return s.Start() | ||
} | ||
|
||
func (s *session) Stop() error { | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
s.session.Close() | ||
s.stop = true | ||
return nil | ||
} | ||
|
||
type renderer struct { | ||
callBack func(name []byte, val int) | ||
names []string | ||
val time.Duration | ||
prev int | ||
} | ||
|
||
func newRenderer(cb func(name []byte, val int)) *renderer { | ||
return &renderer{callBack: cb} | ||
} | ||
|
||
func (r *renderer) visitor(frame profiler.FrameInfo) { | ||
if frame.Level > r.prev || (frame.Level == 0 && r.prev == 0) { | ||
r.names = append(r.names, frame.Name) | ||
} else { | ||
r.flush() | ||
if frame.Level == 0 { | ||
r.names = []string{frame.Name} | ||
} else { | ||
r.names = append(r.names[:frame.Level], frame.Name) | ||
} | ||
} | ||
r.val = frame.SampledTime | ||
r.prev = frame.Level | ||
} | ||
|
||
func (r *renderer) flush() { | ||
r.callBack([]byte(strings.Join(r.names, ";")), int(r.val.Milliseconds())) | ||
} |
This file contains 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 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